Advertisement
acclivity

pyFindOddTriangle

Aug 11th, 2022
896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. # Given the coordinates of the points of a set of triangles, find the odd one out
  2. # The triangles are of similar shapes, but different sizes and rotations
  3. # One triangle is a mirror image
  4. # Mike Kerry - August 2022
  5.  
  6. import math
  7. rad2deg = 180/math.pi
  8.  
  9.  
  10. def get_length_and_slope(t, w, v):
  11.     # Compute the length of one side of a triangle
  12.     # and the bearing of the end point from the start point
  13.     # t points to a triangle. w and v refer to 2 points of the triangle
  14.  
  15.     dx = t[v][0] - t[w][0]      # x-axis distance between points
  16.     dy = t[v][1] - t[w][1]      # y-axis distance between points
  17.  
  18.     # math.hypot calculates a hypotoneuse    sqrt(dx*dx + dy*dy)
  19.     lgth = math.hypot(dx, dy)
  20.  
  21.     # atan2 computes a bearing between 2 points, between -pi and +pi radians
  22.     # given x and y axis distances dx and dy between the 2 points
  23.     ang = rad2deg * math.atan2(dy, dx)
  24.     return lgth, ang
  25.  
  26.  
  27. a = [(3.0, 6.0), (3.0, 3.0), (7.0, 3.0)]
  28. b = [(4.4, 3.2), (4.4, 5.0), (2.0, 5.0)]
  29. c = [(3.0, 5.2), (5.4, 5.2), (5.4, 2.0)]
  30. d = [(4.0, 1.0), (5.5, 1.0), (5.5, 3.0)]
  31. arr = [a, b, c, d]
  32.  
  33.  
  34. for j, tri in enumerate(arr):
  35.     print("\nTRIANGLE: ", chr(ord('A') + j))       # TRIANGLE: A thru D
  36.     # Assume the points of the triangle are labelled p, q and r
  37.     # Hence the sides are pq, qr, and pr
  38.     p, q, r = 0, 1, 2
  39.     lenpq, slopepq = get_length_and_slope(tri, p, q)
  40.     lenqr, slopeqr = get_length_and_slope(tri, q, r)
  41.     lenpr, slopepr = get_length_and_slope(tri, p, r)
  42.     circ = lenpq + lenqr + lenpr    # total circumference of triangle
  43.     # for each side, compute its proportion of the overall circumference
  44.     percentpq = lenpq * 100 / circ
  45.     percentqr = lenqr * 100 / circ
  46.     percentpr = lenpr * 100 / circ
  47.     print(f"Percentages: {percentpq:.0f} {percentqr:.0f} {percentpr:.0f}")
  48.  
  49.     # compute the directed angle at point q (between sides pq and qr)
  50.     angleq = slopeqr - slopepq
  51.     print(f"Angle q: {angleq:.1f}")
  52.  
  53. # Results:
  54. # TRIANGLE:  A
  55. # Percentages: 25 33 42
  56. # Angle q: 90.0
  57. #
  58. # TRIANGLE:  B
  59. # Percentages: 25 33 42
  60. # Angle q: 90.0
  61. #
  62. # TRIANGLE:  C
  63. # Percentages: 25 33 42
  64. # Angle q: -90.0
  65. #
  66. # TRIANGLE:  D
  67. # Percentages: 25 33 42
  68. # Angle q: 90.0
  69.  
  70. # From the above, we can see that triangle C is the odd one.
  71. # Its angle 'q' is reversed compared to the other three triangles
  72.  
  73. print(math.pi)
  74.  
  75.  
  76.  
  77.  
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement