prjbrook

centroid7.py

Jul 24th, 2024
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. def centroid_info(vertexA,vertexB,vertexC):
  2.     def midpoint(p1,p2):
  3.         x1, y1 = p1
  4.         x2, y2 = p2 #unpack
  5.         mp1= (x1+x2)/2
  6.         mp2 =(y1+y2)/2
  7.         return (mp1,mp2) #Not sure whether to return this as ordered pair or invividiual numbers or list.
  8.     def line_equation(p1, p2):
  9.         x1, y1 = p1
  10.         x2, y2 = p2
  11.         m = (y2 - y1) / (x2 - x1) if x2 != x1 else float('inf')
  12.         b = y1 - m * x1 if m != float('inf') else None
  13.         return (m, b)
  14.     def intersection(m1, b1, m2, b2):
  15.         if m1 == m2:
  16.             return None  # Lines are parallel
  17.         if m1 == float('inf'):
  18.             x = b1
  19.             y = m2 * x + b2
  20.         elif m2 == float('inf'):
  21.             x = b2
  22.             y = m1 * x + b1
  23.         else:
  24.             x = (b2 - b1) / (m1 - m2)
  25.             y = m1 * x + b1
  26.         return x, y
  27.     def intersection2(L1,L2): #where L1 is line (m,c)
  28.         m1,b1 = L1
  29.         m2,b2 = L2      #Pb lines here
  30.         if m1 == m2:
  31.             return None  # Lines are parallel
  32.         if m1 == float('inf'):
  33.             x = b1
  34.             y = m2 * x + b2
  35.         elif m2 == float('inf'):
  36.             x = b2
  37.             y = m1 * x + b1
  38.         else:
  39.             x = (b2 - b1) / (m1 - m2)
  40.             y = m1 * x + b1
  41.         return (x, y)       #so returns a point as an ordered pair
  42.  
  43.  
  44.     mpBC = midpoint(vertexB,vertexC)
  45.     #print ("Midxxx point of BC is ", mpBC)
  46.     mpAC = midpoint(vertexA,vertexC)
  47.     #print ("Mid point of AC is ", mpAC)
  48.     mpAB = midpoint(vertexA,vertexB)
  49.     #print ("Mid point of AB is ", mpAB)
  50.     midpoints = (mpBC,mpAC,mpAB) #That is, the midpoints opposite A,B,C resp.
  51.  
  52.     medianA = line_equation(vertexA,mpBC) #median from A
  53.     #print('Median from A Is ',medianA)
  54.     medianB = line_equation(vertexB,mpAC) #median from A
  55.     #print('Median from A Is ',medianB)
  56.     medianC = line_equation(vertexC,mpAB) #median from A
  57.     #print('Median from C Is ',medianC)
  58.     medians = (medianA, medianB, medianC) #That is median line joining A to mid-point of BC etc
  59.  
  60.     centroid = intersection2(medianA,medianB)
  61.     #print('Centroid is ',centroid)
  62.     return_group = (centroid, medians, midpoints)
  63.     print("Return group for centroid is; ",return_group)
  64.     return return_group
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment