Created
September 22, 2010 10:38
-
-
Save shawnchin/591485 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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