Solving Triangle Medians and Cevians with Python
If you remember some advanced math problems with triangles, where you have some cevians and medians, that split a triangle’s side into a 4:3 ration and you have to find the ratio of the split of the two cevian itself, then you are in the correct place. The below picture should remind you of something:

Solving that kind of typical problem is not that difficult, once you have solved about 10 like it. Then, you would probably start seeing the “magic” of the basis vectors. “Translating” the problem into python is interesting. The code below works and is the one used in the video:
from sympy import symbols, Eq, solve import matplotlib.pyplot as plt import numpy as np
# λ and μ
lambda1, mi1 = symbols('lambda mi')
# Define:
# AG = λ * AA1, where AA1 = 1/2a + 1/2b
# CG = μ * CC1, where CC1 = -b + 2/5a
# Intersection -> b + μ(-b + 2/5a) = λ(1/2a + 1/2b)
# Coefficient comparison for 'a' and 'b':
eq1 = Eq((2/5) * mi1 - (1/2) * lambda1, 0)
eq2 = Eq(1 - mi1 - (1/2) * lambda1, 0)
# Solve the system:
solution = solve((eq1, eq2), (lambda1, mi1))
# Solutions for λ and μ:
lambda1_value = round(float(solution[lambda1]),2) # λ represents AG/AA1
mi1_value = round(float(solution[mi1]),2) # μ represents CG/CC1
print(f'AG:AA1 is {lambda1_value} (λ).')
print(f'CG:CC1 is {mi1_value} (μ).')
# Define the points A, B, C in a coordinate plane
A = np.array([0, 0]) # Origin
B = np.array([1, 0]) # AB - basis vector a
C = np.array([0, 1]) # AC - basis vector b
# Parameters t and s
t, s = symbols('t s')
# Calculate A1 (ratio 1:1)
A1 = (B + C) / 2
# Calculate C1 (ratio 2:3)
C1 = (3 * A + 2 * B) / 5
# Equations for the intersection
eq1 = Eq(t * A1[0], (1 - s) * C[0] + s * C1[0])
eq2 = Eq(t * A1[1], (1 - s) * C[1] + s * C1[1])
solution = solve((eq1, eq2), (t, s))
# Extract the solution for G
t_val = float(solution[t])
G = t_val * A1
# Plot the triangle and the key lines
plt.figure(figsize=(8, 8))
# Plot the triangle sides
plt.plot([A[0], B[0]], [A[1], B[1]], 'k-', label='AB')
plt.plot([A[0], C[0]], [A[1], C[1]], 'k-', label='AC')
plt.plot([B[0], C[0]], [B[1], C[1]], 'k-', label='BC')
# Plot the medians and cevians
plt.plot([A[0], A1[0]], [A[1], A1[1]], 'b--', label='AA1 (Median)')
plt.plot([C[0], C1[0]], [C[1], C1[1]], 'g--', label='CC1 (Cevian)')
# Highlight points
plt.scatter(*A, color='black', label='A (0, 0)')
plt.scatter(*B, color='black', label='B (1, 0)')
plt.scatter(*C, color='black', label='C (0, 1)')
plt.scatter(*A1, color='blue', label='A1 (Midpoint of BC)')
plt.scatter(*C1, color='green', label='C1 (2:3 on AB)')
plt.scatter(*G, color='red', label='G (Intersection of AA1 and CC1)')
# Annotate points
plt.text(A[0] - 0.05, A[1] - 0.05, 'A', fontsize=12)
plt.text(B[0] + 0.05, B[1] - 0.05, 'B', fontsize=12)
plt.text(C[0] - 0.05, C[1] + 0.05, 'C', fontsize=12)
plt.text(A1[0] + 0.05, A1[1], 'A1', fontsize=12)
plt.text(C1[0] + 0.05, C1[1], 'C1', fontsize=12)
plt.text(G[0] + 0.05, G[1] + 0.05, 'G', fontsize=12)
# Grid, legend, and title
plt.grid(color='gray', linestyle='--', linewidth=0.5)
plt.legend()
plt.title('Triangle ABC with Medians and Cevians')
plt.axis('equal')
plt.show()

The YouTube video:
- The paper solution from the video – 20241211_cevians_and_medians
Enjoy it! 🙂