Advertisement
prjbrook

incentre7.py

Jul 24th, 2024
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.85 KB | None | 0 0
  1. #incentre7.py works ok. Puts in 3 yellow bisector lines that meet at incentre
  2. import math
  3. import matplotlib.pyplot as plt
  4. def line_equation2(p1, p2):
  5.     x1, y1 = p1
  6.     x2, y2 = p2
  7.     m = (y2 - y1) / (x2 - x1) if x2 != x1 else float('inf')
  8.     b = y1 - m * x1 if m != float('inf') else None
  9.     #print("m,b",m,b)
  10.     return (m,b)
  11. def intersection2(L1,L2): #where L1 is line (m,c)
  12.     m1,b1 = L1
  13.     m2,b2 = L2      #Pb lines here
  14.     if m1 == m2:
  15.         return None  # Lines are parallel
  16.     if m1 == float('inf'):
  17.         x = b1
  18.         y = m2 * x + b2
  19.     elif m2 == float('inf'):
  20.         x = b2
  21.         y = m1 * x + b1
  22.     else:
  23.         x = (b2 - b1) / (m1 - m2)
  24.         y = m1 * x + b1
  25.     return (x, y)       #so returns a point as an ordered pair
  26. def line_equation(p1, p2):
  27.     x1, y1 = p1
  28.     x2, y2 = p2
  29.     m = (y2 - y1) / (x2 - x1) if x2 != x1 else float('inf')
  30.     b = y1 - m * x1 if m != float('inf') else None
  31.     return (m, b)
  32. def equations_of_sides_of_triangle(VA,VB,VC):
  33.     Line_AB= line_equation(VA,VB)
  34.     Line_BC= line_equation(VB,VC)
  35.     Line_CA= line_equation(VC,VA)
  36.     return Line_AB,Line_BC,Line_CA
  37. def incentre_info(V1,V2,V3):
  38. ##    def line_equation(p1, p2):
  39. ##        x1, y1 = p1
  40. ##        x2, y2 = p2
  41. ##        m = (y2 - y1) / (x2 - x1) if x2 != x1 else float('inf')
  42. ##        b = y1 - m * x1 if m != float('inf') else None
  43.     Line_AB= line_equation(V1,V2)
  44.     Line_BC= line_equation(V2,V3)  
  45.     Line_CA= line_equation(V3,V1)
  46.     x1,y1=V1
  47.     x2,y2 = V2
  48.     x3,y3 = V3
  49.     # Calculate the lengths of the three sides
  50.     print('Vertices are A,B,C ', x1,y1,x2,y2,x3,y3)
  51.    
  52.     c = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
  53.     a = math.sqrt((x3 - x2)**2 + (y3 - y2)**2)
  54.     b = math.sqrt((x1 - x3)**2 + (y1 - y3)**2)
  55.    
  56.     # Calculate the semi-perimeter
  57.     s = (a + b + c) / 2
  58.    
  59.     # Calculate the area of the triangle
  60.     area = 0.5 * (a * b * math.sin(math.acos((a**2 + b**2 - c**2) / (2 * a * b))))
  61.    
  62.     # Calculate the center and radius of the incircle
  63.     x_center = (a * x1 + b * x2 + c * x3) / (a + b + c)
  64.     y_center = (a * y1 + b * y2 + c * y3) / (a + b + c)
  65.     I = (x_center,y_center)     #I = incentre
  66.     radius = area / s
  67.  
  68.     #medianA = line_equation(vertexA,mpBC) #median from A
  69.     angle_bisector_lineA = line_equation2(V1,I) #median from A
  70.     angle_bisector_lineB = line_equation2(V2,I) #median from A
  71.     angle_bisector_lineC = line_equation2(V3,I) #median from A
  72.          
  73.     foot_of_bisector_of_A = intersection2(angle_bisector_lineA,Line_BC)
  74.     foot_of_bisector_of_B = intersection2(angle_bisector_lineB,Line_CA)
  75.     foot_of_bisector_of_C = intersection2(angle_bisector_lineC,Line_AB)
  76.  
  77.     angle_bisectors = (angle_bisector_lineA,angle_bisector_lineB,angle_bisector_lineC)
  78.     #angle_bisectors2 = [angle_bisector_lineA,angle_bisector_lineB,angle_bisector_lineC]
  79.     feet_of_angle_bisectors =(foot_of_bisector_of_A, foot_of_bisector_of_B,foot_of_bisector_of_C)
  80.     #return angle_bisector_lineA,angle_bisector_lineB,angle_bisector_lineC, foot_of_bisector_of_A, foot_of_bisector_of_B,foot_of_bisector_of_C
  81.     group_angle_bisectors = I, angle_bisectors, feet_of_angle_bisectors
  82. #    return I, angle_bisectors, feet_of_angle_bisectors #so returns I-tuple, 3 tuples for lines, 3 tuples for feet
  83.     return group_angle_bisectors
  84.  
  85. vertexA = (0,0)
  86. vertexB =(60,10)
  87. vertexC =(30,60)
  88. angleBisectorInfo = incentre_info(vertexA,vertexB,vertexC)
  89. incentrePoint, bisectors, bisector_feet = angleBisectorInfo
  90. incentrex, incentrey = incentrePoint
  91. fA,fB,fC = bisector_feet
  92. fAx,fAy =fA
  93. fBx,fBy =fB
  94. fCx,fCy =fC
  95.  
  96. fig, ax = plt.subplots(figsize=(6, 6))
  97. Ax,Ay = vertexA
  98. Bx,By = vertexB
  99. Cx,Cy = vertexC
  100. circle = plt.Circle((incentrex, incentrey), 0.5, fill=False, color='y')
  101. ax.add_artist(circle)
  102. ax.plot([Ax,Bx,Cx,Ax], [Ay,By,Cy,Ay], 'b-', linewidth=2) #this plots the triangle
  103.  
  104. ax.plot([Ax,fAx], [Ay, fAy], 'y-', linewidth=1)
  105. ax.plot([Bx,fBx], [By, fBy], 'y-', linewidth=1)
  106. ax.plot([Cx,fCx], [Cy, fCy], 'y-', linewidth=1)
  107.  
  108. plt.show()
  109. three_lines=equations_of_sides_of_triangle(vertexA,vertexB,vertexC)
  110. print("three lines are ", three_lines)
  111.  
  112. #result = drop_perpendicular(x1,y1, x2, y2, x3, y3):
  113. result=incentre_info(vertexA,vertexB,vertexC)
  114. print(f"incentre information:incentre,angle_bisectors,feet=,{result}")
  115. #print('Area = ', area)
  116. #Find feet of angle_bisectors and sides of triangle ie. where does ..
  117. #..angle_bisector_lineA cut line_BC.
  118. #Use intersection2(L1,L2). ie intersection2(angle_bisector_lineA,line_BC)...
  119. #Call this foot_of_bisector_ofA
  120. #foot_of_bisector_of_A = intersection2(angle_bisector_lineA,line_BC)
  121. #print("Footof bisector of A is ", foot_of_besector_of_A)
  122. #The feet look OK for bisector of A,B but not for C
  123. # No, seems Ok now correction been made.
  124. #Next add incentre coords to final major return statement around line 75
  125. #works ok. See return statement around line 79.
  126.  
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement