Graphics Reference
In-Depth Information
PointAndVectorsToPointAndVectors
method for
AffineTransformation2
; once we'd done this, the
PointsToPoints
method was straightforward:
1
2
3
4
5
6
7
8
9
10
public static AffineTransform2 PointsToPoints(
Point p1, Point p2, Point p3,
Point q1, Point q2, Point q3)
{
Vector v1 = p2 - p1;
Vector v2 = p3 - p1;
Vector w1 = q2 - q1;
Vector w2 = q3 - q1;
return AffineTransform2.PointAndVectorsToPointAndVectors(p1, v1, v2, q1, w1, w2);
}
The
PointAndVectorsToPointAndVectors
code is implemented in a relatively
straightforward way: We know that the vectors
v1
and
v2
must be sent to the
vectors
w1
and
w2
; in terms of the 3
×
3 matrix, that means that the upper-left
corner must be the 2
2 matrix that effects this transformation of 2-space. So we
invoke the
LinearTransformation2
method
VectorsToVectors
to produce the
transformation. Unfortunately, the resultant transformation does not send
p1
to
q1
in general. To ensure this, we precede this vector transform with a translation that
takes
p1
to the origin (the translation has no effect on vectors, of course); we then
perform the linear transformation; we then follow this with a transformation that
takes the origin to
q1
. The net result is that
p1
is sent to
q1
, and the vectors are
transformed as required.
This approach relies on the
VectorsToVectors
method for
LinearTrans
-
formation2
. Writing that is straightforward: We place the vectors
v1
and
v2
in
the first two columns of a 3
×
3 matrix, with a 1 in the lower right. This trans-
formation
T
sends
e
1
to
v
1
and
e
2
to
v
2
. Similarly, we can use the
w
's to build a
transformation
S
that sends
e
1
to
w
1
and
e
2
to
w
2
. The composition
T
×
S
−
1
sends
◦
v
1
to
e
1
to
w
1
, and similarly for
v
2
, and hence solves our problem.
The only really subtle implementation problem is the
PointsToPoints
method for
ProjectiveTransformation2
. Explaining this code requires a bit of mathematics,
but it's all mathematics that we've seen before in various forms.
We're given four points
P
1
,
P
2
,
P
3
, and
P
4
in the Euclidean plane, and we are
to find a projective transformation that sends them to the four points
Q
1
,
Q
2
,
Q
3
,
and
Q
4
of the Euclidean plane.
Before we go any further, we should mention a limitation. When we described
the
VectorsToVectors
method of
LinearTransformation2
, we promised to send
v
1
and
v
2
to
w
1
and
w
2
, but there was, in fact, a constraint. If
v
1
=
0
and
w
1
=
0
,
there's no linear transformation that accomplishes this. In fact, if
v
1
is a multiple
of
v
2
, in general there's no linear transformation that solves the problem (except
in the very special case where
w
1
is the same multiple of
w
2
, in which case there
are an infinite number of solutions). The implicit constraint was that
v
1
and
v
2
must be
linearly independent
for our solution to work (or for the general problem
to have a solution). In the case of
PointsToPoints
, we require something similar:
The points
P
i
(
i
=
1,
...
,4
)
must be in
general position,
which means that (a) no