A high school mathematics problem, the point P1 and the point P2 have formed a straight line, the perpendicular line of the known straight line is made through the point P3, and the coordinates of the intersection point of the perpendicular line and the known straight line are found.
p1 = [100, 15]
p2 = [16, 85]
p3 = [-50, 100]
if p2[0] - p1[0] == 0:
# p1 p2 构成垂线,那么垂直线就是一条水平线
x = p1[0]
y = p3[1]
elif p2[1] - p1[1] == 0:
# p1 p2 构成水平线,那么垂直线就是一条垂直线
x = p3[0]
y = p1[1]
else:
slope = (p2[1] - p1[1]) / (p2[0] - p1[0])
perpendicularSlope = -1 / slope
intercept = p3[1] - perpendicularSlope * p3[0]
x = (p3[1] - p1[1] + slope * p1[0] - perpendicularSlope * p3[0]) / (slope - perpendicularSlope)
y = slope * (x - p1[0]) + p1[1]
# 绘制两条直线和交点
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
plt.plot([p1[0], p2[0]], [p1[1], p2[1]], color='b')
plt.plot([p3[0], x], [p3[1], y], color='r')
plt.scatter([x], [y], color='r')
# 给P1,P2,P3点加注释
plt.annotate('P1', xy=(p1[0], p1[1]), xytext=(p1[0] + 0.5, p1[1] + 0.5))
plt.annotate('P2', xy=(p2[0], p2[1]), xytext=(p2[0] + 0.5, p2[1] + 0.5))
plt.annotate('P3', xy=(p3[0], p3[1]), xytext=(p3[0] + 0.5, p3[1] + 0.5))
# 给交点加注释
plt.annotate('JD', xy=(x, y), xytext=(x + 0.5, y + 0.5))
# 让坐标轴比例一样
plt.axis('equal')
plt.show()