I'm having a hard time creating a program that will draw all of the stars from 3 to 7. The program needs to have:

• The variables numberOfSides, numberOfTrips, firstNumberOfSides, and lastNumberOfSides.
• A series of stars one after the other for 3 to 7 sides
• The screen must clear after each star is fully drawn
• The straight-line segments need to be proportional by numberOfTrips/numberOfSides

So far, all I have is this:

``````import turtle
wn = turtle.Screen()
jake = turtle.Turtle()

for i in range(3,8):
numberOfSides = i
numberOfTrips = ((numberOfSides+1)//2-1)
for n in range(numberOfSides):
jake.forward(100)
jake.right(360*numberOfTrips/i)

turtle.resetscreen()
``````

I'm especially having a hard time getting all of the shapes to be drawn. I'm missing a pentagon and hexagon.

## All 5 Replies

You need to define what a star IS.

Basically you have n points and you should make cyclic arithmetical sequence that contains all the points and have a difference more then 1 (in case of a square and triangle it can be 1).
For example for a poligon this sequence is: 1,3,5,2,4
As you can see, there are two stars with seven points.

If you've got that, you can calculate the angle. Then you can draw the points.

If you do the math, then the angle is :
Let us assume that the difference between the points is D.
The interior angle of the convex hull is: `180 − 1/n*360`

The angle at the star point is:
interior_angle - (180-D*interior_angle)

``````import turtle
wn = turtle.Screen()
jake = turtle.Turtle()

numberOfSides = 7
sequence_diff = 2 # the sequence 1,3,5,7,2,4,6
interior_angle = 180-1.0/numberOfSides*360
star_angle=interior_angle - (180-sequence_diff*interior_angle)

for _ in range(numberOfSides):
jake.forward(100)
jake.right(star_angle)

turtle.resetscreen()
``````

Finding out what the sequence difference is, ...
is left for the user..

And I did not do the math correctly.

`star_angle=interior_angle - (sequence_diff*180-(sequence_diff-1)*interior_angle)`

If you change sequence_diff to 1, then you get the polygon, as expected.

Oh no. It only works if all numbers between 2 and the number of sides -2 are not divider of number of sides.

So it does not work on hexagon.

There is a little hiccup for stars with 3 and 4 sides. You can divide 360 by the number of sides for stars with 5 sides and up, but for 3 or 4 sides this gives an angle greater than or equal to 90 which will not work. I will leave this conundrum up to the OP to solve for now. Both star_angle and left_angle have to be calculated differently for angles >= 90. Draw it out on a piece of paper and measure the angles and you should be fine.

``````def draw_star(number_of_sides):
star_angle=360.0/number_of_sides  ## float for Python 2.X
left_angle = star_angle*2
print number_of_sides, star_angle
turtle.penup()
turtle.goto(-150, -100)
turtle.pendown()
for _ in range(number_of_sides):
turtle.forward(100)
turtle.right(star_angle)
turtle.forward(100)
turtle.left(left_angle)

raw_input("Press a key")
turtle.clear()

for num_sides in range(3, 8):
draw_star(num_sides)
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.