# Coverage details for edu.uci.ics.jung.visualization.transform.AffineTransformer

Line | Hits | Source
--- | --- | ---
1 |  | `/*`
2 |  | `* Copyright (c) 2005, the JUNG Project and the Regents of the University of`
3 |  | `* California All rights reserved.`
4 |  | `*`
5 |  | `* This software is open-source under the BSD license; see either "license.txt"`
6 |  | `* or http://jung.sourceforge.net/license.txt for a description.`
7 |  | `*`
8 |  | `* Created on Apr 16, 2005`
9 |  | `*/`
10 |  | ``
11 |  | `package edu.uci.ics.jung.visualization.transform;`
12 |  | ``
13 |  | `import java.awt.Shape;`
14 |  | `import java.awt.geom.AffineTransform;`
15 |  | `import java.awt.geom.GeneralPath;`
16 |  | `import java.awt.geom.NoninvertibleTransformException;`
17 |  | `import java.awt.geom.PathIterator;`
18 |  | `import java.awt.geom.Point2D;`
19 |  | ``
20 |  | `import edu.uci.ics.jung.visualization.transform.shape.ShapeTransformer;`
21 |  | ``
22 |  | `/**`
23 |  | `*`
24 |  | `* Provides methods to map points from one coordinate system to`
25 |  | `* another, by delegating to a wrapped AffineTransform (uniform)`
26 |  | `* and its inverse.`
27 |  | `*`
28 |  | `* @author Tom Nelson - RABA Technologies`
29 |  | `*/`
30 |  | `public class AffineTransformer implements Transformer, ShapeTransformer {`
31 |  | ``
32 |  | `protected AffineTransform inverse;`
33 |  | ``
34 |  | `/**`
35 |  | `* the AffineTransform to use. Initialize to identity`
36 |  | `*`
37 |  | `*/`
38 | 0 | `protected AffineTransform transform = new AffineTransform();`
39 |  | ``
40 |  | `/**`
41 |  | `* create an instance that does not transform points`
42 |  | `*`
43 |  | `*/`
44 | 0 | `public AffineTransformer() {`
45 |  | `// nothing left to do`
46 | 0 | `}`
47 |  | `/**`
48 |  | `* Create an instance with the supplied transform`
49 |  | `*/`
50 | 0 | `public AffineTransformer(AffineTransform transform) {`
51 | 0 | `if(transform != null)`
52 | 0 | `this.transform = transform;`
53 | 0 | `}`
54 |  | ``
55 |  | `/**`
56 |  | `* @return Returns the transform.`
57 |  | `*/`
58 |  | `public AffineTransform getTransform() {`
59 | 0 | `return transform;`
60 |  | `}`
61 |  | `/**`
62 |  | `* @param transform The transform to set.`
63 |  | `*/`
64 |  | `public void setTransform(AffineTransform transform) {`
65 | 0 | `this.transform = transform;`
66 | 0 | `}`
67 |  | ``
68 |  | `/**`
69 |  | `* applies the inverse transform to the supplied point`
70 |  | `* @param p`
71 |  | `* @return`
72 |  | `*/`
73 |  | `public Point2D inverseTransform(Point2D p) {`
74 |  | ``
75 | 0 | `return getInverse().transform(p, null);`
76 |  | `}`
77 |  | ``
78 |  | `public AffineTransform getInverse() {`
79 | 0 | `if(inverse == null) {`
80 |  | `try {`
81 | 0 | `inverse = transform.createInverse();`
82 | 0 | `} catch (NoninvertibleTransformException e) {`
83 | 0 | `e.printStackTrace();`
84 | 0 | `}`
85 |  | `}`
86 | 0 | `return inverse;`
87 |  | `}`
88 |  | ``
89 |  | `/**`
90 |  | `* getter for scalex`
91 |  | `*/`
92 |  | `public double getScaleX() {`
93 | 0 | `return transform.getScaleX();`
94 |  | `}`
95 |  | ``
96 |  | `/**`
97 |  | `* getter for scaley`
98 |  | `*/`
99 |  | `public double getScaleY() {`
100 | 0 | `return transform.getScaleY();`
101 |  | `}`
102 |  | ``
103 |  | `public double getScale() {`
104 | 0 | `return Math.sqrt(transform.getDeterminant());`
105 |  | `}`
106 |  | ``
107 |  | `/**`
108 |  | `* getter for shear in x axis`
109 |  | `*/`
110 |  | `public double getShearX() {`
111 | 0 | `return transform.getShearX();`
112 |  | `}`
113 |  | ``
114 |  | `/**`
115 |  | `* getter for shear in y axis`
116 |  | `*/`
117 |  | `public double getShearY() {`
118 | 0 | `return transform.getShearY();`
119 |  | `}`
120 |  | ``
121 |  | `/**`
122 |  | `* get the translate x value`
123 |  | `*/`
124 |  | `public double getTranslateX() {`
125 | 0 | `return transform.getTranslateX();`
126 |  | `}`
127 |  | ``
128 |  | `/**`
129 |  | `* get the translate y value`
130 |  | `*/`
131 |  | `public double getTranslateY() {`
132 | 0 | `return transform.getTranslateY();`
133 |  | `}`
134 |  | ``
135 |  | ``
136 |  | ``
137 |  | ``
138 |  | ``
139 |  | `/**`
140 |  | `* applies the transform to the supplied point`
141 |  | `*/`
142 |  | `public Point2D transform(Point2D p) {`
143 | 0 | `if(p == null) return null;`
144 | 0 | `return transform.transform(p, null);`
145 |  | `}`
146 |  | ``
147 |  | `/**`
148 |  | `* transform the supplied shape from graph coordinates to`
149 |  | `* screen coordinates`
150 |  | `* @return the GeneralPath of the transformed shape`
151 |  | `*/`
152 |  | `public Shape transform(Shape shape) {`
153 | 0 | `GeneralPath newPath = new GeneralPath();`
154 | 0 | `float[] coords = new float[6];`
155 | 0 | `for(PathIterator iterator=shape.getPathIterator(null);`
156 | 0 | `iterator.isDone() == false;`
157 | 0 | `iterator.next()) {`
158 | 0 | `int type = iterator.currentSegment(coords);`
159 | 0 | `switch(type) {`
160 |  | `case PathIterator.SEG_MOVETO:`
161 | 0 | `Point2D p = transform(new Point2D.Float(coords[0], coords[1]));`
162 | 0 | `newPath.moveTo((float)p.getX(), (float)p.getY());`
163 | 0 | `break;`
164 |  | ``
165 |  | `case PathIterator.SEG_LINETO:`
166 | 0 | `p = transform(new Point2D.Float(coords[0], coords[1]));`
167 | 0 | `newPath.lineTo((float)p.getX(), (float) p.getY());`
168 | 0 | `break;`
169 |  | ``
170 |  | `case PathIterator.SEG_QUADTO:`
171 | 0 | `p = transform(new Point2D.Float(coords[0], coords[1]));`
172 | 0 | `Point2D q = transform(new Point2D.Float(coords[2], coords[3]));`
173 | 0 | `newPath.quadTo((float)p.getX(), (float)p.getY(), (float)q.getX(), (float)q.getY());`
174 | 0 | `break;`
175 |  | ``
176 |  | `case PathIterator.SEG_CUBICTO:`
177 | 0 | `p = transform(new Point2D.Float(coords[0], coords[1]));`
178 | 0 | `q = transform(new Point2D.Float(coords[2], coords[3]));`
179 | 0 | `Point2D r = transform(new Point2D.Float(coords[4], coords[5]));`
180 | 0 | `newPath.curveTo((float)p.getX(), (float)p.getY(),`
181 |  | `(float)q.getX(), (float)q.getY(),`
182 |  | `(float)r.getX(), (float)r.getY());`
183 | 0 | `break;`
184 |  | ``
185 |  | `case PathIterator.SEG_CLOSE:`
186 | 0 | `newPath.closePath();`
187 |  | `break;`
188 |  | ``
189 |  | `}`
190 |  | `}`
191 | 0 | `return newPath;`
192 |  | `}`
193 |  | ``
194 |  | `/**`
195 |  | `* transform the supplied shape from graph coordinates to`
196 |  | `* screen coordinates`
197 |  | `* @return the GeneralPath of the transformed shape`
198 |  | `*/`
199 |  | `public Shape inverseTransform(Shape shape) {`
200 | 0 | `GeneralPath newPath = new GeneralPath();`
201 | 0 | `float[] coords = new float[6];`
202 | 0 | `for(PathIterator iterator=shape.getPathIterator(null);`
203 | 0 | `iterator.isDone() == false;`
204 | 0 | `iterator.next()) {`
205 | 0 | `int type = iterator.currentSegment(coords);`
206 | 0 | `switch(type) {`
207 |  | `case PathIterator.SEG_MOVETO:`
208 | 0 | `Point2D p = inverseTransform(new Point2D.Float(coords[0], coords[1]));`
209 | 0 | `newPath.moveTo((float)p.getX(), (float)p.getY());`
210 | 0 | `break;`
211 |  | ``
212 |  | `case PathIterator.SEG_LINETO:`
213 | 0 | `p = inverseTransform(new Point2D.Float(coords[0], coords[1]));`
214 | 0 | `newPath.lineTo((float)p.getX(), (float) p.getY());`
215 | 0 | `break;`
216 |  | ``
217 |  | `case PathIterator.SEG_QUADTO:`
218 | 0 | `p = inverseTransform(new Point2D.Float(coords[0], coords[1]));`
219 | 0 | `Point2D q = inverseTransform(new Point2D.Float(coords[2], coords[3]));`
220 | 0 | `newPath.quadTo((float)p.getX(), (float)p.getY(), (float)q.getX(), (float)q.getY());`
221 | 0 | `break;`
222 |  | ``
223 |  | `case PathIterator.SEG_CUBICTO:`
224 | 0 | `p = inverseTransform(new Point2D.Float(coords[0], coords[1]));`
225 | 0 | `q = inverseTransform(new Point2D.Float(coords[2], coords[3]));`
226 | 0 | `Point2D r = inverseTransform(new Point2D.Float(coords[4], coords[5]));`
227 | 0 | `newPath.curveTo((float)p.getX(), (float)p.getY(),`
228 |  | `(float)q.getX(), (float)q.getY(),`
229 |  | `(float)r.getX(), (float)r.getY());`
230 | 0 | `break;`
231 |  | ``
232 |  | `case PathIterator.SEG_CLOSE:`
233 | 0 | `newPath.closePath();`
234 |  | `break;`
235 |  | ``
236 |  | `}`
237 |  | `}`
238 | 0 | `return newPath;`
239 |  | `}`
240 |  | ``
241 |  | `public double getRotation() {`
242 | 0 | `double[] unitVector = new double[]{0,0,1,0};`
243 | 0 | `double[] result = new double[4];`
244 |  | ``
245 | 0 | `transform.transform(unitVector, 0, result, 0, 2);`
246 |  | ``
247 | 0 | `double dy = Math.abs(result[3] - result[1]);`
248 | 0 | `double length = Point2D.distance(result[0], result[1], result[2], result[3]);`
249 | 0 | `double rotation = Math.asin(dy / length);`
250 |  | ``
251 | 0 | `if (result[3] - result[1] > 0) {`
252 | 0 | `if (result[2] - result[0] < 0) {`
253 | 0 | `rotation = Math.PI - rotation;`
254 |  | `}`
255 |  | `} else {`
256 | 0 | `if (result[2] - result[0] > 0) {`
257 | 0 | `rotation = 2 * Math.PI - rotation;`
258 |  | `} else {`
259 | 0 | `rotation = rotation + Math.PI;`
260 |  | `}`
261 |  | `}`
262 |  | ``
263 | 0 | `return rotation;`
264 |  | `}`
265 |  | ``
266 |  | `public String toString() {`
267 | 0 | `return "Transformer using "+transform;`
268 |  | `}`
269 |  | ``
270 |  | `}`

this report was generated by version 1.0.5 of jcoverage. visit www.jcoverage.com for updates. | copyright © 2003, jcoverage ltd. all rights reserved. Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.