I'm writing a function that dots equally-spaced points on the circumference of a circle centered at origin, then draws lines between all points.
I have been messing with this code for a while now and I can not seem to get it to function as intended. I need to make a circle out of points from the given radius and number of points in turtle. This is what I have so far:
from turtle import *
def drawLineSeg (p, q):
up()
goto(p)
down()
goto(q)
def pntCirc (r, n):
for i in range(1, n+1):
up()
goto(0,0)
forward(r)
down()
dot(10)
L.append (pos())
right(360/n)
L = []
def clique (r, n):
pntCirc (r, n)
color ('blue')
for i in range(0, len(L)):
drawLineSeg (L[0], L[i])
drawLineSeg (L[1], L[i])
drawLineSeg (L[2], L[i])
drawLineSeg (L[3], L[i])
drawLineSeg (L[4], L[i])
drawLineSeg (L[5], L[i])
drawLineSeg (L[6], L[i])
drawLineSeg (L[7], L[i])
drawLineSeg (L[8], L[i])
drawLineSeg (L[9], L[i])
drawLineSeg (L[10], L[i])
drawLineSeg (L[11], L[i])
drawLineSeg (L[12], L[i])
drawLineSeg (L[13], L[i])
drawLineSeg (L[14], L[i])
drawLineSeg (L[15], L[i])
drawLineSeg (L[16], L[i])
drawLineSeg (L[17], L[i])
drawLineSeg (L[18], L[i])
drawLineSeg (L[19], L[i])
return
speed(50)
color ('red')
clique (300, 20)
# clique (300, 8)
# clique (300, 16)
hideturtle()
done()
In the clique function, I have tried using for loops and while loops, which is around what I need to use for this to work; I can't use any more technical stuff like enumerate, numpy, or matplotlib. I need to make this work for any amount of given points, not just the 20 points I tested for. It throws an IndexError when I test for 8 points, for example. How do I do this with one code, not hard-written?
clique (300, 20)
# clique (300, 8)
# clique (300, 16)
I have, as stated above, hard coded it to give me the full result (this is the full model that I hard coded the function for) when using 20 points, but if I just put in:
for i in range(0, len(L)):
drawLineSeg (L[0], L[i])
Any help would be greatly appreciated as I am currently stuck and am not sure how to proceed from here.
Great effort here. What you're drawing is a complete graph. You're very close, only missing an inner loop which is essentially what the hardcoded 20 calls simulates.
We need to find all combinations of points, so the logic is essentially a nested loop:
While moving the turtle from the center will work, I'd prefer to compute the points around the circumference of the circle mathematically. The formula for a point is:
... but the center is assumed to be 0 for our purposes (this is a good thing to make a parameter).
To compute all of the points around the circumference, we can iterate from 0 to 360 degrees in increments of 360 / n (the number of points we want to draw), then convert our angle to radians and plug into the formula. No lists required.
As a cosmetic aside, I'd prefer to avoid
from turtle import *which brings in too many functions into the namespace. Better toimport turtle, then use theturtleprefix going forward. I'd also prefer to create aTurtleinstance and pass it into the function so it's not reliant on global state.I'd also prefer to use parameters on the function for all of the available knobs. Center
xandywould be nice to add.The final result is: