Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Latest commit

 

History

History
84 lines (57 loc) · 3.9 KB

README.md

File metadata and controls

84 lines (57 loc) · 3.9 KB

Karel Rasterizer

An image rasterizer written in Python using CodeHS' Karel API


Background

This (very, very overkill) project was done for a basic assignment in the AP Computer Science Principles ("AP CSP") class where Karel the robot dog was supposed to paint just a few blocks by a series of hard-coded steps.

It was designed to use CodeHS' Karel Python API.

Demos:

(Note: Make sure to set the speed to maximum when running the below demos.)

Online interactive demo (Edit-able source code)
Online demo (No source code)

In order to draw a different scene, simply replace the array on lines number 127-148 (All of which start with 't.extend'.) with one of the samples provided in the 'scenes.txt' file in the './examples' folder.


What made my program interesting was the way it drew these images, it first constructed a 20x20 array (Framebuffer) with a one-to-one representation of the world's grids and then painted the world according to the array onto the world; it could even "upscale" the image array for larger worlds (Such as 40x40), this works just as a monitor in real life does. (Albeit slower, which is fair considering that it is running on an already-slow interpreted programming language like Python, implemented in a poorly-optimized JavaScript emulator, inside of a browser on top of the hardware.)

Although initially it is faster to hard-code the steps for drawing an image rather than writing a rasterizer to draw it for you, the latter would allow new images to be drawn in a matter of minutes as opposed to hours of hard-coding it again in the former, because you would just be modifying an array that looks similar to the world that it's going to get painted on.

For the rest of the technical details, refer to the comments inside of the source code.


You could also copy-paste the contents of the 'main.py' file in the './src' folder into CodeHS' editor in assignment number 2.1.10 ("Create your UltraKarel Image!") and see the rasterizer in action.

The source code is idomatic Python with some minor syntactic modifications to work-around CodeHS' Python interpreter bugs & limitations.


Video demonstration of a night scene image being drawn onto the world:


Demo GIF's placeholder, If the .GIF file does not load properly then you could try manually opening the 'demo.gif' file in the 'docs' folder.
It takes exactly 20 seconds to fully render a 20x20 (400 pixels) world. So, a row containg 20 pixels will be drawn every second.

Some other scenes:
(Also found inside of the './docs' folder.)



The 20x20 alongside 40x40 renderings of Iran's Neo-Persian royal standard. (https://en.wikipedia.org/wiki/Derafsh_Kaviani)



The 40x40 variant of the night scene drawn earlier.