Remember that points on a circle are given by the formula
x = r * cos(theta)
y = r * sin(theta)
where r
is the radius and theta
is the angle subtended by the ray extending from the origin to the point and the x-axis. The vertices of an octagon are equally spaced on a circle. So if we choose equally spaced theta
s from 0 to 2 pi radians, then we'll have the vertices of an octagon:
import turtle
from math import pi, cos, sin
def ngon(n, dist, phase, shift=(0,0)):
theta = 2 * pi / n
x0, y0 = shift
turtle.penup()
x = dist * cos(phase) + x0
y = dist * sin(phase) + y0
turtle.goto(x, y)
turtle.pendown()
for i in range(1, n+1):
x = dist * cos(i*theta + phase) + x0
y = dist * sin(i*theta + phase) + y0
turtle.goto(x, y)
def demo_ngon():
turtle.speed(0.1)
turtle.width(2.0)
ngon(8, dist=100, phase=0)
ngon(8, dist=68, phase=0)
turtle.mainloop()
demo_ngon()