问题
I have two points that I want to connect in a specific way - see the first picture. I know the coordinates of all four points. I then need to move the whole shape to coordinates [0, 0] and rotate it, so the main two points are both on x-axis (see the second picture). Next I need to "squeeze" the shape on the x-axis only so that the last point has coordinates [0, 1] (see the last picture).

My question is - how do I compute the coordinates of the middle two points effectively in Java without getting into manual analytical maths?
回答1:
java.awt.geom.AffineTransform may be a startup point.
This program apply the 3 transformations (in reverse order):
import java.awt.geom.AffineTransform;
public class TransRotScal {
public static void main( String[] args ) {
double theta = Math.atan2( -15.0, 40.0 );
AffineTransform trans = new AffineTransform(); // Identity
trans.scale( 1/43.0, 1.0 );
trans.rotate( theta );
trans.translate( -10, -20 );
double[] in = {
10, 20,
10, 30,
50, 30,
50, 35
};
double[] out = new double[in.length];
trans.transform( in, 0, out, 0, in.length/2 );
for( int ptNdx = 0; ptNdx < out.length; ptNdx += 2 ) {
System.out.printf( "{%7.4f, %7.4f }\n", out[ptNdx], out[ptNdx+1]);
}
}
}
Outputs:
{ 0,0000, 0,0000 }
{ 0,0817, 9,3633 }
{ 0,9527, -4,6816 }
{ 0,9935, 0,0000 }
To give this answer, I've wrote a little program, but I wish a simpler one, and to do that I'm have post this question : Affine transforms for graph, not for text and labels.
来源:https://stackoverflow.com/questions/15734388/transforming-a-shape