求两直线交点坐标

使用面积法与矢量叉积即可。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

struct Point
{
	double x,y;
	
	Point() {}
	Point(double X,double Y) :x(X),y(Y) {}
	Point operator + (const Point a)const { return Point(a.x+x,a.y+y); }
	Point operator - (const Point a)const { return Point(x-a.x,y-a.y); }
	Point operator * (const double a)const { return Point(x*a,y*a); }
	double operator * (const Point a)const { return x*a.y-y*a.x; }
	void read() { scanf("%lf %lf",&x,&y); }
	void print() { printf("%lf %lf\n",x,y); }
}A,B,C,D;

Point cross(Point p1,Point v1,Point p2,Point v2)
{
	double t=((p2-p1)*v2)/(v1*v2);
	return p1+v1*t;
}

int main()
{
	A.read(),B.read(),C.read(),D.read();
	cross(A,B-A,C,D-C).print();
	return 0;
}
posted @ 2020-07-03 09:32  With_penguin  阅读(550)  评论(0编辑  收藏  举报