求直线交点 叉积
2017-04-04 23:26
225 查看
一般方程法:
直线的一般方程为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)/D
D = 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
[cpp] view
plain copy
#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;
}
直线的一般方程为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)/D
D = 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
[cpp] view
plain copy
#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;
}
相关文章推荐
- 求直线交点 叉积
- 两条直线的交点(叉积)
- POJ 1408 Fishnet(几何--叉积求面积 + 求直线的交点坐标)
- 求直线交点 叉积
- 算法之美——求两直线交点(三维叉积)——求四边形面积(二维叉积)
- 叉积--求两条直线(线段)的交点
- (计算几何step8.1.2.2)POJ 1269 Intersecting Lines(使用叉积来计算两条直线的交点)
- POJ 1269 Intersecting Lines 叉积(求直线交点)
- 算法之美——求两直线交点(三维叉积)——求四边形面积(二维叉积)
- poj 1408 Fishnet(计算几何 叉积求面积 求两直线交点 暴力)
- poj1039Pipe(直线交点、叉积)
- HDU 1466 计算直线的交点数(动态规划)
- 计蒜客NOIP模拟赛D2T2 直线的交点
- POJ 1269 Intresting Lines(直线之间的关系及求交点)
- POJ 2074(视线与障碍物+求直线和线段交点)
- [poj] 2074 Line of Sight || 直线相交求交点
- hdu 1466计算直线的交点数 (简单dp)
- HDU 1466 计算直线的交点数
- n 条直线给交点
- 计算两直线的交点 poj 1269 我的第一道几何题