Advertisement
creamygoat

Circle from two points and a sweep angle

Apr 25th, 2013
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. from math import *
  2.  
  3. def VSum(*VectorArgs):
  4.   if len(VectorArgs) == 1:
  5.     Vectors = VectorArgs[0]
  6.   else:
  7.     Vectors = VectorArgs
  8.   Result = tuple(Vectors[0])
  9.   for i in range(1, len(Vectors)):
  10.     Result = tuple(a + b for a, b in zip(Result, Vectors[i]))
  11.   return Result
  12.  
  13. def VDiff(A, B):
  14.   return tuple(x - y for x, y in zip(A, B))
  15.  
  16. def VLengthSquared(A):
  17.   return sum(x * x for x in A)
  18.  
  19. def VLength(A):
  20.   return sqrt(VLengthSquared(A))
  21.  
  22. def VScaled(A, Scale):
  23.   return tuple(x * Scale for x in A)
  24.  
  25. def VPerp(A):
  26.   return (-A[1], A[0])
  27.  
  28.  
  29. def CentredArcFromArcSweep(P1, P2, SweepAngle):
  30.  
  31.   circle = 2.0 * pi
  32.  
  33.   Diff = VDiff(P2, P1)
  34.   Dist = VLength(Diff)
  35.   s = sin(0.5 * SweepAngle)
  36.   Radius = Dist / (2.0 * s)
  37.  
  38.   M = VScaled(VSum(P1, P2), 0.5)
  39.   t = tan(0.5 * SweepAngle)
  40.   Centre = VSum(M, VPerp(VScaled(Diff, 0.5 / t)))
  41.  
  42.   R1 = VDiff(P1, Centre)
  43.   StartAngle = atan2(R1[1], R1[0])
  44.   EndAngle = StartAngle + SweepAngle
  45.   EndAngle = ((EndAngle + pi) % circle) - pi
  46.  
  47.   return Centre, Radius, StartAngle, EndAngle
  48.  
  49. def Main():
  50.  
  51.   P1 = (0.7071, 2.0 - 0.7071)
  52.   P2 = (0.7071, 2.0 + 0.7071)
  53.   Angle = radians(90.0)
  54.  
  55.   print "Start point, end point and (signed) sweep angle:"
  56.   print P1, P2, degrees(Angle)
  57.  
  58.   Centre, Radius, StartAngle, EndAngle = CentredArcFromArcSweep(P1, P2, Angle)
  59.  
  60.   print "Centre, radius and start and end angles:"
  61.   print Centre, Radius, degrees(StartAngle), degrees(EndAngle)
  62.  
  63.  
  64. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement