How do you calculate the point where two lines in the plane intersect? It is not very hard to do, but the formula can look quite complicated, depending on how you write it up. This article is a reminder that it can be expressed in a simple manner.
We start out by not restricting ourselves to the plane, but any vector space with an inner product \langle \cdot, \cdot \rangle. Let two lines be represented as
and
where \hbox{p}, \hbox{q}, \hbox{v}, and \hbox{w} are vectors. We assume that both \hbox{v} and \hbox{w} are non-null. See Figure 1.
We look for values of s and t such that
Let \hat{\hbox{w}} \neq 0 be a vector perpendicular to \hbox{w}, \langle \hbox{w}, \hat{\hbox{w}} \rangle = 0. We get
Now if \langle \hbox{v}, \hat{\hbox{w}} \rangle = 0 there are two possibilities: If \langle \hbox{q} – \hbox{p}, \hat{\hbox{w}} \rangle = 0 there are infinitely many solutions, i.e., the lines overlap, but if \langle \hbox{q} – \hbox{p}, \hat{\hbox{w}} \rangle \neq 0 there are no solutions, i.e., the lines are parallel and do not intersect.
Assume then \langle \hbox{v}, \hat{\hbox{w}} \rangle \neq 0. We get
and thus, after inserting into (1), the point of intersection is
The Plane is Special
The derivation above is actually a little careless. If (2) is to hold for some s and t, then (3) must also hold. Turning the implication the other way, which we would like to, is less straightforward.
Assume that (3) holds for some value of s,
What does this mean? It means that the vectors s \hbox{v} + \hbox{p} – \hbox{q} and \hat{\hbox{w}} are perpendicular to each other, and if we are in two dimensions/the plane we must have s \hbox{v} + \hbox{p} – \hbox{q} = t \hbox{w} for some value of t. This is (2) and we are done.
Does this work in higher dimensions? Generally, no. Consider, e.g., three dimensions and Equation (4). What can we derive of it now? We have that
for some values of \alpha and \beta and where \langle \hbox{w}_1, \hat{\hbox{w}} \rangle = \langle \hbox{w}_2, \hat{\hbox{w}} \rangle = 0. And \alpha \hbox{w}_1 + \beta \hbox{w}_2 = t \hbox{w} does not necessarily hold for any t, so (2) does generally not follow in three dimensions or more.
Summary Using Coordinates
Let us consider the usual two-dimensional Euclidean space and Cartesian coordinates. We set
\hbox{v} = (v_1,v_2), \quad \hbox{w} = (w_1,w_2),
and use the inner product
Rotating a vector \hbox{w} = (w_1,w_2) counterclockwise by a right angle is easily done with \hat{\hbox{w}} = (-w_2, w_1). It is easily checked that \langle \hbox{w}, \hat{\hbox{w}} \rangle = 0.
Recall that we are interested in knowing whether the two lines
and
intersect.
Setting \alpha = w_1 v_2 – w_2 v_1 and \beta = w_1 (q_2-p_2) – w_2 (q_1-p_1), we have
- \alpha = 0, \beta = 0: The two lines overlap.
- \alpha = 0, \beta \neq 0: The lines are parallel but do not intersect.
- \alpha \neq 0: The lines meet at a single point, (p_1 + \frac{\beta}{\alpha} v_1, p_2 + \frac{\beta}{\alpha} v_2).



Could you help me out please? This is a sequel to a game i wrote 7 or so years ago called “Force Disruptor”. Well, i
guess a forgot how to write code for this. This has been bothering me for close to eternity. I know the codes problem is
somewhere in here, but i can’t figure it out! Could you do me a huge favor and look at it?
The error is somewhere in here:
void fixCollisions()
{
for (int i = 1; i<wall_ubound;i++)
{
if (lineSegmentIntersection(
FDX-cos(FDA),FDY-sin(FDA),
FDX+cos(FDA),FDY+sin(FDA),
wallX[i]-cos(wallRot[i]),wallY[i]-sin(wallRot[i]),
wallX[i]+cos(wallRot[i]),wallY[i]+sin(wallRot[i]),
&FDX,
&FDY))
bigX=true; // /\ There is something weird going on here.
}
} // Shucks!
//————————————————————-
bool lineSegmentIntersection(
double Ax, double Ay,
double Bx, double By,
double Cx, double Cy,
double Dx, double Dy,
double *X, double *Y) {
double distAB, theCos, theSin, newX, ABpos ;
// Fail if either line segment is zero-length.
if (Ax==Bx && Ay==By || Cx==Dx && Cy==Dy) return false;
// Fail if the segments share an end-point.
if (Ax==Cx && Ay==Cy || Bx==Cx && By==Cy
|| Ax==Dx && Ay==Dy || Bx==Dx && By==Dy) {
return false; }
// (1) Translate the system so that point A is on the origin.
Bx-=Ax; By-=Ay;
Cx-=Ax; Cy-=Ay;
Dx-=Ax; Dy-=Ay;
// Discover the length of segment A-B.
distAB=sqrt(Bx*Bx+By*By);
// (2) Rotate the system so that point B is on the positive X axis.
theCos=Bx/distAB;
theSin=By/distAB;
newX=Cx*theCos+Cy*theSin;
Cy =Cy*theCos-Cx*theSin; Cx=newX;
newX=Dx*theCos+Dy*theSin;
Dy =Dy*theCos-Dx*theSin; Dx=newX;
// Fail if segment C-D doesn't cross line A-B.
if (Cy<0. && Dy=0. && Dy>=0.) return false;
// (3) Discover the position of the intersection point along line A-B.
ABpos=Dx+(Cx-Dx)*Dy/(Dy-Cy);
// Fail if segment C-D crosses line A-B outside of segment A-B.
if (ABposdistAB) return false;
// (4) Apply the discovered position to line A-B in the original coordinate system.
*X=Ax+ABpos*theCos;
*Y=Ay+ABpos*theSin;
return true;
}
Thanks for the help, i really appreciate it.
| 2010-06-17 @ 23:14