/** * Transforms the figure. * * @param tx the transformation. */ public void transform(AffineTransform tx) { if (TRANSFORM.get(this) != null || tx.getType() != (tx.getType() & AffineTransform.TYPE_TRANSLATION)) { if (TRANSFORM.get(this) == null) { TRANSFORM.basicSet(this, (AffineTransform) tx.clone()); } else { AffineTransform t = TRANSFORM.getClone(this); t.preConcatenate(tx); TRANSFORM.basicSet(this, t); } } else { for (int i = 0; i < coordinates.length; i++) { tx.transform(coordinates[i], coordinates[i]); } if (FILL_GRADIENT.get(this) != null && !FILL_GRADIENT.get(this).isRelativeToFigureBounds()) { Gradient g = FILL_GRADIENT.getClone(this); g.transform(tx); FILL_GRADIENT.basicSet(this, g); } if (STROKE_GRADIENT.get(this) != null && !STROKE_GRADIENT.get(this).isRelativeToFigureBounds()) { Gradient g = STROKE_GRADIENT.getClone(this); g.transform(tx); STROKE_GRADIENT.basicSet(this, g); } } invalidate(); }
public AffineTransform getAffineTransform( int width, int height, double[] _xyzSW, double[] _xyzSE, double[] _xyzNW) { int[] cornerNW = projection.screenProjection(_xyzNW); int[] cornerSE = projection.screenProjection(_xyzSE); int[] cornerSW = projection.screenProjection(_xyzSW); double[] vectWE = { (double) cornerSE[0] - (double) cornerSW[0], (double) cornerSE[1] - (double) cornerSW[1] }; double normvectWE = sqrt(sqr(vectWE[0]) + sqr(vectWE[1])); double[] vectSN = { (double) cornerNW[0] - (double) cornerSW[0], (double) cornerNW[1] - (double) cornerSW[1] }; double normvectSN = sqrt(sqr(vectSN[0]) + sqr(vectSN[1])); double angleSW = acos((vectWE[0] * vectSN[0] + vectWE[1] * vectSN[1]) / (normvectWE * normvectSN)); if (angleSW == 0.0) { return null; } AffineTransform t = new AffineTransform(); t.translate(cornerNW[0], cornerNW[1]); t.scale(sign(vectWE[0]), -sign(vectSN[1])); t.rotate(-atan(vectSN[0] / vectSN[1])); t.shear(0, 1 / tan(PI - angleSW)); t.scale(normvectWE * cos(angleSW - PI / 2) / (double) width, normvectSN / (double) height); double[] _cornerSE_tr = new double[2]; double[] _cornerSE = {width, height}; t.transform(_cornerSE, 0, _cornerSE_tr, 0, 1); if (isDiff(_cornerSE_tr, cornerSE)) { double[] vectSE_NW_1 = { (double) cornerNW[0] - (double) cornerSE[0], (double) cornerNW[1] - (double) cornerSE[1] }; double[] vectSE_NW_2 = { (double) cornerNW[0] - (double) _cornerSE_tr[0], (double) cornerNW[1] - (double) _cornerSE_tr[1] }; double normvect_1 = sqrt(sqr(vectSE_NW_1[0]) + sqr(vectSE_NW_1[1])); double normvect_2 = sqrt(sqr(vectSE_NW_1[0]) + sqr(vectSE_NW_1[1])); double cos_angle = (((vectSE_NW_1[0] * vectSE_NW_2[0] + vectSE_NW_1[1] * vectSE_NW_2[1]) / (normvect_1 * normvect_2))); double vect = (vectSE_NW_1[0] * vectSE_NW_2[1] - vectSE_NW_1[1] * vectSE_NW_2[0]); AffineTransform t2 = new AffineTransform(); if (vect < 0) { t2.rotate(acos(cos_angle), cornerNW[0], cornerNW[1]); } else { t2.rotate(-acos(cos_angle), cornerNW[0], cornerNW[1]); } t.preConcatenate(t2); } // TODO patch for many cases... /*double[] _cornerSW_tr = new double[2]; double[] _cornerSW = { 0, img.getHeight(canvas) }; t.transform(_cornerSW, 0, _cornerSW_tr, 0, 1); if (isDiff(_cornerSW_tr, cornerSW)) { double[] vectSW_NW_1 = { (double) cornerNW[0] - (double) cornerSW[0], (double) cornerNW[1] - (double) cornerSW[1] }; double[] vectSW_NW_2 = { (double) cornerNW[0] - (double) _cornerSW_tr[0], (double) cornerNW[1] - (double) _cornerSW_tr[1] }; double normvect_1 = sqrt(sqr(vectSW_NW_1[0]) + sqr(vectSW_NW_1[1])); double normvect_2 = sqrt(sqr(vectSW_NW_1[0]) + sqr(vectSW_NW_1[1])); double cos_angle = (((vectSW_NW_1[0] * vectSW_NW_2[0] + vectSW_NW_1[1] * vectSW_NW_2[1]) / (normvect_1 * normvect_2))); double vect = (vectSW_NW_1[0] * vectSW_NW_2[1] - vectSW_NW_1[1] * vectSW_NW_2[0]); System.out.println(cos_angle + " " + vect + " -> " + toDegrees(acos(cos_angle))); //System.out.println(" "+vectSE_NW_1[0]+","+vectSE_NW_1[1]+" "+vectSE_NW_2[0]+","+vectSE_NW_2[1]); AffineTransform t2 = new AffineTransform(); if (vect > 0) t2.rotate(acos(cos_angle), cornerNW[0], cornerNW[1]); else t2.rotate(-acos(cos_angle), cornerNW[0], cornerNW[1]); t.preConcatenate(t2); }*/ return t; }