1

So, I'm using a special turtle graphics set of classes in Java (but they have all the regular commands: move, paint, turn, etc.). I'm trying to draw a six-point star (which is effectively two triangles).

Could anyone perhaps give some pseudo-code as to how I could draw the star? I understand how the graphics work, and I can calculate the angles of the points (they're 30 degrees) but I don't really get how I could put it all together...?

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105

3 Answers3

0

First, I'm not sure what you mean by saying the angles are 30 degrees. The interior angles of an equilateral triangle are all 60 degrees. (They have to add up to 180, remember?). But a turtle has to turn through the exterior angles (the supplements of these), which are 120 degrees.

The other thing you need to figure out is how far to move the turtle in between drawing the triangles. This is the length of a side of the hexagon that circumscribes your star. With a bit of geometry, you can figure out that this length is the length of a side of the triangle divided by the square root of 3.

Here's some Logo code for you (that's got to be better than pseudo-code, right?)

TO Star6 :size
  ; Draw First Triangle
  REPEAT 3 [FD :size RT 120]

  ; Reposition for Second Triangle
  PU
  RT 90
  FD :size / SQRT 3
  LT 90
  PD

  ; Draw Second Triangle
  REPEAT 3 [FD :size LT 120]

  ; Return to starting position
  PU
  LT 90
  FD :size / SQRT 3
  RT 90
  PD
END

; Draw some stars of various sizes and colors
CS
SETCOLOR "Red
Star6 50
SETCOLOR "Green
Star6 100
SETCOLOR "Blue
Star6 200

You can play with it at this online Logo interpreter: http://www.calormen.com/logo/

Tim Goodman
  • 23,308
  • 7
  • 64
  • 83
0

Since folks claim that Python is executable pseudo-code, how about:

SIDE_LENGTH = 2 * HEIGHT / sqrt(3)
CIRCUMSCRIBED_RADIUS = 2 * HEIGHT / 3

for triangle in (1, 2):
    turtle.penup()
    turtle.right(150)
    turtle.forward(CIRCUMSCRIBED_RADIUS * triangle)
    turtle.right(30)
    turtle.pendown()

    for side in (1, 2, 3):
        turtle.right(120)
        turtle.forward(SIDE_LENGTH)

You'll need to supply HEIGHT as the height of one of the triangles that make up the star. You may also need to set the starting orientation depending on which way you want your star to point:

enter image description here

cdlane
  • 40,441
  • 5
  • 32
  • 81
0

Start at the top of the star, facing north (up).

Turn south (180 clockwise), and then anti-clockwise the angle (30). Go (distance).

Turn north (150 anti-clockwise), and then clockwise (60). Go(distance).

Turn south (120 clockwise), and then anti-clockwise (90). Go(distance).

Et cetera. This should give you a relatively simple idea as to how to write an algorithm for each step.

Puppy
  • 144,682
  • 38
  • 256
  • 465