I am having to solve a problem from computational geometry which involves finding out if $2$ given line segments intersect.
More about the problem I am trying to solve over here if anyone is interested.
Now I keep coming across the following calculation:
(v1.X*v2.Y) - (v1.Y*v2.X)
It is suppose to be the vector cross product in 2D.
Now I have a couple of questions:
- What is the vector cross product in $2D$ ? Its not even defined there, so what is going on ?
- If the cross product is used so extensively why is it called as "mathematical hack" ? Everyone seems to be expected to know about it.
- How does it help when finding out if $2$ lines intersect or not ? What does it have to do with orientation like here.
I want to know how I can use this calculation to find out if $2$ lines intersect. What is the intuition behind it ?
How does it help ?
Thanks.
The "vector cross product in 2D" is neither a product (in the strict sense), nor a "vector", nor is it really in 2D. Other than that, it's well named. :(
The ordinary cross product takes a pair of nonzero vectors in 3-space, $u$ and $v$, and produces a new vector $w = u \times v$ whose length is the area of the parallelogram spanned by $u$ and $v$ (hence the length may be zero), and whose direction (if $w$ is nonzero) is perpendicular to $u$ and $v$, and has the property that $u, v, w$ (in that order) form a right-hand-oriented basis of 3-space.
If you have a pair of vectors $u$ adn $v$ in the plane, you can append a $0$ to each one, so if $u = (a, b)$, you get $U = (a, b, 0)$, and similarly for $v$. Then $W = U \times V$ is a vector in 3-space perpendicular to both (but possibly zero!). Well, perpendicular to both means that it must have the form $$ W = (0, 0, z) $$ and the expression you've written down is exactly the formula for that $z$ component. That means that if $u, v$ is a positively-oriented basis for the plane, then the "2D cross product" will be positive; if it's negatively oriented, the "product" will be negative. And if it's zero, then one of the vectors $u$ adn $v$ is a multiple of the other.
As for testing for intersection of segments, probably your best bet is to look for papers by Thomas Akenine-Moller, who has pretty much written the be-all and end-all of such papers. There's no reason to rehash the contents here.