Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- #import math
- import matplotlib.pyplot as plt
- def orthocenter_info(A, B, C):
- def line_equation(p1, p2):
- x1, y1 = p1
- x2, y2 = p2
- m = (y2 - y1) / (x2 - x1) if x2 != x1 else float('inf')
- b = y1 - m * x1 if m != float('inf') else None
- return m, b
- def perpendicular_line(point, m):
- x, y = point
- m_perp = -1 / m if m != 0 else float('inf')
- b_perp = y - m_perp * x if m_perp != float('inf') else None
- return (m_perp, b_perp)
- def intersection(m1, b1, m2, b2):
- if m1 == m2:
- return None # Lines are parallel
- if m1 == float('inf'):
- x = b1
- y = m2 * x + b2
- elif m2 == float('inf'):
- x = b2
- y = m1 * x + b1
- else:
- x = (b2 - b1) / (m1 - m2)
- y = m1 * x + b1
- return (x, y)
- # Calculate equations of two altitudes. First get eqns of sides of triangle
- m_BC, b_BC = line_equation(B, C)
- m_AC, b_AC = line_equation(A, C)
- m_AB, b_AB = line_equation(A, B)
- m_A_perp, b_A_perp = perpendicular_line(A, m_BC)
- m_B_perp, b_B_perp = perpendicular_line(B, m_AC)
- m_C_perp, b_C_perp = perpendicular_line(C, m_AB)
- perp_lines = ( (m_BC,b_BC), (m_B_perp, b_B_perp),(m_C_perp, b_C_perp) )
- Afoot = intersection(m_A_perp,b_A_perp, m_BC,b_BC)
- Bfoot = intersection(m_B_perp,b_B_perp, m_AC,b_AC)
- Cfoot = intersection(m_C_perp,b_C_perp, m_AB,b_AB)
- feet = (Afoot,Bfoot,Cfoot)
- # Find the intersection of the two altitudes
- orthocenter = intersection(m_A_perp, b_A_perp, m_B_perp, b_B_perp)
- return orthocenter, perp_lines, feet
- # Test the function
- # vertexA = (0, 0)
- # vertexB = (60, 10)
- # vertexC = (30, 60)
- #
- # orthoInfo = orthocenter_info(vertexA, vertexB, vertexC)
- # print("Result is: ",orthoInfo)
- #
- # orthocentrePoint, perpLines, altitudeFeet = orthoInfo
- # orthcentrex, orthcentrey = orthocentrePoint
- #
- # fA,fB,fC = altitudeFeet
- # fAx,fAy =fA
- # fBx,fBy =fB
- # fCx,fCy =fC
- #
- #
- # fig, ax = plt.subplots(figsize=(6, 6))
- # Ax,Ay = vertexA
- # Bx,By = vertexB
- # Cx,Cy = vertexC
- # circle = plt.Circle((orthcentrex, orthcentrey), 0.5, fill=False, color='r')
- # ax.add_artist(circle)
- # ax.plot([Ax,Bx,Cx,Ax], [Ay,By,Cy,Ay], 'r-', linewidth=2) #this plots the triangle
- # ax.plot([Ax,fAx], [Ay, fAy], 'r-', linewidth=1)
- # ax.plot([Bx,fBx], [By, fBy], 'r-', linewidth=1)
- # ax.plot([Cx,fCx], [Cy, fCy], 'r-', linewidth=1)
- #
- # plt.show()
- #ortho5.py works ok 20/07/24
Advertisement
Add Comment
Please, Sign In to add comment