Weekly Programming Challenge #4

I’m running a bit behind this week! Vacationing on beautiful Vashon Island has me struggling to keep up with my usual schedule. Plenty to do around here — the farmer’s market, the beach, hiking…lots of activities!

Some of my favorite programming activities are those that involve producing graphical output. There’s just something really satisfying about having the results of my experiments displayed visually, whether it be drawing mazes, curves, knots, or graphs. To that end, this week’s programming challenge will be drawing lines!

But first, let’s recap last week’s challenge…

Recap: Challenge #3

Last week’s challenge was to write a program that would solve mazes. Hard
mode was to find the shortest solution to a maze with multiple possible
solutions. And here’s the coolest thing: everyone that submitted a solution,
did hard mode!

Two in particular qualify for “surprise me”, I think:

The other hard mode submissions:

I gave it a try in Elixir, for the first time, and was pleasantly surprised that I was able to wrap my mind around it enough to succeed at hard mode! You can see my submission here: https://github.com/jamis/weekly-challenges/tree/master/003-maze-solver

Now, on to challenge #4!

Programming Challenge #4

For this challenge, you’re not allowed to use any graphics or image library. None! In short, you will represent an image as a raw matrix of RGB pixel values, which you’ll draw to using a minimal API of your own invention. When you’re done drawing to it, you’ll write it out to a file in PPM format.

Let’s break this down.

First, the raw image data. You must represent the image as an array of (at least) RGB values, where each color is (at least) 8 bits in size. That is to say, each pixel will be three numbers: red (0–255), green (0–255), and blue (0–255). If you wish to support an alpha channel, that’s fine as well. Your image API only needs to support a single method, “draw pixel”, to write a pixel of a given color, to a given (x,y) coordinate. (More methods are fine, if you want to go nuts — but you only need to support the one method.)

Second, drawing! This is the fun part. You’ll get to implement Bresenham’s line algorithm (which should be really, really easy, because there is pseudocode for it all over the Internet). You should allow arbitrary lines, with arbitrary start and end points, in arbitrary colors.

Third, write it as a PPM file. You may use either the ASCII (P3) format, or the binary (P6) format. (If you have trouble finding an app that can view PPM files, you may use a tool like ImageMagick to convert your output to PNG or whatever pleases you.)

That’s it! Doing just that will score you a “normal mode” submission. For hard mode, then… Let’s try something a bit different this time.


Normal mode is, as stated before, these three things:

We’ll call that the basis, and give you one point for those three things together. For each of the following options, you’ll earn another point:

And if you really want to get creative, shoot for “surprise me” mode! This doesn’t need to be viciously difficult, but it ought to be something more than the basics, applied in a way that is unexpected or thoughtful. See what you can come up with!

This challenge will run until Saturday, August 27th, at 12:00 MDT (18:00 UTC).

Submissions are now closed for this challenge, but feel free to try your hand at it anyway! If you’re following along, the next week’s challenge is “Priority Queues and Binary Heaps”. See you there!

Programmer, Author, Lifelong Learner. http://www.mazesforprogrammers.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store