Guest User

Untitled

a guest
Feb 17th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. #!/usr/bin/python
  2. import sys
  3. from math import pi, sin, cos, tan, asin, acos, atan, sqrt
  4.  
  5. def arc(fromx, fromy, tox, toy, radius, sweep):
  6. global x0, y0
  7. return '<path stroke="#000" stroke-width="2" fill="none" d="M%.3f,%.3f A%.3f,%.3f,0,0,%d,%.3f,%.3f" />'"n" % (x0+fromx, y0-fromy, radius, radius, sweep, x0+tox, y0-toy)
  8.  
  9. def line(fromx, fromy, tox, toy):
  10. global x0, y0
  11. return '<path stroke="#000" stroke-width="2" fill="none" d="M%.3f,%.3f L%.3f,%.3f" />'"n" % (x0+fromx, y0-fromy, x0+tox, y0-toy)
  12.  
  13. def linevia(fromx, fromy, viax, viay, tox, toy):
  14. global x0, y0
  15. return '<path stroke="#000" stroke-width="2" fill="none" d="M%.3f,%.3f L%.3f,%.3f %.3f,%.3f" />'"n" % (x0+fromx, y0-fromy, x0+viax, y0-viay, x0+tox, y0-toy)
  16.  
  17. if __name__ == '__main__':
  18.  
  19. # Parse parameters
  20. if len(sys.argv) >= 5 and len(sys.argv) <= 7:
  21. w = float(sys.argv[1])
  22. t = float(sys.argv[2])
  23. r1 = float(sys.argv[3])
  24. r2 = float(sys.argv[4])
  25. if len(sys.argv) >= 6:
  26. beta = float(sys.argv[5]) * pi / 180.0
  27. if beta > 0.0 and len(sys.argv) >= 7:
  28. b = float(sys.argv[6])
  29. else:
  30. b = 0.0
  31. else:
  32. beta = 0.0
  33. b = 0.0
  34.  
  35. else:
  36. sys.stderr.write("n"
  37. "Usage: %s width thickness lowerradius upperradius [ apexangle [ apexwidth ] ] > output.svgn"
  38. "n"
  39. "If specified, the apex angle is in degrees.n"
  40. "n" % sys.argv[0])
  41. sys.exit(1)
  42.  
  43. # The base angle (lower arc angle) is phi:
  44. phi = pi - acos( (0.5*(w-b) - r1 - t - r2*cos(0.5*beta)) / (r1 + t + r2))
  45. x1 = 0.5*w - t - r1
  46. x2 = x1 + (r1 + t + r2)*cos(phi)
  47. y2 = (r1 + t + r2)*sin(phi)
  48. w1 = 0.5*b
  49. w2 = x1 + r1*cos(phi)
  50. w3 = x1 + (r1+t)*cos(phi)
  51. h1 = r1*sin(phi)
  52. h2 = (r1+t)*sin(phi)
  53. h3 = y2 - (r2 + t)*sqrt(1 - (x2*x2)/((r2+t)*(r2+t)))
  54. h5 = y2 - r2*sin(0.5*beta)
  55. h4 = h5 - 0.5*b*tan(0.5*beta)
  56. if beta > 0.0:
  57. h = h5 + 0.5*b/tan(0.5*beta)
  58. else:
  59. h = h5
  60.  
  61. w0 = x1
  62. w4 = x1 + r1
  63. w5 = x1 + r1 + t
  64.  
  65. pixelwidth = round(2*t + w)
  66. pixelheight = round(2*t + h)
  67. x0 = int(pixelwidth / 2)
  68. y0 = int(round(h + t))
  69.  
  70. sys.stdout.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>' "n"
  71. '<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d" viewBox="0 0 %d %d" >' "n" % (pixelwidth, pixelheight, pixelwidth, pixelheight))
  72. sys.stdout.write('<rect x="0" y="0" width="%d" height="%d" stroke="none" fill="#fff" />' "n" % (pixelwidth, pixelheight))
  73.  
  74. # Left-side lower outer arc
  75. sys.stdout.write(arc( -w5,0, -w3,h2, r1+t, 1 ))
  76.  
  77. # Left-side upper outer arc
  78. sys.stdout.write(arc( -w3,h2, -w1,h5, r2, 0 ))
  79.  
  80. # Cap
  81. if w1 > 0:
  82. sys.stdout.write(linevia( -w1,h5, 0,h, w1,h5 ))
  83.  
  84. # Right-side upper outer arc
  85. sys.stdout.write(arc( w1,h5, w3,h2, r2, 0 ))
  86.  
  87. # Right-side lower outer arc
  88. sys.stdout.write(arc( w3,h2, w5,0, r1+t, 1 ))
  89.  
  90. # Right-side lower inner arc
  91. sys.stdout.write(arc( w4,0, w2,h1, r1, 0 ))
  92.  
  93. # Right-side upper inner arc
  94. sys.stdout.write(arc( w2,h1, 0,h3, r2+t, 1 ))
  95.  
  96. # Left-side upper inner arc
  97. sys.stdout.write(arc( 0,h3, -w2,h1, r2+t, 1 ))
  98.  
  99. # Left-size lower inner arc
  100. sys.stdout.write(arc( -w2,h1, -w4,0, r1, 0 ))
  101.  
  102. # Arc separator lines
  103. sys.stdout.write(line( -w2,h1, -w3,h2 ))
  104. sys.stdout.write(line( w2,h1, w3,h2 ))
  105. sys.stdout.write(line( 0,h4, 0,h3 ))
  106. sys.stdout.write(line( -w5,0, -w4,0 ))
  107. sys.stdout.write(line( w5,0, w4,0 ))
  108.  
  109. # Cap bottom
  110. if w1 > 0:
  111. sys.stdout.write(linevia( -w1,h5, 0,h4, w1,h5 ))
  112.  
  113. sys.stdout.write("</svg>n")
  114.  
  115. python ogee.py 800 10 500 600 45 15 > out.svg
Add Comment
Please, Sign In to add comment