Line-Line Intersection线段交点

43 篇文章 0 订阅

One of the most common tasks you will find in geometry problems is line intersection. Despite the fact that it is so common, a lot of coders still have trouble with it. The first question is, what form are we given our lines in, and what form would we like them in? Ideally, each of our lines will be in the form Ax+By=C, where A, B and C are the numbers which define the line. However, we are rarely given lines in this format, but we can easily generate such an equation from two points. Say we are given two different points, (x1, y1) and (x2, y2), and want to find A, B and C for the equation above. We can do so by setting

A = y2-y1
B = x1-x2
C = Ax1+By1

Regardless of how the lines are specified, you should be able to generate two different points along the line, and then generate A, B and C. Now, lets say
that you have lines, given by the equations:

A1x + B1y = C1
A2x + B2y = C2

To find the point at which the two lines intersect, we simply need to solve the two equations for the two unknowns, x and y.

 


double det = A1*B2 - A2*B1
if(det == 0){
//Lines are parallel
}else{
double x = (B2*C1 - B1*C2)/det
double y = (A1*C2 - A2*C1)/det
}

To see where this comes from, consider multiplying the top equation by B2, and the bottom equation by B1. This gives you

A1B2x + B1B2y = B2C1

A2B1x + B1B2y = B1C2

Now, subtract the bottom equation from the top equation to get

A1B2x - A2B1x = B2C1 - B1C2
 

Finally, divide both sides by A1B2 - A2B1, and you get the equation for x. The equation for y can be derived similarly.

This gives you the location of the intersection of two lines, but what if you have line segments, not lines. In this case, you need to make sure that the point you found is on both of the line segments. If your line segment goes from (x1,y1) to (x2,y2), then to check if (x,y) is on that segment, you just need to check that min(x1,x2) ≤ x ≤ max(x1,x2), and do the same thing for y. You must be careful about double precision issues though. If your point is right on the edge of the segment, or if the segment is horizontal or vertical, a simple comparison might be problematic. In these cases, you can either do your comparisons with some tolerance, or else use a fraction class.

https://www.topcoder.com/community/data-science/data-science-tutorials/geometry-concepts-line-intersection-and-its-applications/

 

C# Unity3D

public static vector3 LineIntersection(Vector3 l1p1,Vector3 l1p2,Vector3 l2p1,Vector3 l2p2){

float t1=0,t2=0;

vector3 A=l1p2-l1p1;

vector3 B=l2p2-l2p1;

vector3 C=l2p1-l1p1;

vector pp1=l1p1+t1*A;

vector pp2=l2p1+t2*B;

/*

t1*A.x-t2*B.x=C.x;

t1*A.z-t2*B.z=C.z;

mA=|A.x   -B.x|

        |A.z  -B.z|

m_T=|t1|

         |t2|

m_C=|C.x|

          |C.z|

*/

float det=B.x*A.z-A.z*B.z;

if(det==0)

return vector.zero;

t1=(B.x*C.z-B.z*C.x)/det;

return lip1+t1*A;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值