Advertisement
Guest User

Involute of cubic curve

a guest
Mar 24th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. import os
  5. from math import *
  6.  
  7. from pyx import canvas, path, deco, trafo, style, text, color, deformer
  8.  
  9. rgb = color.rgb
  10. rgbfromhexstring = color.rgbfromhexstring
  11.  
  12. red, green, blue, yellow = (
  13.     rgbfromhexstring("#d00000"),
  14.     rgbfromhexstring("#006000"),
  15.     rgb.blue, rgb(0.75, 0.75, 0))
  16.  
  17.  
  18. def axis(c, x0, y0, x1, y1, extra):
  19.     w = x1-x0
  20.     mx = 0.01*w
  21.  
  22.     h = y1-y0
  23.     my = 0.01*h
  24.  
  25.     tick = 1.
  26.     while w/tick<10.:
  27.         tick *= 0.1
  28.     x = floor(x0)-1.
  29.     assert x<x0
  30.     assert x<x1
  31.     while x < x1:
  32.         x += tick
  33.         if x<x0:
  34.             continue
  35.         count = int(round(x/tick))
  36.         m = 0.5*my if (count%5) else my
  37.         c.stroke(path.line(x, -m, x, +m), [style.linewidth.thin]+extra)
  38.  
  39.     c.stroke(path.line(x0-tick, 0, x1+2*tick, 0),
  40.         extra+[deco.earrow(size=0.4)]) #[style.linewidth.thin]+extra)
  41.  
  42.  
  43. def plot(c, f, df, x0, x1):
  44.  
  45.     n = 100
  46.     dx = 1.*(x1-x0)/n
  47.     x = x0
  48.     y = y0 = y1 = f(x)
  49.  
  50.     ps = [path.moveto(x, y)]
  51.     for i in range(n):
  52.  
  53.         x2 = x+dx
  54.         y2 = f(x2)
  55.  
  56.         y0 = min(y0, y2)
  57.         y1 = max(y1, y2)
  58.  
  59.         r = dx
  60.         ps.append(path.lineto(x2, y2))
  61.  
  62.         x, y = x2, y2
  63.  
  64.     c.stroke(path.path(*ps), [red]+[style.linewidth.THick]+extra)
  65.  
  66.     axis(c, x0, y0, x1, y1, extra)
  67.  
  68.  
  69.  
  70. def main():
  71.  
  72.     os.chdir("frames")
  73.  
  74.     def f(x):
  75.         return x**3
  76.  
  77.     def df(x):
  78.         return 3*x**2
  79.  
  80.     global extra
  81.     sx, sy = 5., 5.
  82.     extra = [trafo.scale(sx=sx, sy=sy)]
  83.  
  84.     pts = []
  85.  
  86.     delta = 1.7 # path length
  87.  
  88.     N = 200
  89.     dx = 1.0 / N
  90.     x0 = -0.7
  91.     i = 0
  92.  
  93.     r = 1.2
  94.     clip = path.rect(-r*sx, -sy, 2*r*sx, 2*sy)
  95.  
  96.     while 1:
  97.  
  98.         c = canvas.canvas([canvas.clip(clip)])
  99.    
  100.         #x0 = -0.9 + i*dx
  101.         y0 = f(x0)
  102.         #print "x0=%.3f y0=%.3f" % (x0, y0)
  103.         s = (1 + df(x0)**2)**0.5
  104.         dy = f(x0+dx)-y0
  105.         #print dx, dy, delta,
  106.         delta -= (dx**2 + dy**2)**0.5
  107.         print i, "%.4f"%delta
  108.  
  109.         x1 = x0 + delta/s
  110.  
  111.         y1 = y0 + (x1-x0)*df(x0)
  112.         c.stroke(path.line(x0, y0, x1, y1), [style.linewidth.Thick]+extra)
  113.  
  114.         plot(c, f, df, -1., 1.)
  115.    
  116.         r = 0.02
  117.         c.fill(path.circle(x1, y1, r), extra)
  118.    
  119.         pts.append((x1, y1))
  120.  
  121.         if x1 > 1.:
  122.             break
  123.  
  124.         x0 += dx
  125.  
  126.         ps = [path.moveto(pts[0][0], pts[0][1])] + [path.lineto(p[0], p[1]) for p in pts[1:]]
  127.         if len(pts)>1:
  128.             c.stroke(path.path(*ps), [blue, style.linewidth.Thick]+extra)
  129.  
  130.         name = "pic-%.3d.pdf"%i
  131.         assert name.endswith(".pdf")
  132.         if i%10 == 0:
  133.             c.writePDFfile(name)
  134.             os.system("pdftocairo -png %s"%(name))
  135.  
  136.         i += 1
  137.  
  138.     print
  139.  
  140.  
  141. if __name__ == "__main__":
  142.  
  143.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement