The maze book for programmers!
mazesforprogrammers.com
Algorithms, circle mazes, hex grids, masking, weaving, braiding, 3D and 4D grids, spheres, and more!
In considering week #14, I realized I should have split it up. It really ought to have built on the foundation of a turtle graphics system, which would have then been used to draw the L-system fractals. Well, live and learn!
This week, we’ll rectify that oversight. We’ll implement a basic turtle graphics system, which is great fun all on its own! But first, a recap of week #14...
For the 14th weekly programming challenge, we tackled L-system fractals. As I said, though, it was probably a bit too large of a challenge. Two people tackled it, and only one completed normal mode. (I tackled it, too, but wasn’t able to fit the full project into my schedule, either.)
The normal mode winner this week is Régis Kuckaertz, who provided a Javascript implementation. You can see his work here: https://github.com/regiskuckaertz/wc-14. Great job, Régis!
Lasse Ebert and I also tackled it, but neither of us quite managed to cross the finish line. Lasse’s code (Elixr) is here: https://github.com/lasseebert/jamis_challenge/tree/master/014_fract, and mine (Clojure) is here: https://github.com/jamis/weekly-challenges/tree/master/014-lindenmayer-systems.
Let’s see what week #15 brings!
Most of you are probably already familiar with turtle graphics, but here’s a speedy introduction if you’re not. The basic idea is that there is a cursor (the "turtle") that follows instructions you give it. Instructions are things like:
The turtle then interprets those instructions and produces the corresponding image. Easy, yeah?
For normal mode (and one point), create a turtle graphics system that supports the following instructions:
turn(degrees) – rotates the turtle, changing it’s heading.pen(upOrDown) – lifts the pen up or sets it down, depending on the (boolean) argument.move(distance) – moves the turtle the given distance along its current heading. This only draws something to the canvas if the pen is currently down.Assume the turtle starts in the center of the canvas, with a heading of 0 degrees corresponding to "up" (towards the top of the canvas). Produce an image drawn by your turtle.
That’s it!
For hard mode, you get to add some additional operations to your turtle. Each of the following items will earn you an additional point:
setColor(color) – sets the color that should be used to draw subsequent lines.setStyle(style) – sets the line style to be used for drawing. You should support at least solid lines, dashed lines, and dotted lines.setWidth(width) – sets the width of the line to be used for drawing.push() and pop() – these save the turtle’s state (position, orientation, pen status, etc.), allowing you to return the turtle to an earlier position.definePrimitive(name, ...) and drawPrimitive(name) – definePrimitive allows you to describe and name a series of commands, and then execute those commands at a later date via drawPrimitive.setScale(scale) – scales all distances and sizes by the given amount, defaulting to 1.0. (This is especially useful with a drawPrimitive API, because you can define the primitive against some unit size, and then change the draw scale before drawing the primitive.)Aside from new operations, you can earn additional points by adding any of the following features:
Feel free to shoot for "surprise me" mode, too! It doesn’t have to be any harder than normal mode, but it ought to implement the challenge (either normal mode or hard mode) in some surprising, clever, or otherwise delightful way. See what you can come up with!
This challenge will run until Saturday, November 12th, at 12:00pm MST (19:00 UTC).
The deadline for this challenge has passed, but feel free to try your hand at it anyway! Go ahead and submit a link to your solution in the comments, anytime. If you’re following along, the next week’s challenge is "The Midpoint Displacement Algorithm". See you there!
These articles lovingly written and prepared for your reading pleasure by Jamis Buck <[email protected]>. Follow me on Twitter and stalk my code at GitHub.
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.