root/trunk/AffineTransformFactory.java

Revision 3, 1.9 kB (checked in by dpaola2, 1 week ago)

old pathways project added

Line 
1 /**
2  * AffineTransformFactory
3  *
4  * It's possible to create an affine transform from three points and their
5  * desired transformed points, but AffineTransform doesn't know how to do it.
6  * So this class should suffice - it has a static method to create an
7  * affine transform from three points and their transformed points.
8  *
9  * This was created with the AffineTransform documentation and Mathematica,
10  * with find-n-replace and a few regexps.
11  *
12  * It is also possible to use more points to get a more accurate transform, but
13  * I have no idea how to do that, so we just use 3.
14  *
15  * This is probably quite inefficient, because neither me nor the Java compiler
16  * will cache the values of the oft-repeated terms.  Oh, well.
17  */
18
19 import java.awt.geom.*;
20
21 public class AffineTransformFactory {
22         public static AffineTransform createTransform(Point2D[] in, Point2D[] out) {
23                 double x1 = in[0].getX();
24                 double y1 = in[0].getY();
25                 double x2 = in[1].getX();
26                 double y2 = in[1].getY();
27                 double x3 = in[2].getX();
28                 double y3 = in[2].getY();
29                 double xp1 = out[0].getX();
30                 double yp1 = out[0].getY();
31                 double xp2 = out[1].getX();
32                 double yp2 = out[1].getY();
33                 double xp3 = out[2].getX();
34                 double yp3 = out[2].getY();
35
36 double m00=(-((xp2*y1-xp3*y1-xp1*y2+xp3*y2+xp1*y3-xp2*y3)/((-x2)*y1+x3*y1+x1*y2-x3*y2-x1*y3+x2*y3)));
37 double m01=(-(((-x2)*xp1+x3*xp1+x1*xp2-x3*xp2-x1*xp3+x2*xp3)/(x2*y1-x3*y1-x1*y2+x3*y2+x1*y3-x2*y3)));
38 double m02=(-(((-x3)*xp2*y1+x2*xp3*y1+x3*xp1*y2-x1*xp3*y2-x2*xp1*y3+x1*xp2*y3)/((-x2)*y1+x3*y1+x1*y2-x3*y2-x1*y3+x2*y3)));
39 double m10=(-((y2*yp1-y3*yp1-y1*yp2+y3*yp2+y1*yp3-y2*yp3)/(x2*y1-x3*y1-x1*y2+x3*y2+x1*y3-x2*y3)));
40 double m11=(-(((-x2)*yp1+x3*yp1+x1*yp2-x3*yp2-x1*yp3+x2*yp3)/(x2*y1-x3*y1-x1*y2+x3*y2+x1*y3-x2*y3)));
41 double m12=(-(((-x3)*y2*yp1+x2*y3*yp1+x3*y1*yp2-x1*y3*yp2-x2*y1*yp3+x1*y2*yp3)/(x2*y1-x3*y1-x1*y2+x3*y2+x1*y3-x2*y3)));
42
43                 return new AffineTransform(m00, m10, m01, m11, m02, m12);
44         }
45 }
Note: See TracBrowser for help on using the browser.