1 26 package org.nightlabs.editor2d.util; 27 28 import java.awt.Rectangle ; 29 import java.awt.geom.AffineTransform ; 30 31 import org.apache.log4j.Logger; 32 33 39 public class GeomUtil 40 { 41 public static final Logger LOGGER = Logger.getLogger(GeomUtil.class); 42 43 public GeomUtil() { 44 super(); 45 } 46 47 private static AffineTransform at = new AffineTransform (); 48 49 66 public static AffineTransform getAffineTransform(int x1, int y1, int w1, int h1, 67 int x2, int y2, int w2, int h2) 68 { 69 if (x1 == x2 && y1 == y2 && w1 == w2 && h1 == h2) 71 { 72 at.setToIdentity(); 74 return at; 75 } 76 77 if (w1 == w2 && h1 == h2) 79 { 80 at.setToIdentity(); 82 at.translate(x2 - x1, y2 - y1); 83 return at; 84 } 85 else if (x1 == x2 && y1 == y2) 86 { 87 at.setToIdentity(); 89 float ratioY = (float)h2 / (float)h1; 90 float ratioX = (float)w2 / (float)w1; 91 float distanceX = (float)x1 - ((float)x1*ratioX); 92 float distanceY = (float)y1 - ((float)y1*ratioY); 93 at.translate(distanceX, distanceY); 94 at.scale(ratioX, ratioY); 95 return at; 96 } 97 else 98 { 99 double ratioX = ((double)w2) / ((double)w1); 102 double ratioY = ((double)h2) / ((double)h1); 103 double x = (double)x1; 104 double y = (double)y1; 105 double distanceX = x - (x*ratioX); 106 double distanceY = y - (y*ratioY); 107 at.setToIdentity(); 108 at.translate(distanceX, distanceY); 109 at.scale(ratioX, ratioY); 110 111 AffineTransform at2 = new AffineTransform (); 113 distanceX = x2 - x1; 114 distanceY = y2 - y1; 115 at2.translate(distanceX, distanceY); 116 117 at.preConcatenate(at2); 118 } 119 return at; 120 } 121 122 131 public static AffineTransform getAffineTransform(Rectangle oldBounds, Rectangle newBounds) 132 { 133 return getAffineTransform(oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height, 134 newBounds.x, newBounds.y, newBounds.width, newBounds.height); 135 } 136 } 137 | Popular Tags |