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

1 comment:

  1. Thanks for your cammands but we cant paste the commands to kturtle programme

    ReplyDelete