craftmath.py 705 B

1234567891011121314151617181920212223242526272829
  1. import numpy as np
  2. def rotation_matrix(a):
  3. return np.array([[np.cos(a), -np.sin(a)],
  4. [np.sin(a), np.cos(a)]])
  5. def margin(angle, size, v1, v2):
  6. vv = v2 - v1
  7. vv = vv / np.sqrt((vv**2).sum())
  8. edgelen = size / np.sin(angle)
  9. v3 = v1 + edgelen * np.dot(rotation_matrix(angle), vv)
  10. v4 = v2 - edgelen * np.dot(rotation_matrix(-angle), vv)
  11. return np.c_[v2, v4, v3, v1].T
  12. def regular_polygon(n, v1, v2):
  13. ang = 2 * np.pi / n
  14. mat = rotation_matrix(ang)
  15. vs = np.c_[v1, v2]
  16. vv = v2 - v1
  17. vprev = v2
  18. for i in range(n - 2):
  19. v = np.dot(mat, vv) + vprev
  20. vs = np.c_[vs, v]
  21. vv = v - vprev
  22. vprev = v
  23. return vs.T