0

#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!

Axiu666
  • 1
  • 1
  • 1
    Welcome to Stack Overflow. "I looked up online and it says for a star polygon :n/density cannot be further reduced." I cannot make any sense of this. What exactly did you look up online? How did you do so? What is "it", and how does "it" "say" that? What is `n`, and what is `density`? – Karl Knechtel Jul 11 '22 at 19:23
  • 1
    "My problem is that I know I cannot draw all the stars possible. (example: if there's a big number divisible by 3,7, and 13, my code will not work)" I can't understand what any of this means. Please read [ask] and [mre]. Take a big step back, forget everything about how you want to try to explain the problem, and follow this template exactly: 1) What input do I need to supply to the function in order to see a problem? 2) When I supply that input, **what should happen**? 3) When I supply that input, **what does happen**, and **how is that different**? – Karl Knechtel Jul 11 '22 at 19:25
  • Next, try to understand what the code does, [step by step](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). If you cannot find a problem, explain the intended logic to us, step by step. For example, where the code says `elif math.gcd(n,3) == 1:`, what do you expect that to mean? What is the **intended purpose** of that test? Why does it test `3`, and not any other number? What do the `n` and `density` values represent? – Karl Knechtel Jul 11 '22 at 19:28
  • Also, try to compare your existing description of the problem to the code. You say: "So I wrote this code to make sure it changes the density if the GCD of n and density is not 1." So, tell me: **where in the code** is the part that is supposed to compute the GCD of `n` and `density`? What would the code to do that look like? – Karl Knechtel Jul 11 '22 at 19:29
  • "I am currently working on a lab problem that asks me to draw a star polygon with n points using both iteration and recursion." How many stars should be drawn when `star_iter` is called? Just one? Every possible one with `n` points? Something else? But no matter what, it should draw stars with `n` points, right? Therefore, *does it make sense for the logic inside the function, to change the value of `n`*? "So I wrote this code to make sure it changes the density..." so... do that? – Karl Knechtel Jul 11 '22 at 19:31
  • Thank you so much for pointing out the unclear part of my post! I have now modified it and I hope it makes more sense! – Axiu666 Jul 11 '22 at 22:26

0 Answers0