public
Last active

  • Download Gist
k2.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.