Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import *
- from numpy import arange
- from itertools import combinations
- p = 7
- q = p // 2
- # all points on the circle
- pts = [(cos(x), sin(x)) for x in arange(0, 2*pi, 2*pi / p)]
- # all segments of the star
- segments = list(zip(pts, pts[q:] + pts[:q]))
- def intersection(s1, s2):
- x1, y1 = s1[0]
- x2, y2 = s1[1]
- x3, y3 = s2[0]
- x4, y4 = s2[1]
- d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
- if d == 0:
- return None
- return (((x1*y2 - y1*x2) * (x3 - x4) - (x1 - x2) * (x3*y4 - y3*x4)) / d,
- ((x1*y2 - y1*x2) * (y3 - y4) - (y1 - y2) * (x3*y4 - y3*x4)) / d)
- # remove duplicates from all intersections of segments
- all_pts = list(set(filter(None, [intersection(s[0], s[1]) for s in combinations(segments, 2)])))
- def to_polar(x):
- return (sqrt(x[0]**2 + x[1]**2),
- atan2(x[1], x[0]))
- def from_polar(x):
- return (x[0] * cos(x[1]),
- x[0] * sin(x[1]))
- all_pts_polar = list(map(to_polar, all_pts))
- all_pts_polar.sort(key=lambda x: -x[1])
- # start is at alpha=0, so in the middle, need to put it first and rotate
- all_pts_polar = all_pts_polar[len(all_pts_polar) // 2:] + all_pts_polar[:len(all_pts_polar) // 2]
- all_pts_polar = [(r, alpha + pi / 2) for (r, alpha) in all_pts_polar]
- result = list(map(from_polar, all_pts_polar))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement