Guest User

Untitled

a guest
Jul 26th, 2022
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # https://math.stackexchange.com/a/4500458/746312
  4.  
  5. from math import sqrt, sin, cos, acos, pi
  6.  
  7. class Point:
  8.     def __init__ (self, x, y):
  9.         self.x, self.y = x, y
  10.  
  11.     def __add__ (self, Q):
  12.         return Point (self.x + Q.x, self.y + Q.y)
  13.  
  14.     def __sub__ (self, Q):
  15.         return Point (self.x - Q.x, self.y - Q.y)
  16.  
  17.     def __truediv__ (self, Q):
  18.         return Point (self.x / Q, self.y / Q)
  19.  
  20.     def __mul__ (self, Q):
  21.         if isinstance (Q, Point):
  22.             return self.x * Q.x + self.y * Q.y
  23.         return Point (self.x * Q, self.y * Q)
  24.            
  25.     def __rmul__ (self, Q):
  26.         return self * Q
  27.  
  28.     def abs2 (self):
  29.         return self * self
  30.  
  31.     def abs (self):
  32.         return sqrt (self.abs2())
  33.  
  34.     def bot (self):
  35.         return Point (self.y, -self.x)
  36.  
  37.     def cos (self, Q):
  38.         return (self * Q) / (self.abs() * Q.abs())
  39.  
  40.     def __repr__ (self):
  41.         return "[%s, %s]" % (self.x, self.y)
  42.  
  43.     def rot (self, phi):
  44.         s, c = sin (phi), cos(phi)
  45.         return Point (c * self.x + s * self.y,
  46.                       c * self.y - s * self.x)
  47.  
  48. def center (A, B, phi):
  49.     """Get the center M of a circle through points A and B such that a
  50.       triangle ABC has an angle of PHI at C.  This means C lies on the
  51.       circumcircle around M through A and B."""
  52.  
  53.     rotAB = (B-A).rot (pi/2 - phi)
  54.     lam = (0.5 * (B*B + A*A) - A*B) / (rotAB * (B-A))
  55.     return A + lam * rotAB
  56.  
  57. def intersect (m1, m2, B):
  58.     """Let Ck be a circle around mk through B.  Return the intersection
  59.       of C1 and C2 that is not B."""
  60.     # Reflect B at line m1---m2.
  61.     m = m2 - m1
  62.     lam =  ((B - m1) * m) / (m * m)
  63.     beta = ((B - m1) * m.bot()) / (m * m)
  64.     return m1 + lam * m - beta * m.bot()
  65.  
  66. def show_solution (A, B, C, alpha, theta):
  67.     M1 = center (A, B, alpha)
  68.     M2 = center (B, C, theta)
  69.     print ("M1 =", M1)
  70.     print ("M2 =", M2)
  71.  
  72.     U = intersect (M1, M2, B)
  73.     print ("U =", U)
  74.  
  75.     w1 = (U-A).cos(U-B)
  76.     w2 = (U-B).cos(U-C)
  77.     print (acos(w1), "= pi *", acos(w1) / pi)
  78.     print (acos(w2), "= pi *", acos(w2) / pi)
  79.  
  80. A = Point (2, 3)
  81. B = Point (6, 5)
  82. C = Point (8, 5)
  83.  
  84. alpha = 45 * pi/180
  85. theta = 55 * pi/180
  86.  
  87. show_solution (A, B, C, alpha, theta)
  88. show_solution (C, B, A, theta, alpha)
  89.  
Advertisement
Add Comment
Please, Sign In to add comment