offset.py 560 B

123456789101112131415161718192021
  1. # offset curve of piecewise cornu curves
  2. from math import *
  3. import pcorn
  4. from clothoid import mod_2pi
  5. def seg_offset(seg, d):
  6. th0 = seg.th(0)
  7. th1 = seg.th(seg.arclen)
  8. z0 = [seg.z0[0] + d * sin(th0), seg.z0[1] - d * cos(th0)]
  9. z1 = [seg.z1[0] + d * sin(th1), seg.z1[1] - d * cos(th1)]
  10. chth = atan2(z1[1] - z0[1], z1[0] - z0[0])
  11. return [pcorn.Segment(z0, z1, mod_2pi(chth - th0), mod_2pi(th1 - chth))]
  12. def offset(curve, d):
  13. segs = []
  14. for seg in curve.segs:
  15. segs.extend(seg_offset(seg, d))
  16. return pcorn.Curve(segs)