不喜欢只给程序,给出链接网页链接,主要讲述一下两点确认直线,点到直线距离,两条直线的交点等问题的解决方法,并给出python程序。之前的回答太复杂,方法选的好,求交点不需要判断太多内容。网页链接
```python
def GeneralEquation(first_x,first_y,second_x,second_y):
# 一般式 Ax+By+C=0
# from http://www.cnblogs.com/DHUtoBUAA/
A=second_y-first_y
B=first_x-second_x
C=second_x*first_y-first_x*second_y
return A,B,C
```
```python
def GetIntersectPointofLines(x1,y1,x2,y2,x3,y3,x4,y4):
# from http://www.cnblogs.com/DHUtoBUAA/
A1,B1,C1=GeneralEquation(x1,y1,x2,y2)
A2, B2, C2 = GeneralEquation(x3,y3,x4,y4)
m=A1*B2-A2*B1
if m==0:
print("平行,无交点")
else:
x=(C2*B1-C1*B2)/m
y=(C1*A2-C2*A1)/m
return x,y
```
#include
int main()
{
double x,y,x0,y0,x1,y1,x2,y2,x3,y3,k1,k2;
scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&x0,&y0,&x1,&y1,&x2,&y2,&x3,&y3);
k1=(y0-y1)/(x0-x1);
k2=(y2-y3)/(x2-x3);
x=(k1*x0-k2*x2+y2-y0)/(k1-k2);
y=y0+(x-x0)*k1;
printf("%lf %lf\n",x,y);
return 0;
}
我测试了下,对的,你看看