#Thank you so much for commenting. I have revised my post. It's my first time using stack overflow and I did not ask the question properly.(Plus English is my second language, so my phrasing could be confusing...Sorry about that!)
I am currently working on a lab problem that asks me to draw a star polygon with n points using both iteration and recursion. When the function star_iter(size,n) is called,it should draw out ONE star with n points and "size" long sides.
I looked up star polygons online (https://www.youtube.com/watch?v=pfhO3HctMQA) and it says for a star polygon:n/density cannot be further reduced. "n" stands for the number of points that are equally spread in a circle. (see picture) star polygon n "density" is hard to explain for me so here's an example: A star polygon with a density of 2 tells you to connect every second point together.star polygon density Examples of working star polygons: The ones with x next to it are not considered star polygons The formula I used to compute the interior angle of star polygons: Sum of angle in star polygon= 180 * (n - 2*density)
So I wrote this code to make sure it changes the density if the GCD of n and density is not 1.
#Star iteration
def gcd(a,b): #find the greatest common divisor of 2 numbers
while b != 0:
a,b = b, a % b
return a
def star_iter(size,n):
#size stands for the length of the sides
#n stands for number of points in the star.
density = 2 #set default density to 2
turtle.pendown()
if n <= 4:
print("Need more points!")
elif n % 2 == 1: #draw star polygons with odd amount of points
for i in range (n):
turtle.forward(size)
# turtle.left(180-(360/(2*n)))
turtle.left(180-((180*(n-2*density))/n))
elif math.gcd(n,3) == 1: #if even, check if 3 works as density(if n/3 is not reducible, use 3 as the density)
for i in range (n):
turtle.forward(size)
turtle.right(180-((180*(n-2*(density+1)))/n))
elif math.gcd(n,7) == 1:#if 3 does not work, check if 7 works as density(if n/7 is not reducible, use 3 as the density)
for i in range (n):
turtle.forward(size)
turtle.right(180-((180*(n-2*(density+5)))/n))
#star_iter(100,14)
From my code, you might see I need to check EVERY prime number to determine the density of the star...I think that's too much work to do but I have no idea about how to simplify it.
Through testing, my code returns the right shapes besides star_iter(100,6). It returns a hexagon that is not a star for sure...It should return a David star. The hexagon it draws
What should I do to fix my code? Am I on the wrong track? Please help me out, thank you!