Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Given the coordinates of the points of a set of triangles, find the odd one out
- # The triangles are of similar shapes, but different sizes and rotations
- # One triangle is a mirror image
- # Mike Kerry - August 2022
- import math
- rad2deg = 180/math.pi
- def get_length_and_slope(t, w, v):
- # Compute the length of one side of a triangle
- # and the bearing of the end point from the start point
- # t points to a triangle. w and v refer to 2 points of the triangle
- dx = t[v][0] - t[w][0] # x-axis distance between points
- dy = t[v][1] - t[w][1] # y-axis distance between points
- # math.hypot calculates a hypotoneuse sqrt(dx*dx + dy*dy)
- lgth = math.hypot(dx, dy)
- # atan2 computes a bearing between 2 points, between -pi and +pi radians
- # given x and y axis distances dx and dy between the 2 points
- ang = rad2deg * math.atan2(dy, dx)
- return lgth, ang
- a = [(3.0, 6.0), (3.0, 3.0), (7.0, 3.0)]
- b = [(4.4, 3.2), (4.4, 5.0), (2.0, 5.0)]
- c = [(3.0, 5.2), (5.4, 5.2), (5.4, 2.0)]
- d = [(4.0, 1.0), (5.5, 1.0), (5.5, 3.0)]
- arr = [a, b, c, d]
- for j, tri in enumerate(arr):
- print("\nTRIANGLE: ", chr(ord('A') + j)) # TRIANGLE: A thru D
- # Assume the points of the triangle are labelled p, q and r
- # Hence the sides are pq, qr, and pr
- p, q, r = 0, 1, 2
- lenpq, slopepq = get_length_and_slope(tri, p, q)
- lenqr, slopeqr = get_length_and_slope(tri, q, r)
- lenpr, slopepr = get_length_and_slope(tri, p, r)
- circ = lenpq + lenqr + lenpr # total circumference of triangle
- # for each side, compute its proportion of the overall circumference
- percentpq = lenpq * 100 / circ
- percentqr = lenqr * 100 / circ
- percentpr = lenpr * 100 / circ
- print(f"Percentages: {percentpq:.0f} {percentqr:.0f} {percentpr:.0f}")
- # compute the directed angle at point q (between sides pq and qr)
- angleq = slopeqr - slopepq
- print(f"Angle q: {angleq:.1f}")
- # Results:
- # TRIANGLE: A
- # Percentages: 25 33 42
- # Angle q: 90.0
- #
- # TRIANGLE: B
- # Percentages: 25 33 42
- # Angle q: 90.0
- #
- # TRIANGLE: C
- # Percentages: 25 33 42
- # Angle q: -90.0
- #
- # TRIANGLE: D
- # Percentages: 25 33 42
- # Angle q: 90.0
- # From the above, we can see that triangle C is the odd one.
- # Its angle 'q' is reversed compared to the other three triangles
- print(math.pi)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement