Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. from math import *
  2. from numpy import arange
  3. from itertools import combinations
  4.  
  5. p = 7
  6. q = p // 2
  7.  
  8. # all points on the circle
  9. pts = [(cos(x), sin(x)) for x in arange(0, 2*pi, 2*pi / p)]
  10. # all segments of the star
  11. segments = list(zip(pts, pts[q:] + pts[:q]))
  12.  
  13. def intersection(s1, s2):
  14.     x1, y1 = s1[0]
  15.     x2, y2 = s1[1]
  16.     x3, y3 = s2[0]
  17.     x4, y4 = s2[1]
  18.     d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
  19.  
  20.     if d == 0:
  21.         return None
  22.     return (((x1*y2 - y1*x2) * (x3 - x4) - (x1 - x2) * (x3*y4 - y3*x4)) / d,
  23.             ((x1*y2 - y1*x2) * (y3 - y4) - (y1 - y2) * (x3*y4 - y3*x4)) / d)
  24.  
  25. # remove duplicates from all intersections of segments
  26. all_pts = list(set(filter(None, [intersection(s[0], s[1]) for s in combinations(segments, 2)])))
  27.  
  28. def to_polar(x):
  29.     return (sqrt(x[0]**2 + x[1]**2),
  30.             atan2(x[1],  x[0]))
  31.  
  32. def from_polar(x):
  33.     return (x[0] * cos(x[1]),
  34.             x[0] * sin(x[1]))
  35.  
  36. all_pts_polar = list(map(to_polar, all_pts))
  37. all_pts_polar.sort(key=lambda x: -x[1])
  38.  
  39. # start is at alpha=0, so in the middle, need to put it first and rotate
  40. all_pts_polar = all_pts_polar[len(all_pts_polar) // 2:] + all_pts_polar[:len(all_pts_polar) // 2]
  41. all_pts_polar = [(r, alpha + pi / 2) for (r, alpha) in all_pts_polar]
  42.  
  43. result = list(map(from_polar, all_pts_polar))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement