prjbrook

ortho6.py

Jul 24th, 2024
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. #
  2. #import math
  3. import matplotlib.pyplot as plt
  4. def orthocenter_info(A, B, C):
  5.     def line_equation(p1, p2):
  6.         x1, y1 = p1
  7.         x2, y2 = p2
  8.         m = (y2 - y1) / (x2 - x1) if x2 != x1 else float('inf')
  9.         b = y1 - m * x1 if m != float('inf') else None
  10.         return m, b
  11.  
  12.     def perpendicular_line(point, m):
  13.         x, y = point
  14.         m_perp = -1 / m if m != 0 else float('inf')
  15.         b_perp = y - m_perp * x if m_perp != float('inf') else None
  16.         return (m_perp, b_perp)
  17.  
  18.     def intersection(m1, b1, m2, b2):
  19.         if m1 == m2:
  20.             return None  # Lines are parallel
  21.         if m1 == float('inf'):
  22.             x = b1
  23.             y = m2 * x + b2
  24.         elif m2 == float('inf'):
  25.             x = b2
  26.             y = m1 * x + b1
  27.         else:
  28.             x = (b2 - b1) / (m1 - m2)
  29.             y = m1 * x + b1
  30.         return (x, y)
  31.  
  32.     # Calculate equations of two altitudes. First get eqns of sides of triangle
  33.     m_BC, b_BC = line_equation(B, C)
  34.     m_AC, b_AC = line_equation(A, C)
  35.     m_AB, b_AB = line_equation(A, B)
  36.    
  37.     m_A_perp, b_A_perp = perpendicular_line(A, m_BC)
  38.     m_B_perp, b_B_perp = perpendicular_line(B, m_AC)
  39.     m_C_perp, b_C_perp = perpendicular_line(C, m_AB)
  40.     perp_lines = ( (m_BC,b_BC), (m_B_perp, b_B_perp),(m_C_perp, b_C_perp) )
  41.    
  42.    
  43.     Afoot = intersection(m_A_perp,b_A_perp, m_BC,b_BC)
  44.    
  45.     Bfoot = intersection(m_B_perp,b_B_perp, m_AC,b_AC)
  46.    
  47.     Cfoot = intersection(m_C_perp,b_C_perp, m_AB,b_AB)
  48.    
  49.     feet = (Afoot,Bfoot,Cfoot)
  50.  
  51.     # Find the intersection of the two altitudes
  52.     orthocenter = intersection(m_A_perp, b_A_perp, m_B_perp, b_B_perp)
  53.  
  54.     return orthocenter, perp_lines, feet
  55.  
  56. # Test the function
  57. # vertexA = (0, 0)
  58. # vertexB = (60, 10)
  59. # vertexC = (30, 60)
  60. #
  61. # orthoInfo = orthocenter_info(vertexA, vertexB, vertexC)
  62. # print("Result is: ",orthoInfo)
  63. #
  64. # orthocentrePoint, perpLines, altitudeFeet = orthoInfo
  65. # orthcentrex, orthcentrey = orthocentrePoint
  66. #
  67. # fA,fB,fC = altitudeFeet
  68. # fAx,fAy =fA
  69. # fBx,fBy =fB
  70. # fCx,fCy =fC
  71. #
  72. #
  73. # fig, ax = plt.subplots(figsize=(6, 6))
  74. # Ax,Ay = vertexA
  75. # Bx,By = vertexB
  76. # Cx,Cy = vertexC
  77. # circle = plt.Circle((orthcentrex, orthcentrey), 0.5, fill=False, color='r')
  78. # ax.add_artist(circle)
  79. # ax.plot([Ax,Bx,Cx,Ax], [Ay,By,Cy,Ay], 'r-', linewidth=2) #this plots the triangle
  80. # ax.plot([Ax,fAx], [Ay, fAy], 'r-', linewidth=1)
  81. # ax.plot([Bx,fBx], [By, fBy], 'r-', linewidth=1)
  82. # ax.plot([Cx,fCx], [Cy, fCy], 'r-', linewidth=1)
  83. #
  84. # plt.show()
  85. #ortho5.py works ok 20/07/24
  86.  
Advertisement
Add Comment
Please, Sign In to add comment