Example: My Gosh, It’s Full of Stars!

Open in turtleSpaces IDE
Run starscape to create a galaxy of randomly-shaped and colored stars.

TO star :radius :points :size :filled
  ;takes number number number boolean (true or false)
  ;eg star 10 9 40 false
  ;or star 20 5 50 true
  
  pu dropanchor tether
  ;pull up the turtle pen, drop the 'anchor' to set the 'anchor point' to the current position, and do not change it if the turtle's rotation changes (tether)
  ;the anchor point is the point the orbit primitives orbit around
  
  if :filled [polyspot :radius :points * 2
    if oddp :points [rt 360 / :points / 4]]
  ;if creating a filled star, create a polygon to fill in the center
  ;if the star has an odd number of points, we need to turn a little
  
  pullout :radius
  ;pull out from the anchor point, which is currently the turtle's position
  
  repeat :points [
    ;repeat once for each point:
    
    pu make "position position
    ;set the :position container to the current turtle position
    
    orbitleft 360 / :points / 2
    ;orbit around the anchor point (in this case the center) to the left
    ;a fraction of 360 degrees divided by the number of points divided by 2
    
    pullout :size
    ;pull the turtle out from the center of the star (anchor point)
    
    setvectors direction :position
    ;point the turtle toward the previous position
    
    if not :filled [pd line distance position :position]
    ;if not making a filled star, create a line in front of the turtle of
    ;the length required for its end to be the previous turtle position.
    
    if :filled [frag]
    ;if creating a filled star, create a 'fragment' based on the last three
    ;turtle positions
    
    pu make "position position
    ;set the :position container to the current position
    
    pullin :size
    ;pull the turtle in toward the center of the star (anchor point)
    
    orbitleft 360 / :points / 2
    ;orbit to the left again a similar amound to the last orbitleft
    
    setvectors direction :position
    ;point toward the previous position
    
    if not :filled [pd line distance position :position]
    ;create a line if not a filled star similarly to the previous line
    
    if :filled [frag]
    ;create a fragment if a filled star similarly to the previous frag
    
  ]
END

TO starscape
  reset ht snappy:setposition [0 0 0] snappy:dropanchor repeat 200 [pu home randvec fd 1000 + random 1000 up 90 snappy:setvectors direction myrtle:position randfc randpc randps randfs star 10 + random 100 3 + random 20 10 + random 90 randbool]
END

One-a-Day: FRAG

The FRAG primitive creates a filled shape out of the current turtle position and her last two positions. For example:

FD 100 RT 90 FD 100 FRAG

will create a triangle.

While turtleSpaces has a variety of shape primitives, sometimes you need to create an arbitrary shape, and FRAG aids you in this.

Take this example, which draws a star:

TO star
  repeat 4 [
    forward 100
    right 170
    forward 86
    frag
    left 70
    forward 86
    right 170
    forward 100
    frag
    right 180
  ]

We can change the number of sides the star has by changing the number of repeats and fiddling with the values a bit:

TO star2
  repeat 5 [
    fd 100 rt 170
    fd 86 frag
    lt 88 fd 86
    rt 170 fd 100
    frag rt 180
  ]
END

First, let’s change our star procedures so they can take a :size parameter, like so:

TO star1 :size
  repeat 4 [
    fd 100 * :size
    rt 170
    fd 86 * :size
    frag
    lt 70
    fd 86 * :size
    rt 170
    fd 100 * :size
    frag
    rt 180
  ]
END

In this case, :size is a ratio, that is, 0.5 will make a star half the size, and 2 will make a star twice the size of the default.

You can change the color of the star using the SETFILLCOLOR primitive, or set a random fill color with RANDFC.

The following procedures create a sky full of stars:

TO star1 :size
  repeat 4 [
    fd 100 * :size
    rt 170
    fd 86 * :size
    frag
    lt 70
    fd 86 * :size
    rt 170
    fd 100 * :size
    frag
    rt 180
  ]
END

TO star2 :size
  repeat 5 [fd 100 * :size rt 170 fd 86 * :size frag lt 88 fd 86 * :size rt 170 fd 100 * :size frag rt 180]
END

TO star3 :size
  repeat 6 [fd 100 * :size rt 170 fd 86 * :size frag lt 100 fd 86 * :size rt 170 fd 100 * :size frag rt 180]
END

TO star4 :size
  repeat 7 [fd 100 * :size rt 170 fd 86 * :size frag lt 108.625 fd 86 * :size rt 170 fd 100 * :size frag rt 180]
END

TO star5 :size
  repeat 8 [fd 100 * :size rt 170 fd 86 * :size frag lt 115 fd 86 * :size rt 170 fd 100 * :size frag rt 180]
END

TO stars
  reset cam:setposition [0 0 0]
  cam:fixate [0 0 0]
  cam:setviewpoint [0 0 0]
  cam:newworker [forever [up 0.1 lt 0.1 rr 0.1 wait 1]]
  repeat 200 [
    pu home randori fd 400 + random 1000 up 90
    lt random 60 pd randpc randfc randfs randps
    make "size (10 + random 90) / 100
    run {pick [star1 star2 star3 star4 star5] :size}
  ]
  
END

Type STARS and press Enter to see the stars!

FRAG’s sister, SHARD creates a three-dimensional FRAG with depth beneath it. This depth is supplied as a parameter, in turtle-units, eg. SHARD 5. Try replacing FRAG with SHARD 5 in one of your star procedures and see what happens! (You’ll need to drag the camera around to see the sides of the star)