Graphics Reference
In-Depth Information
This is a quadratic equation, so we can write down the closed-form solution. Re-
call that for any as 2
+
bs
+
c
=
0,
± b 2
=
b
4 ac
s
.
2 a
Because there is a square root, there may be no real solution (if the number under
the is negative), which occurs when the line misses the circle/sphere. If the
square root term is zero, there is one solution, which means the line grazes the
circle/sphere and thus hits it at exactly one point. If the square root is real, the
±
means there are two real solutions, which correspond to the two points where the
line hits the circle/sphere. If the value of s is between zero and one for either or
both of the solutions, then the line segment between A and B hits the circle/sphere.
B.3.5
More Small Examples
Here are more simple examples of the power of vector math. Remember that all
of these operations should look very similar in code once you have dot, vector
add, and so on as functions and operators.
2D distance of a point and a line. Given a line through points C and D and a
point P , we can compute the signed distance between the point and the line via
the implicit equation
(
P
C
) ·
N
=
0
.
For a point Q , the distance is
=
Q
C
·
N
.
distance
N
If it is on one side of the line, the distance will be positive, and on the other
negative.
Projection of a point onto a plane.
After finding the signed distance d to the
plane, translate the point along N :
N
Q plane =
Q
d
.
N
Reflection direction. Given a vector E in the direction of the viewer, and a unit
normal vector N , the reflection vector R is
N
N
R
=
E
+
2
(
E
·
)
.
Search WWH ::




Custom Search