# Find the Centroid of a triangle by finding the intersection # of two of the three "point to midpoint-of-side" lines. # # Problem posed by Lee Bradley in the Facebook "not just tiny-c # Programming Group". # # This solution coded by Jim McClanahan W4JBM (Jan 2023) # # Figure below is for reference: # # Points do not need to be in any particular order as # long as they form a triangle. # # ^ (Point 2) # / \ # Side 1 / \ Side 0 # / \ # Point 0 +-------+ Point 1 # Side 2 # # Side 0 is across the Angle formed at Point 0, etc. # cosmetic mods, user may now enter coordinates of 3 points - 7/9/23 - lrb from sympy import symbols, Eq, solve def line_between_points(P1, P2): # print(P1) # print(P2) # Calculate Slope y_delta = P2[1] - P1[1] x_delta = P2[0] - P1[0] slope = y_delta / x_delta # Y = Slope*X + Intercept # Intercept = Y - Slope*X intercept = P1[1] - slope*P1[0] print("Line through Point 1 and Point 2 is:", "y = ", slope, "* x + ", intercept) return(slope,intercept) # Definition of the three original points print("\nEnter x coordinate of Point 0") x0 = int(input()) print("\nEnter y coordinate of Point 0") y0 = int(input()) print("\nEnter x coordinate of Point 1") x1 = int(input()) print("\nEnter y coordinate of Point 1") y1 = int(input()) print("\nEnter x coordinate of Point 2") x2 = int(input()) print("\nEnter y coordinate of Point 2") y2 = int(input()) # x_given = [0, 4, 10] # y_given = [0, 0, 6] x_given = [x0, x1, x2] y_given = [y0, y1, y2] x_mid_side0 = (x_given[1] + x_given[2]) / 2.0 y_mid_side0 = (y_given[1] + y_given[2]) / 2.0 x_mid_side1 = (x_given[0] + x_given[2]) / 2.0 y_mid_side1 = (y_given[0] + y_given[2]) / 2.0 print("\nEvaluating Point 0 to Mid-Point of Side 0...") slope0, intercept0 = line_between_points([x_given[0], y_given[0]], [x_mid_side0, y_mid_side0]) print("\nEvaluating Point 1 to Mid-Point of Side 1...") slope1, intercept1 = line_between_points([x_given[1], y_given[1]], [x_mid_side1, y_mid_side1]) print() x, y = symbols('x,y') # Y = Slope*X + Intercept # Y - Slope*X = Intercept equation0 = Eq((y-slope0*x), intercept0) # print(equation0) equation1 = Eq((y-slope1*x), intercept1) # print(equation1) print("Solution for unknowns...") print(solve((equation0, equation1), (x, y)))