Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def centroid_info(vertexA,vertexB,vertexC):
- def midpoint(p1,p2):
- x1, y1 = p1
- x2, y2 = p2 #unpack
- mp1= (x1+x2)/2
- mp2 =(y1+y2)/2
- return (mp1,mp2) #Not sure whether to return this as ordered pair or invividiual numbers or list.
- 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 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
- def intersection2(L1,L2): #where L1 is line (m,c)
- m1,b1 = L1
- m2,b2 = L2 #Pb lines here
- 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) #so returns a point as an ordered pair
- mpBC = midpoint(vertexB,vertexC)
- #print ("Midxxx point of BC is ", mpBC)
- mpAC = midpoint(vertexA,vertexC)
- #print ("Mid point of AC is ", mpAC)
- mpAB = midpoint(vertexA,vertexB)
- #print ("Mid point of AB is ", mpAB)
- midpoints = (mpBC,mpAC,mpAB) #That is, the midpoints opposite A,B,C resp.
- medianA = line_equation(vertexA,mpBC) #median from A
- #print('Median from A Is ',medianA)
- medianB = line_equation(vertexB,mpAC) #median from A
- #print('Median from A Is ',medianB)
- medianC = line_equation(vertexC,mpAB) #median from A
- #print('Median from C Is ',medianC)
- medians = (medianA, medianB, medianC) #That is median line joining A to mid-point of BC etc
- centroid = intersection2(medianA,medianB)
- #print('Centroid is ',centroid)
- return_group = (centroid, medians, midpoints)
- print("Return group for centroid is; ",return_group)
- return return_group
Advertisement
Add Comment
Please, Sign In to add comment