chiark / gitweb /
moebius: break out DiscreteBezier (nfc)
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 7 Apr 2018 20:00:14 +0000 (21:00 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 7 Apr 2018 20:00:14 +0000 (21:00 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
moebius.py

index 58d79f63978cde303e5eb5bea293ad60ad1fd4bf..aadb42ad8b88679c84b8b7755c14e9af1f889048 100644 (file)
@@ -68,6 +68,15 @@ class Twirler(ParametricCircle):
     r = tw.radius(theta)
     return cos(zeta) * r + sin(zeta) * tw._axis
 
+class DiscreteBezier():
+  def __init__(b, cp, nt, bezier_constructor=DoubleCubicBezier):
+    b.nt = nt
+    b._t_vals = np.linspace(0, 1, b.nt+1)
+    b._continuous_bezier = bezier_constructor(cp)
+  def point_at_it(b, it):
+    t = b._t_vals[it]
+    return b._continuous_bezier.point_at_t(t)
+
 class MoebiusHalf:
   def __init__(m, nu, nt):
     '''
@@ -81,9 +90,8 @@ class MoebiusHalf:
     m.nt      = nt
     m._thetas = [ u * tau for u in np.linspace(0, 1, nu+1) ]
     m._cp2b = BezierSegment([ (c,) for c in [0.33,0.33, 1.50]])
-    m._beziers = [ m._bezier(theta) for theta in m._thetas ]
-    m._t_vals = np.linspace(0, 1, m.nt+1)
-  def _bezier(m, theta, constructor=DoubleCubicBezier):
+    m._dbeziers = [ m._dbezier(theta) for theta in m._thetas ]
+  def _dbezier(m, theta, dconstructor=DiscreteBezier):
     cp = [None] * 4
     cp[0] =               m.edge   .point(theta)
     cp[3] =               m.midline.point(theta*2)
@@ -95,7 +103,7 @@ class MoebiusHalf:
     #      file=sys.stderr)
     cp[1] = cp[0] + cp1scale * m.edge   .dirn (theta)
     cp[2] = cp[3] + cp2scale * m.midline.dirn (theta*2)
-    return constructor(cp)
+    return dconstructor(cp, m.nt)
   def point(m, iu, it):
     '''
     0 <= iu <= nu     meaning 0 <= u <= 1
@@ -105,8 +113,7 @@ class MoebiusHalf:
     0 <= it <= nt     across the half-traverse
                        0 is the edge, 1 is the midline
     '''
-    t = m._t_vals[it]
-    return np.array(m._beziers[iu].point_at_t(t))
+    return np.array(m._dbeziers[iu].point_at_it(it))
 
   def details(m, iu, t):
     '''