Monday, April 4, 2011

A random spiral script

Today we started with a spiral, then added some colours, moved it into a command and called it multiple times.


reset
canvassize 1000,1000
# start in the middle
gox 500
goy 500

#
# This command creates a spiral
#
# $scale: how big should the spiral be
# $redcolor, $greencolor, $bluecolor: RGB colours for the spiral
#
learn spiral $scale, $redcolor, $greencolor, $bluecolor {
  pencolor $redcolor, $greencolor, $bluecolor
  for $i = 0 to 71 {
    forward $scale * 20
    turnright 30
    forward $scale * 5
    turnleft 175
    forward $scale * 100
  }
}

# draw the spirals
for $i = 0 to 40 {
  penup
  turnright random 0,360
  forward 200
  turnleft 90
  # if we are too close to the edge jump somewhere random
  # so we don't wander off the canvas
  if (getx < 20 or getx > 980) {
    gox (random 100,900)
  }
  if (gety < 20 or gety > 980) {
    goy (random 100,900)
  }    
  pendown
  spiral (random 1,20)/5, (random 0,255), (random 0,255), (random 0,255)
}

Wednesday, March 30, 2011

Overlaid spirals in colour

This is an extension of the simple spiral script to overlay multiple spirals of increasing size in red, green and blue. The first image is colourful, but very messy.

The messiness is caused by the turtle starting from the direction that the spiral ended in. For the second image we reset the direction, so it looks more organised.

reset
canvassize 500,500

for $y = 5 to 20 {
  go 250,250
  direction 0
  if $y ==5 {
    pencolor 0, 0, 0
  }
  else if $y < 11 {
    pencolor 105 + ((10 - $y)*30), 0, 0
  }
  else if $y < 16 {
    pencolor 0, 105 + ((15 - $y)*30), 0
  }
  else {
    pencolor 0, 0, 105 + ((20 - $y)*30)
  }
  for $x = 2 to 20 {
    repeat $x {
      forward $y
      turnright 105 / $x
    }
  }
}

Friday, March 25, 2011

A messy colour spiral

A rather messy spiral. We originally wrote this in black, but then added the random colour to show a little more of what was happening.

This script is a great one to experiment with changing the forward and turnright values.
reset

for $x = 2 to 50 {
  pencolor (random 0,255), (random 0,255), (random 0,255)
 repeat $x {
  forward 20
  turnright 555 / $x
}

Wednesday, March 16, 2011

A simple spiral

This script draws a simple spiral. It came from a simple change to the polygon spiral script.

reset
penup
turnleft 180
forward 150
turnright 90
pendown

for $x = 2 to 80 {
 repeat $x {
  forward 10
  turnright 105 / $x
}

Sunday, March 6, 2011

Polygon with colour

The plain black polygon was visually boring, so we started by changing the colour to gold:


Friday, March 4, 2011

Polygon

This is a simple script drawing polygons. Note how as the number of lines in the polygon increases the shape looks more and more like a circle.
reset

penup
turnleft 180
forward 150
turnright 90
pendown

for $x = 2 to 50 {
 repeat $x {
  forward 20
  turnright 360 / $x
}

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
 }
}

Sunday, January 30, 2011

Another spiral variant

Another variant of a spiral with a small inner circle and longer outer arms.
We wanted to add some colour so we split the loop into three parts and added a different colour for each loop.

reset

penup
forward 50
turnright 180
pendown

# red layer
pencolor 255, 0, 0
for $x = 1 to 60 {
  forward 109
  turnright 100
  forward 39
  turnleft 29
  forward 78
  turnleft 189
}

# green layer
pencolor 0, 255, 0
for $x = 1 to 60 {
  forward 109
  turnright 100
  forward 39
  turnleft 29
  forward 78
  turnleft 189
}

# blue layer
pencolor 0, 0, 255
for $x = 1 to 60 {
  forward 109
  turnright 100
  forward 39
  turnleft 29
  forward 78
  turnleft 189
}

Saturday, January 29, 2011

A spiral variant

A spiral variant. The kids enjoy changing the numbers and experimenting.
reset

penup
forward 50
turnright 180
pendown

for $x = 1 to 200 {
  forward 109
  turnright 100
  forward 39
  turnleft 29
  forward 78
  turnleft 189
}

Four coloured spirals

The first logo (kturtle) script for this blog. It is a simple one which demonstrates learning a command. We used the HSL Color Picker to choose the RGB values.


This is the script to create the above image. If you change the numbers after forward and turnright, turnleft in the spiral command you can change the spiral shape.

reset

# start in the right spot
penup
turnright 90
forward 100
turnleft 90
forward 20
pendown

#
# This command creates a spiral
#
# $scale: how big should the spiral be
# $redcolor, $greencolor, $bluecolor: RGB colours for the spiral
#
learn spiral $scale, $redcolor, $greencolor, $bluecolor {
  pencolor $redcolor, $greencolor, $bluecolor
  for $i = 0 to 71 {
    forward $scale * 20
    turnright 30
    forward $scale * 5
    turnleft 175
    forward $scale * 100
  }
}

spiral 2, 255, 245, 56

penup
turnleft 100
forward 220
pendown
spiral 1, 179, 0, 173

penup
turnright 110
forward 200
pendown
spiral 0.5, 0, 235, 121

penup
turnright 110
forward 200
pendown
spiral 1, 255, 77, 88