Game Development Reference
return (count==4 || count==-4);
Another useful 2D technique is finding the nearest point on a line
segment to an arbitrary point.
The two triangles AP
P and BP
P define the position of the point P
the line segment AB. A simple technique to calculate the position of P
to use the old favourite the dot product. If we are trying to get the angle at
A then we can use two vectors. First, the line segment AB and, second,
the vector AP. If we set these two vectors to unit length then the dot
• AP returns the cosine of the angle between them. If this
value is negative then the angle is greater than 90° and so the nearest
point to the line segment AB is the point A itself. Similarly, at B, if the
returned cosine is negative then the point P must be to the right of the line
Figure 13.8 Finding the nearest point on a line segment to an arbitrary point.