Game Development Reference
In-Depth Information
The distance to an arbitrary point q is given by
The first part (p-q) is a vector from the point q to the starting point of the line p , we then
usethistowiththedotproductof (p-q)•v iswhichistheprojection ofthelengthoftheline
from the point onto v .
Figure 25 - Distance from a point to a line.
The distance from the point to the line is the length of the resulting vector.
float PointToLineDistance(const point& a, const point& b, const point& p)
{
vector2 v = b - a;
vector2 n = v.Normalize();
vector2 m = a - p;
vector2 r = m - (m * m.Dot(n));
return r.Length();
}
1.4.1.3Ray
A ray, unlike a line is not represented by two points in space but rather by a single point
and a direction. The ray may extend infinitely and is often used when we are interested in
finding any objects that may be down the path of the ray.
Rays are important in UI programming, they provide the mechanism by which the player
interacts with a 3D world. When the player performs an action on a 2D display, we calcu-
Search WWH ::




Custom Search