Saturday, February 26, 2011

A rotated polygon

Following on from the rotated square we extended the script to draw the regular polygons from nonagon to triangle.
We chose custom colours for each of the polygons.
reset

for $y = 9 to 3 step -1
{
  if $y == 3 {
    pencolor 194, 0, 0 
  }
  else if $y == 4 {
    pencolor 0, 194, 0 
  }
  else if $y == 5 {
    pencolor 0, 0, 194 
  }
  else if $y == 6 {
    pencolor 0, 194, 194 
  }
  else if $y == 7 {
    pencolor 194, 194, 0 
  }
  else if $y == 8 {
    pencolor 194, 0, 194
  }
  else if $y == 9 {
    pencolor 255, 239, 59
  }
  repeat 24 {
    turnright 15
    repeat $y {
      forward 60
      turnleft 360/$y
    }
  }
}

Wednesday, February 23, 2011

A rotated square

We started with a square and then rotated it.
reset

pencolor 194, 0, 0 


repeat 24 {
  turnright 15
  repeat 4 {
    forward 120
    turnleft 90
  }
}

Saturday, February 19, 2011

Learning RGB - more colours

This is a rewrite of the previous Learning RGB script. It loops through a few more colours.

reset
penwidth 2
penup

turnleft 90
forward 113
turnleft 90
forward 113
turnleft 180
pendown

for $r = 0 to 1 {
  for $g = 0 to 1 {
    for $b = 0 to 1 {
      for $x = 0 to 255 step 2 {
        pencolor $r*$x,$g*$x,$b*$x
        forward 16
        turnright 90
        forward 1
        turnright 90
        pencolor $r*($x + 1),$g*($x + 1),$b*($x + 1)
        forward 16
        turnleft 90
        forward 1
        turnleft 90
      }  

      penup
      turnleft 90
      forward 256
      turnright 90
      forward 17
      pendown
    }
  }
}

Thursday, February 10, 2011

Learning RGB

This is a simple script, which draws three lines changing the red, green and blue values.

reset
penwidth 2
penup

turnleft 90
forward 113
turnleft 90
forward 113
turnleft 180

pendown
for $x = 0 to 255 step 2 {
  pencolor $x,0,0
  forward 16
  turnright 90
  forward 1
  turnright 90
  pencolor ($x + 1),0,0
  forward 16
  turnleft 90
  forward 1
  turnleft 90
}  

penup
turnleft 90
forward 256
turnright 90
forward 17
pendown

pendown
for $x = 0 to 255 step 2 {
  pencolor 0,$x,0
  forward 16
  turnright 90
  forward 1
  turnright 90
  pencolor 0,($x + 1),0
  forward 16
  turnleft 90
  forward 1
  turnleft 90
}  

penup
turnleft 90
forward 256
turnright 90
forward 17
pendown

pendown
for $x = 0 to 255 step 2 {
  pencolor 0,0,$x
  forward 16
  turnright 90
  forward 1
  turnright 90
  pencolor 0,0,($x + 1)
  forward 16
  turnleft 90
  forward 1
  turnleft 90
}

Tuesday, February 8, 2011

Rainbow

Often we start by sketching a picture of what we want to code. This is a sketch of the rainbow, with RGB colours in hexadecimal.


This is the rainbow we created based on the above drawing. It is not the most efficient code, but it works.


Here is a screenshot of the finished program followed by a video of the turtle in action.

Thursday, February 3, 2011

Spiral traffic light

Today we created a spiral traffic light. We started with a plain spiral, changed the colour to red, then added a green spiral and finally a yellow one in the middle.
reset

# red spiral
for $x = 1 to 72 {
  pencolor 4 * $x, 0, 0
  forward 123
  turnright 67
  forward 7
  turnleft 156
  forward 7
  turnleft 156
}

penup
turnleft 90
forward 20
turnleft 90
forward 50
pendown

# green spiral
for $x = 1 to 72 {
  pencolor 0 , 4 * $x, 0
  forward 123
  turnright 67
  forward 7
  turnleft 156
  forward 7
  turnleft 156
}

penup
forward 50
turnleft 190
pendown

# yellow spiral
for $x = 1 to 72 {
  pencolor  4 * $x, 4 * $x, 0
  forward 123
  turnright 67
  forward 7
  turnleft 156
  forward 7
  turnleft 156
}

Wednesday, February 2, 2011

Star

This code uses some basic trigonometry to draw stars with various point sizes. Some examples of starts with 5 and 10 points.


And an overlay of starts with points ranging from 2 to 10:

reset

turnleft 90

$baseLength = 40
$starHeight = 60
$starLength = sqrt($starHeight * $starHeight + ($baseLength/2)*($baseLength/2))

for $x = 2 to 10 {
 repeat $x {
  # draw the base line
  forward $baseLength
  # calculate the angle of triangle at the bottom corner
  $angle = arctan (($baseLength/2) / $starHeight)
  # turn to draw the side
  turnleft 90 + $angle
  # draw the side
  forward $starLength
  # turn to draw the other side
  turnleft 2 * (90 - $angle)
  # draw the other side
  forward $starLength
  # turn to draw the base (again)
  turnleft 90 + $angle
  # draw the base (again)
  forward $baseLength
  # turn the turtle to start the next point
  turnright 360 / $x
 }
}