Skip to content

Instantly share code, notes, and snippets.

@shawnchin
Created September 22, 2010 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawnchin/591485 to your computer and use it in GitHub Desktop.
Save shawnchin/591485 to your computer and use it in GitHub Desktop.
import sys, math
p,t,w=map(int,sys.argv[1:]) # read input
grid = [[' ']*(w+1) for i in range(0,w+1)] # prepare grid
def plot(x,y):
"""plots point on grid"""
grid[int(round(x))][int(round(y))]='x'
def line (x0, y0, x1, y1):
"""draw line using bresenham's line algorithm"""
steep = abs(y1 - y0) > abs(x1 - x0)
if steep:
(x0,y0,x1,y1) = (y0,x0,y1,x1)
if x0 > x1:
(x0,y0,x1,y1) = (x1,y1,x0,y0)
dx = x1 - x0
dy = abs(y1 - y0)
err = dx/2
y = y0
if y0 < y1:
ys = 1
else:
ys = -1
for x in range(int(round(x0)), int(math.ceil(x1))):
if steep:
plot(y,x)
else:
plot(x,y)
err = err - dy
if err < 0:
y = y + ys
err = err + dx
# find vertices
radius = w/2
calc_vert = lambda i,F: radius * ( 1 + F(math.pi * 2 / p * i))
point = [ (calc_vert(i,math.sin),calc_vert(i,math.cos)) for i in range(p)]
# connect vertices
for i in range(p):
x0,y0 = point[i]
x1,y1 = point[(i + t) % p]
line(x0,y0,x1,y1)
print "\n".join(["".join(i)for i in grid]) # print grid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment