Given the 4 points describing 2 line segments, how do you calculate if line A is towards or away from line B?
The 2 lines have a fixed length, and can be measured as distance from x1/y1 to x2/y2.
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.Sign up to join this community
B be two points on the black line. Let
D be your blue segment. The sign of the
z coordinate of cross product
AB^AC tells you whether
C is "left" or "right" of the black line. Similarly, cross product
AB^CD tells you whether
CD steers "left" or "right" of the black line.
We don't really want to know whether it's left or right; all we want is to make sure they're the same direction or the opposite direction, that's why we multiply the two values.
The following pseudocode should therefore work:
z1 = (xB-xA)*(yC-yA) - (yB-yA)*(xC-xA); z2 = (xB-xA)*(yD-yC) - (yB-yA)*(xD-xC); z3 = z1 * z2; if (z3 < 0) ; /* Pointing towards (BUT maybe even crossing) */ else if (z3 > 0 || z2 != 0) ; /* Pointing away */ else ; /* Parallel */
I am afraid I need some time to write a proper solution for the Bezier curve. Is the following situation towards or away?
Assuming start point is the green circle, and end point is the red arrow
Compute the distance between the start point as DS and the black segment, and do the same for the end point (red arrow) as DE. If DS>DE, then the segment is pointing toward. if DE>DS, it is pointing away. If both are equal, the two are parallels.
You can find how to compute the distance from a point to a segment here, and to a quadratic bezier curve here. However, depending of the shape of the bezier curve, it might return weird results (the curve can be crossing itself)