Graphics Reference
In-Depth Information
int IntersectPlanes(Plane p1, Plane p2, Point &p, Vector &d)
{
// Compute direction of intersection line
d = Cross(p1.n, p2.n);
// If d is zero, the planes are parallel (and separated)
// or coincident, so they're not considered intersecting
if (Dot(d, d) < EPSILON) return 0;
float d11 = Dot(p1.n, p1.n);
float d12 = Dot(p1.n, p2.n);
float d22 = Dot(p2.n, p2.n);
float denom = d11*d22 - d12*d12;
float k1 = (p1.d*d22 - p2.d*d12) / denom;
float k2 = (p2.d*d11 - p1.d*d12) / denom;
p = k1*p1.n + k2*p2.n;
return 1;
}
This code can be optimized by realizing that the denominator expression is really
just the result of applying Lagrange's identity to (
n
1
×
n
2
)
·
(
n
1
×
n
2
). The denominator
is therefore equivalent to
denom
=
d
·
d
.
Using the vector identity
u
×
(
v
×
w
)
=
(
u
·
w
)
v
−
(
v
·
w
)
u
,
the expression for
P
=
k
1
n
1
+
k
2
n
2
can also be further simplified as follows.
P
=
k
1
n
1
+
k
2
n
2
⇔
(original expression)
P
=[
(
d
1
(
n
2
·
n
2
)
−
d
2
(
n
1
·
n
2
))
n
1
+
(
d
2
(
n
1
·
n
1
)
−
d
1
(
n
1
·
n
2
))
n
2
⇔
(substituting k
1
and k
2
)
]
/
denom
P denom
=
(
d
1
(
n
2
·
n
2
)
−
d
2
(
n
1
·
n
2
))
n
1
+
(
d
2
(
n
1
·
n
1
)
⇔
(multiplying both sides by denom)
−
d
1
(
n
1
·
n
2
))
n
2
P denom
=
(
d
1
(
n
2
·
n
2
)
n
1
−
d
2
(
n
1
·
n
2
)
n
1
)
+
(
d
2
(
n
1
·
n
1
)
n
2
⇔
(distributing scalar multiply)
−
d
1
(
n
1
·
n
2
)
n
2
)