Skip to content

Purogo MyFirst3DDrawing

Thomas E. Enebo edited this page May 20, 2012 · 2 revisions

So with 2 dimensional drawing under your belt, it is time to take it up a notch. 3 dimensional drawings require just remembering a second angle. For all horizontal changes in orientation we just turnleft (or turnright). For all vertical changes it is just turnup (or turndown). Sounds simple right? WRONG. But don't fret. Purogo is all about experimentation. Always remember that you can undraw your last failed experiment and try again. Be brave and move onward!

Let's try to make an outline of a cube. Enter in cube1:

turtle("cube", :pig) do |*args|
  length = (args[0] || 5).to_i
  block_type = (args[1] || :stone).to_sym

  # Set block to be used for all drawing
  block block_type  

  # Draw base of cube
  4.times do |i|
    mark i # Mark 4 lower corners
    forward length
    turnleft 90
  end

  # Draw one up pillar and save top of cube
  4.times do |i|
    goto i 
    turnup 90
    forward length
  end # Still at top of last pillar

  # Still pointing straight up after last draw turn back horizontal
  turndown 90

  # Draw top of cube
  4.times do |i|
    forward length
    turnleft 90
  end
end

Let's break this down by section and explain things:

  # Draw base of cube
  4.times do |i|
    mark i # Mark 4 lower corners
    forward length
    turnleft 90
  end

Here we are just writing out a square like we did in earlier exercises. This will be the base of our cube. The big difference is we are marking each corner of the square with the labels 0,1,2, and 3. This is so we know where to draw our vertical pillars. Let's look at the pillars next:

  # Draw one up pillar and save top of cube
  4.times do |i|
    goto i 
    turnup 90
    forward length
  end # Still at top of last pillar

For each pillar, we in-turn goto the corners we saved from the last section and then we 'turnip 90'. Remember that when we save positions using mark, it will also save the angles associated with it. So we need to tell the program at this point to turn upward 90 degrees. Then we just move forward to physically draw the pillar. We only have the top of the cube to draw now and our current position is at the top of the fourth vertical pillar. Let's re-orient ourselves by turning down by 90 degrees:

  # Still pointing straight up after last draw turn back horizontal
  turndown 90

Now let's finish this thing off:

  # Draw top of cube
  4.times do |i|
    forward length
    turnleft 90
  end

You may notice that the base and the top of drawing this cube is similiar. In Purogo/MyFirstTower we will use named blocks to capitalize on this fact.

Clone this wiki locally