您的位置:首页 > 其它

求直线交点 叉积

2012-05-20 13:00 176 查看
一般方程法:直线的一般方程为F(x) = ax + by + c = 0。既然我们已经知道直线的两个点,假设为(x0,y0), (x1, y1),那么可以得到a = y0 – y1, b = x1 – x0, c = x0y1 – x1y0。因此我们可以将两条直线分别表示为F0(x) = a0*x + b0*y + c0 = 0, F1(x) = a1*x + b1*y + c1 = 0那么两条直线的交点应该满足a0*x + b0*y +c0 = a1*x + b1*y + c1由此可推出x = (b0*c1 – b1*c0)/D y = (a1*c0 – a0*c1)/DD = a0*b1 – a1*b0, (D为0时,表示两直线平行)二者实际上就是连立方程组F0(x) = a0*x + b0*y + c0 = 0, F1(x) = a1*x + b1*y + c1 = 0的叉积应用
i j k
a0 b0 c0
a1 b1 c1

#include"iostream"
#include"stdio.h"
#include"math.h"
using namespace std;

struct Point
{
	double x;
	double y;
};

struct Line
{
	Point p1,p2;
	double a,b,c;
};

void GetLinePara(Line *l)
{
	l->a=l->p1.y-l->p2.y;
	l->b=l->p2.x-l->p1.x;
	l->c=l->p1.x*l->p2.y-l->p2.x*l->p1.y;
}

Point GetCrossPoint(Line *l1,Line *l2)
{
	GetLinePara(l1);
	GetLinePara(l2);
	double D=l1->a*l2->b-l2->a*l1->b;
	Point p;
	p.x=(l1->b*l2->c-l2->b*l1->c)/D;
	p.y=(l1->c*l2->a-l2->c*l1->a)/D;
	return p;
}

int main()
{
	Line l1,l2;
	while(true)
	{
		scanf("%lf%lf%lf%lf",&l1.p1.x,&l1.p1.y,&l1.p2.x,&l1.p2.y);
		scanf("%lf%lf%lf%lf",&l2.p1.x,&l2.p1.y,&l2.p2.x,&l2.p2.y);
		Point Pc=GetCrossPoint(&l1,&l2);
		printf("Cross point:%lf %lf\n",Pc.x,Pc.y);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: