author | sherman |
Tue, 30 Aug 2011 11:53:11 -0700 | |
changeset 10419 | 12c063b39232 |
parent 7006 | 05505fff1342 |
child 15994 | 5c8a3d840366 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7006
05505fff1342
4358979: javax.swing.border should have a DashedBorder
malenkov
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.awt.geom; |
|
27 |
||
28 |
import java.awt.Shape; |
|
7006
05505fff1342
4358979: javax.swing.border should have a DashedBorder
malenkov
parents:
5506
diff
changeset
|
29 |
import java.beans.ConstructorProperties; |
2 | 30 |
|
31 |
/** |
|
32 |
* The <code>AffineTransform</code> class represents a 2D affine transform |
|
33 |
* that performs a linear mapping from 2D coordinates to other 2D |
|
34 |
* coordinates that preserves the "straightness" and |
|
35 |
* "parallelness" of lines. Affine transformations can be constructed |
|
36 |
* using sequences of translations, scales, flips, rotations, and shears. |
|
37 |
* <p> |
|
38 |
* Such a coordinate transformation can be represented by a 3 row by |
|
39 |
* 3 column matrix with an implied last row of [ 0 0 1 ]. This matrix |
|
40 |
* transforms source coordinates {@code (x,y)} into |
|
41 |
* destination coordinates {@code (x',y')} by considering |
|
42 |
* them to be a column vector and multiplying the coordinate vector |
|
43 |
* by the matrix according to the following process: |
|
44 |
* <pre> |
|
45 |
* [ x'] [ m00 m01 m02 ] [ x ] [ m00x + m01y + m02 ] |
|
46 |
* [ y'] = [ m10 m11 m12 ] [ y ] = [ m10x + m11y + m12 ] |
|
47 |
* [ 1 ] [ 0 0 1 ] [ 1 ] [ 1 ] |
|
48 |
* </pre> |
|
49 |
* <p> |
|
50 |
* <a name="quadrantapproximation"><h4>Handling 90-Degree Rotations</h4></a> |
|
51 |
* <p> |
|
52 |
* In some variations of the <code>rotate</code> methods in the |
|
53 |
* <code>AffineTransform</code> class, a double-precision argument |
|
54 |
* specifies the angle of rotation in radians. |
|
55 |
* These methods have special handling for rotations of approximately |
|
56 |
* 90 degrees (including multiples such as 180, 270, and 360 degrees), |
|
57 |
* so that the common case of quadrant rotation is handled more |
|
58 |
* efficiently. |
|
59 |
* This special handling can cause angles very close to multiples of |
|
60 |
* 90 degrees to be treated as if they were exact multiples of |
|
61 |
* 90 degrees. |
|
62 |
* For small multiples of 90 degrees the range of angles treated |
|
63 |
* as a quadrant rotation is approximately 0.00000121 degrees wide. |
|
64 |
* This section explains why such special care is needed and how |
|
65 |
* it is implemented. |
|
66 |
* <p> |
|
67 |
* Since 90 degrees is represented as <code>PI/2</code> in radians, |
|
68 |
* and since PI is a transcendental (and therefore irrational) number, |
|
69 |
* it is not possible to exactly represent a multiple of 90 degrees as |
|
70 |
* an exact double precision value measured in radians. |
|
71 |
* As a result it is theoretically impossible to describe quadrant |
|
72 |
* rotations (90, 180, 270 or 360 degrees) using these values. |
|
73 |
* Double precision floating point values can get very close to |
|
74 |
* non-zero multiples of <code>PI/2</code> but never close enough |
|
75 |
* for the sine or cosine to be exactly 0.0, 1.0 or -1.0. |
|
76 |
* The implementations of <code>Math.sin()</code> and |
|
77 |
* <code>Math.cos()</code> correspondingly never return 0.0 |
|
78 |
* for any case other than <code>Math.sin(0.0)</code>. |
|
79 |
* These same implementations do, however, return exactly 1.0 and |
|
80 |
* -1.0 for some range of numbers around each multiple of 90 |
|
81 |
* degrees since the correct answer is so close to 1.0 or -1.0 that |
|
82 |
* the double precision significand cannot represent the difference |
|
83 |
* as accurately as it can for numbers that are near 0.0. |
|
84 |
* <p> |
|
85 |
* The net result of these issues is that if the |
|
86 |
* <code>Math.sin()</code> and <code>Math.cos()</code> methods |
|
87 |
* are used to directly generate the values for the matrix modifications |
|
88 |
* during these radian-based rotation operations then the resulting |
|
89 |
* transform is never strictly classifiable as a quadrant rotation |
|
90 |
* even for a simple case like <code>rotate(Math.PI/2.0)</code>, |
|
91 |
* due to minor variations in the matrix caused by the non-0.0 values |
|
92 |
* obtained for the sine and cosine. |
|
93 |
* If these transforms are not classified as quadrant rotations then |
|
94 |
* subsequent code which attempts to optimize further operations based |
|
95 |
* upon the type of the transform will be relegated to its most general |
|
96 |
* implementation. |
|
97 |
* <p> |
|
98 |
* Because quadrant rotations are fairly common, |
|
99 |
* this class should handle these cases reasonably quickly, both in |
|
100 |
* applying the rotations to the transform and in applying the resulting |
|
101 |
* transform to the coordinates. |
|
102 |
* To facilitate this optimal handling, the methods which take an angle |
|
103 |
* of rotation measured in radians attempt to detect angles that are |
|
104 |
* intended to be quadrant rotations and treat them as such. |
|
105 |
* These methods therefore treat an angle <em>theta</em> as a quadrant |
|
106 |
* rotation if either <code>Math.sin(<em>theta</em>)</code> or |
|
107 |
* <code>Math.cos(<em>theta</em>)</code> returns exactly 1.0 or -1.0. |
|
108 |
* As a rule of thumb, this property holds true for a range of |
|
109 |
* approximately 0.0000000211 radians (or 0.00000121 degrees) around |
|
110 |
* small multiples of <code>Math.PI/2.0</code>. |
|
111 |
* |
|
112 |
* @author Jim Graham |
|
113 |
* @since 1.2 |
|
114 |
*/ |
|
115 |
public class AffineTransform implements Cloneable, java.io.Serializable { |
|
116 |
||
117 |
/* |
|
118 |
* This constant is only useful for the cached type field. |
|
119 |
* It indicates that the type has been decached and must be recalculated. |
|
120 |
*/ |
|
121 |
private static final int TYPE_UNKNOWN = -1; |
|
122 |
||
123 |
/** |
|
124 |
* This constant indicates that the transform defined by this object |
|
125 |
* is an identity transform. |
|
126 |
* An identity transform is one in which the output coordinates are |
|
127 |
* always the same as the input coordinates. |
|
128 |
* If this transform is anything other than the identity transform, |
|
129 |
* the type will either be the constant GENERAL_TRANSFORM or a |
|
130 |
* combination of the appropriate flag bits for the various coordinate |
|
131 |
* conversions that this transform performs. |
|
132 |
* @see #TYPE_TRANSLATION |
|
133 |
* @see #TYPE_UNIFORM_SCALE |
|
134 |
* @see #TYPE_GENERAL_SCALE |
|
135 |
* @see #TYPE_FLIP |
|
136 |
* @see #TYPE_QUADRANT_ROTATION |
|
137 |
* @see #TYPE_GENERAL_ROTATION |
|
138 |
* @see #TYPE_GENERAL_TRANSFORM |
|
139 |
* @see #getType |
|
140 |
* @since 1.2 |
|
141 |
*/ |
|
142 |
public static final int TYPE_IDENTITY = 0; |
|
143 |
||
144 |
/** |
|
145 |
* This flag bit indicates that the transform defined by this object |
|
146 |
* performs a translation in addition to the conversions indicated |
|
147 |
* by other flag bits. |
|
148 |
* A translation moves the coordinates by a constant amount in x |
|
149 |
* and y without changing the length or angle of vectors. |
|
150 |
* @see #TYPE_IDENTITY |
|
151 |
* @see #TYPE_UNIFORM_SCALE |
|
152 |
* @see #TYPE_GENERAL_SCALE |
|
153 |
* @see #TYPE_FLIP |
|
154 |
* @see #TYPE_QUADRANT_ROTATION |
|
155 |
* @see #TYPE_GENERAL_ROTATION |
|
156 |
* @see #TYPE_GENERAL_TRANSFORM |
|
157 |
* @see #getType |
|
158 |
* @since 1.2 |
|
159 |
*/ |
|
160 |
public static final int TYPE_TRANSLATION = 1; |
|
161 |
||
162 |
/** |
|
163 |
* This flag bit indicates that the transform defined by this object |
|
164 |
* performs a uniform scale in addition to the conversions indicated |
|
165 |
* by other flag bits. |
|
166 |
* A uniform scale multiplies the length of vectors by the same amount |
|
167 |
* in both the x and y directions without changing the angle between |
|
168 |
* vectors. |
|
169 |
* This flag bit is mutually exclusive with the TYPE_GENERAL_SCALE flag. |
|
170 |
* @see #TYPE_IDENTITY |
|
171 |
* @see #TYPE_TRANSLATION |
|
172 |
* @see #TYPE_GENERAL_SCALE |
|
173 |
* @see #TYPE_FLIP |
|
174 |
* @see #TYPE_QUADRANT_ROTATION |
|
175 |
* @see #TYPE_GENERAL_ROTATION |
|
176 |
* @see #TYPE_GENERAL_TRANSFORM |
|
177 |
* @see #getType |
|
178 |
* @since 1.2 |
|
179 |
*/ |
|
180 |
public static final int TYPE_UNIFORM_SCALE = 2; |
|
181 |
||
182 |
/** |
|
183 |
* This flag bit indicates that the transform defined by this object |
|
184 |
* performs a general scale in addition to the conversions indicated |
|
185 |
* by other flag bits. |
|
186 |
* A general scale multiplies the length of vectors by different |
|
187 |
* amounts in the x and y directions without changing the angle |
|
188 |
* between perpendicular vectors. |
|
189 |
* This flag bit is mutually exclusive with the TYPE_UNIFORM_SCALE flag. |
|
190 |
* @see #TYPE_IDENTITY |
|
191 |
* @see #TYPE_TRANSLATION |
|
192 |
* @see #TYPE_UNIFORM_SCALE |
|
193 |
* @see #TYPE_FLIP |
|
194 |
* @see #TYPE_QUADRANT_ROTATION |
|
195 |
* @see #TYPE_GENERAL_ROTATION |
|
196 |
* @see #TYPE_GENERAL_TRANSFORM |
|
197 |
* @see #getType |
|
198 |
* @since 1.2 |
|
199 |
*/ |
|
200 |
public static final int TYPE_GENERAL_SCALE = 4; |
|
201 |
||
202 |
/** |
|
203 |
* This constant is a bit mask for any of the scale flag bits. |
|
204 |
* @see #TYPE_UNIFORM_SCALE |
|
205 |
* @see #TYPE_GENERAL_SCALE |
|
206 |
* @since 1.2 |
|
207 |
*/ |
|
208 |
public static final int TYPE_MASK_SCALE = (TYPE_UNIFORM_SCALE | |
|
209 |
TYPE_GENERAL_SCALE); |
|
210 |
||
211 |
/** |
|
212 |
* This flag bit indicates that the transform defined by this object |
|
213 |
* performs a mirror image flip about some axis which changes the |
|
214 |
* normally right handed coordinate system into a left handed |
|
215 |
* system in addition to the conversions indicated by other flag bits. |
|
216 |
* A right handed coordinate system is one where the positive X |
|
217 |
* axis rotates counterclockwise to overlay the positive Y axis |
|
218 |
* similar to the direction that the fingers on your right hand |
|
219 |
* curl when you stare end on at your thumb. |
|
220 |
* A left handed coordinate system is one where the positive X |
|
221 |
* axis rotates clockwise to overlay the positive Y axis similar |
|
222 |
* to the direction that the fingers on your left hand curl. |
|
223 |
* There is no mathematical way to determine the angle of the |
|
224 |
* original flipping or mirroring transformation since all angles |
|
225 |
* of flip are identical given an appropriate adjusting rotation. |
|
226 |
* @see #TYPE_IDENTITY |
|
227 |
* @see #TYPE_TRANSLATION |
|
228 |
* @see #TYPE_UNIFORM_SCALE |
|
229 |
* @see #TYPE_GENERAL_SCALE |
|
230 |
* @see #TYPE_QUADRANT_ROTATION |
|
231 |
* @see #TYPE_GENERAL_ROTATION |
|
232 |
* @see #TYPE_GENERAL_TRANSFORM |
|
233 |
* @see #getType |
|
234 |
* @since 1.2 |
|
235 |
*/ |
|
236 |
public static final int TYPE_FLIP = 64; |
|
237 |
/* NOTE: TYPE_FLIP was added after GENERAL_TRANSFORM was in public |
|
238 |
* circulation and the flag bits could no longer be conveniently |
|
239 |
* renumbered without introducing binary incompatibility in outside |
|
240 |
* code. |
|
241 |
*/ |
|
242 |
||
243 |
/** |
|
244 |
* This flag bit indicates that the transform defined by this object |
|
245 |
* performs a quadrant rotation by some multiple of 90 degrees in |
|
246 |
* addition to the conversions indicated by other flag bits. |
|
247 |
* A rotation changes the angles of vectors by the same amount |
|
248 |
* regardless of the original direction of the vector and without |
|
249 |
* changing the length of the vector. |
|
250 |
* This flag bit is mutually exclusive with the TYPE_GENERAL_ROTATION flag. |
|
251 |
* @see #TYPE_IDENTITY |
|
252 |
* @see #TYPE_TRANSLATION |
|
253 |
* @see #TYPE_UNIFORM_SCALE |
|
254 |
* @see #TYPE_GENERAL_SCALE |
|
255 |
* @see #TYPE_FLIP |
|
256 |
* @see #TYPE_GENERAL_ROTATION |
|
257 |
* @see #TYPE_GENERAL_TRANSFORM |
|
258 |
* @see #getType |
|
259 |
* @since 1.2 |
|
260 |
*/ |
|
261 |
public static final int TYPE_QUADRANT_ROTATION = 8; |
|
262 |
||
263 |
/** |
|
264 |
* This flag bit indicates that the transform defined by this object |
|
265 |
* performs a rotation by an arbitrary angle in addition to the |
|
266 |
* conversions indicated by other flag bits. |
|
267 |
* A rotation changes the angles of vectors by the same amount |
|
268 |
* regardless of the original direction of the vector and without |
|
269 |
* changing the length of the vector. |
|
270 |
* This flag bit is mutually exclusive with the |
|
271 |
* TYPE_QUADRANT_ROTATION flag. |
|
272 |
* @see #TYPE_IDENTITY |
|
273 |
* @see #TYPE_TRANSLATION |
|
274 |
* @see #TYPE_UNIFORM_SCALE |
|
275 |
* @see #TYPE_GENERAL_SCALE |
|
276 |
* @see #TYPE_FLIP |
|
277 |
* @see #TYPE_QUADRANT_ROTATION |
|
278 |
* @see #TYPE_GENERAL_TRANSFORM |
|
279 |
* @see #getType |
|
280 |
* @since 1.2 |
|
281 |
*/ |
|
282 |
public static final int TYPE_GENERAL_ROTATION = 16; |
|
283 |
||
284 |
/** |
|
285 |
* This constant is a bit mask for any of the rotation flag bits. |
|
286 |
* @see #TYPE_QUADRANT_ROTATION |
|
287 |
* @see #TYPE_GENERAL_ROTATION |
|
288 |
* @since 1.2 |
|
289 |
*/ |
|
290 |
public static final int TYPE_MASK_ROTATION = (TYPE_QUADRANT_ROTATION | |
|
291 |
TYPE_GENERAL_ROTATION); |
|
292 |
||
293 |
/** |
|
294 |
* This constant indicates that the transform defined by this object |
|
295 |
* performs an arbitrary conversion of the input coordinates. |
|
296 |
* If this transform can be classified by any of the above constants, |
|
297 |
* the type will either be the constant TYPE_IDENTITY or a |
|
298 |
* combination of the appropriate flag bits for the various coordinate |
|
299 |
* conversions that this transform performs. |
|
300 |
* @see #TYPE_IDENTITY |
|
301 |
* @see #TYPE_TRANSLATION |
|
302 |
* @see #TYPE_UNIFORM_SCALE |
|
303 |
* @see #TYPE_GENERAL_SCALE |
|
304 |
* @see #TYPE_FLIP |
|
305 |
* @see #TYPE_QUADRANT_ROTATION |
|
306 |
* @see #TYPE_GENERAL_ROTATION |
|
307 |
* @see #getType |
|
308 |
* @since 1.2 |
|
309 |
*/ |
|
310 |
public static final int TYPE_GENERAL_TRANSFORM = 32; |
|
311 |
||
312 |
/** |
|
313 |
* This constant is used for the internal state variable to indicate |
|
314 |
* that no calculations need to be performed and that the source |
|
315 |
* coordinates only need to be copied to their destinations to |
|
316 |
* complete the transformation equation of this transform. |
|
317 |
* @see #APPLY_TRANSLATE |
|
318 |
* @see #APPLY_SCALE |
|
319 |
* @see #APPLY_SHEAR |
|
320 |
* @see #state |
|
321 |
*/ |
|
322 |
static final int APPLY_IDENTITY = 0; |
|
323 |
||
324 |
/** |
|
325 |
* This constant is used for the internal state variable to indicate |
|
326 |
* that the translation components of the matrix (m02 and m12) need |
|
327 |
* to be added to complete the transformation equation of this transform. |
|
328 |
* @see #APPLY_IDENTITY |
|
329 |
* @see #APPLY_SCALE |
|
330 |
* @see #APPLY_SHEAR |
|
331 |
* @see #state |
|
332 |
*/ |
|
333 |
static final int APPLY_TRANSLATE = 1; |
|
334 |
||
335 |
/** |
|
336 |
* This constant is used for the internal state variable to indicate |
|
337 |
* that the scaling components of the matrix (m00 and m11) need |
|
338 |
* to be factored in to complete the transformation equation of |
|
339 |
* this transform. If the APPLY_SHEAR bit is also set then it |
|
340 |
* indicates that the scaling components are not both 0.0. If the |
|
341 |
* APPLY_SHEAR bit is not also set then it indicates that the |
|
342 |
* scaling components are not both 1.0. If neither the APPLY_SHEAR |
|
343 |
* nor the APPLY_SCALE bits are set then the scaling components |
|
344 |
* are both 1.0, which means that the x and y components contribute |
|
345 |
* to the transformed coordinate, but they are not multiplied by |
|
346 |
* any scaling factor. |
|
347 |
* @see #APPLY_IDENTITY |
|
348 |
* @see #APPLY_TRANSLATE |
|
349 |
* @see #APPLY_SHEAR |
|
350 |
* @see #state |
|
351 |
*/ |
|
352 |
static final int APPLY_SCALE = 2; |
|
353 |
||
354 |
/** |
|
355 |
* This constant is used for the internal state variable to indicate |
|
356 |
* that the shearing components of the matrix (m01 and m10) need |
|
357 |
* to be factored in to complete the transformation equation of this |
|
358 |
* transform. The presence of this bit in the state variable changes |
|
359 |
* the interpretation of the APPLY_SCALE bit as indicated in its |
|
360 |
* documentation. |
|
361 |
* @see #APPLY_IDENTITY |
|
362 |
* @see #APPLY_TRANSLATE |
|
363 |
* @see #APPLY_SCALE |
|
364 |
* @see #state |
|
365 |
*/ |
|
366 |
static final int APPLY_SHEAR = 4; |
|
367 |
||
368 |
/* |
|
369 |
* For methods which combine together the state of two separate |
|
370 |
* transforms and dispatch based upon the combination, these constants |
|
371 |
* specify how far to shift one of the states so that the two states |
|
372 |
* are mutually non-interfering and provide constants for testing the |
|
373 |
* bits of the shifted (HI) state. The methods in this class use |
|
374 |
* the convention that the state of "this" transform is unshifted and |
|
375 |
* the state of the "other" or "argument" transform is shifted (HI). |
|
376 |
*/ |
|
377 |
private static final int HI_SHIFT = 3; |
|
378 |
private static final int HI_IDENTITY = APPLY_IDENTITY << HI_SHIFT; |
|
379 |
private static final int HI_TRANSLATE = APPLY_TRANSLATE << HI_SHIFT; |
|
380 |
private static final int HI_SCALE = APPLY_SCALE << HI_SHIFT; |
|
381 |
private static final int HI_SHEAR = APPLY_SHEAR << HI_SHIFT; |
|
382 |
||
383 |
/** |
|
384 |
* The X coordinate scaling element of the 3x3 |
|
385 |
* affine transformation matrix. |
|
386 |
* |
|
387 |
* @serial |
|
388 |
*/ |
|
389 |
double m00; |
|
390 |
||
391 |
/** |
|
392 |
* The Y coordinate shearing element of the 3x3 |
|
393 |
* affine transformation matrix. |
|
394 |
* |
|
395 |
* @serial |
|
396 |
*/ |
|
397 |
double m10; |
|
398 |
||
399 |
/** |
|
400 |
* The X coordinate shearing element of the 3x3 |
|
401 |
* affine transformation matrix. |
|
402 |
* |
|
403 |
* @serial |
|
404 |
*/ |
|
405 |
double m01; |
|
406 |
||
407 |
/** |
|
408 |
* The Y coordinate scaling element of the 3x3 |
|
409 |
* affine transformation matrix. |
|
410 |
* |
|
411 |
* @serial |
|
412 |
*/ |
|
413 |
double m11; |
|
414 |
||
415 |
/** |
|
416 |
* The X coordinate of the translation element of the |
|
417 |
* 3x3 affine transformation matrix. |
|
418 |
* |
|
419 |
* @serial |
|
420 |
*/ |
|
421 |
double m02; |
|
422 |
||
423 |
/** |
|
424 |
* The Y coordinate of the translation element of the |
|
425 |
* 3x3 affine transformation matrix. |
|
426 |
* |
|
427 |
* @serial |
|
428 |
*/ |
|
429 |
double m12; |
|
430 |
||
431 |
/** |
|
432 |
* This field keeps track of which components of the matrix need to |
|
433 |
* be applied when performing a transformation. |
|
434 |
* @see #APPLY_IDENTITY |
|
435 |
* @see #APPLY_TRANSLATE |
|
436 |
* @see #APPLY_SCALE |
|
437 |
* @see #APPLY_SHEAR |
|
438 |
*/ |
|
439 |
transient int state; |
|
440 |
||
441 |
/** |
|
442 |
* This field caches the current transformation type of the matrix. |
|
443 |
* @see #TYPE_IDENTITY |
|
444 |
* @see #TYPE_TRANSLATION |
|
445 |
* @see #TYPE_UNIFORM_SCALE |
|
446 |
* @see #TYPE_GENERAL_SCALE |
|
447 |
* @see #TYPE_FLIP |
|
448 |
* @see #TYPE_QUADRANT_ROTATION |
|
449 |
* @see #TYPE_GENERAL_ROTATION |
|
450 |
* @see #TYPE_GENERAL_TRANSFORM |
|
451 |
* @see #TYPE_UNKNOWN |
|
452 |
* @see #getType |
|
453 |
*/ |
|
454 |
private transient int type; |
|
455 |
||
456 |
private AffineTransform(double m00, double m10, |
|
457 |
double m01, double m11, |
|
458 |
double m02, double m12, |
|
459 |
int state) { |
|
460 |
this.m00 = m00; |
|
461 |
this.m10 = m10; |
|
462 |
this.m01 = m01; |
|
463 |
this.m11 = m11; |
|
464 |
this.m02 = m02; |
|
465 |
this.m12 = m12; |
|
466 |
this.state = state; |
|
467 |
this.type = TYPE_UNKNOWN; |
|
468 |
} |
|
469 |
||
470 |
/** |
|
471 |
* Constructs a new <code>AffineTransform</code> representing the |
|
472 |
* Identity transformation. |
|
473 |
* @since 1.2 |
|
474 |
*/ |
|
475 |
public AffineTransform() { |
|
476 |
m00 = m11 = 1.0; |
|
477 |
// m01 = m10 = m02 = m12 = 0.0; /* Not needed. */ |
|
478 |
// state = APPLY_IDENTITY; /* Not needed. */ |
|
479 |
// type = TYPE_IDENTITY; /* Not needed. */ |
|
480 |
} |
|
481 |
||
482 |
/** |
|
483 |
* Constructs a new <code>AffineTransform</code> that is a copy of |
|
484 |
* the specified <code>AffineTransform</code> object. |
|
485 |
* @param Tx the <code>AffineTransform</code> object to copy |
|
486 |
* @since 1.2 |
|
487 |
*/ |
|
488 |
public AffineTransform(AffineTransform Tx) { |
|
489 |
this.m00 = Tx.m00; |
|
490 |
this.m10 = Tx.m10; |
|
491 |
this.m01 = Tx.m01; |
|
492 |
this.m11 = Tx.m11; |
|
493 |
this.m02 = Tx.m02; |
|
494 |
this.m12 = Tx.m12; |
|
495 |
this.state = Tx.state; |
|
496 |
this.type = Tx.type; |
|
497 |
} |
|
498 |
||
499 |
/** |
|
500 |
* Constructs a new <code>AffineTransform</code> from 6 floating point |
|
501 |
* values representing the 6 specifiable entries of the 3x3 |
|
502 |
* transformation matrix. |
|
503 |
* |
|
504 |
* @param m00 the X coordinate scaling element of the 3x3 matrix |
|
505 |
* @param m10 the Y coordinate shearing element of the 3x3 matrix |
|
506 |
* @param m01 the X coordinate shearing element of the 3x3 matrix |
|
507 |
* @param m11 the Y coordinate scaling element of the 3x3 matrix |
|
508 |
* @param m02 the X coordinate translation element of the 3x3 matrix |
|
509 |
* @param m12 the Y coordinate translation element of the 3x3 matrix |
|
510 |
* @since 1.2 |
|
511 |
*/ |
|
7006
05505fff1342
4358979: javax.swing.border should have a DashedBorder
malenkov
parents:
5506
diff
changeset
|
512 |
@ConstructorProperties({ "scaleX", "shearY", "shearX", "scaleY", "translateX", "translateY" }) |
2 | 513 |
public AffineTransform(float m00, float m10, |
514 |
float m01, float m11, |
|
515 |
float m02, float m12) { |
|
516 |
this.m00 = m00; |
|
517 |
this.m10 = m10; |
|
518 |
this.m01 = m01; |
|
519 |
this.m11 = m11; |
|
520 |
this.m02 = m02; |
|
521 |
this.m12 = m12; |
|
522 |
updateState(); |
|
523 |
} |
|
524 |
||
525 |
/** |
|
526 |
* Constructs a new <code>AffineTransform</code> from an array of |
|
527 |
* floating point values representing either the 4 non-translation |
|
528 |
* enries or the 6 specifiable entries of the 3x3 transformation |
|
529 |
* matrix. The values are retrieved from the array as |
|
530 |
* { m00 m10 m01 m11 [m02 m12]}. |
|
531 |
* @param flatmatrix the float array containing the values to be set |
|
532 |
* in the new <code>AffineTransform</code> object. The length of the |
|
533 |
* array is assumed to be at least 4. If the length of the array is |
|
534 |
* less than 6, only the first 4 values are taken. If the length of |
|
535 |
* the array is greater than 6, the first 6 values are taken. |
|
536 |
* @since 1.2 |
|
537 |
*/ |
|
538 |
public AffineTransform(float[] flatmatrix) { |
|
539 |
m00 = flatmatrix[0]; |
|
540 |
m10 = flatmatrix[1]; |
|
541 |
m01 = flatmatrix[2]; |
|
542 |
m11 = flatmatrix[3]; |
|
543 |
if (flatmatrix.length > 5) { |
|
544 |
m02 = flatmatrix[4]; |
|
545 |
m12 = flatmatrix[5]; |
|
546 |
} |
|
547 |
updateState(); |
|
548 |
} |
|
549 |
||
550 |
/** |
|
551 |
* Constructs a new <code>AffineTransform</code> from 6 double |
|
552 |
* precision values representing the 6 specifiable entries of the 3x3 |
|
553 |
* transformation matrix. |
|
554 |
* |
|
555 |
* @param m00 the X coordinate scaling element of the 3x3 matrix |
|
556 |
* @param m10 the Y coordinate shearing element of the 3x3 matrix |
|
557 |
* @param m01 the X coordinate shearing element of the 3x3 matrix |
|
558 |
* @param m11 the Y coordinate scaling element of the 3x3 matrix |
|
559 |
* @param m02 the X coordinate translation element of the 3x3 matrix |
|
560 |
* @param m12 the Y coordinate translation element of the 3x3 matrix |
|
561 |
* @since 1.2 |
|
562 |
*/ |
|
563 |
public AffineTransform(double m00, double m10, |
|
564 |
double m01, double m11, |
|
565 |
double m02, double m12) { |
|
566 |
this.m00 = m00; |
|
567 |
this.m10 = m10; |
|
568 |
this.m01 = m01; |
|
569 |
this.m11 = m11; |
|
570 |
this.m02 = m02; |
|
571 |
this.m12 = m12; |
|
572 |
updateState(); |
|
573 |
} |
|
574 |
||
575 |
/** |
|
576 |
* Constructs a new <code>AffineTransform</code> from an array of |
|
577 |
* double precision values representing either the 4 non-translation |
|
578 |
* entries or the 6 specifiable entries of the 3x3 transformation |
|
579 |
* matrix. The values are retrieved from the array as |
|
580 |
* { m00 m10 m01 m11 [m02 m12]}. |
|
581 |
* @param flatmatrix the double array containing the values to be set |
|
582 |
* in the new <code>AffineTransform</code> object. The length of the |
|
583 |
* array is assumed to be at least 4. If the length of the array is |
|
584 |
* less than 6, only the first 4 values are taken. If the length of |
|
585 |
* the array is greater than 6, the first 6 values are taken. |
|
586 |
* @since 1.2 |
|
587 |
*/ |
|
588 |
public AffineTransform(double[] flatmatrix) { |
|
589 |
m00 = flatmatrix[0]; |
|
590 |
m10 = flatmatrix[1]; |
|
591 |
m01 = flatmatrix[2]; |
|
592 |
m11 = flatmatrix[3]; |
|
593 |
if (flatmatrix.length > 5) { |
|
594 |
m02 = flatmatrix[4]; |
|
595 |
m12 = flatmatrix[5]; |
|
596 |
} |
|
597 |
updateState(); |
|
598 |
} |
|
599 |
||
600 |
/** |
|
601 |
* Returns a transform representing a translation transformation. |
|
602 |
* The matrix representing the returned transform is: |
|
603 |
* <pre> |
|
604 |
* [ 1 0 tx ] |
|
605 |
* [ 0 1 ty ] |
|
606 |
* [ 0 0 1 ] |
|
607 |
* </pre> |
|
608 |
* @param tx the distance by which coordinates are translated in the |
|
609 |
* X axis direction |
|
610 |
* @param ty the distance by which coordinates are translated in the |
|
611 |
* Y axis direction |
|
612 |
* @return an <code>AffineTransform</code> object that represents a |
|
613 |
* translation transformation, created with the specified vector. |
|
614 |
* @since 1.2 |
|
615 |
*/ |
|
616 |
public static AffineTransform getTranslateInstance(double tx, double ty) { |
|
617 |
AffineTransform Tx = new AffineTransform(); |
|
618 |
Tx.setToTranslation(tx, ty); |
|
619 |
return Tx; |
|
620 |
} |
|
621 |
||
622 |
/** |
|
623 |
* Returns a transform representing a rotation transformation. |
|
624 |
* The matrix representing the returned transform is: |
|
625 |
* <pre> |
|
626 |
* [ cos(theta) -sin(theta) 0 ] |
|
627 |
* [ sin(theta) cos(theta) 0 ] |
|
628 |
* [ 0 0 1 ] |
|
629 |
* </pre> |
|
630 |
* Rotating by a positive angle theta rotates points on the positive |
|
631 |
* X axis toward the positive Y axis. |
|
632 |
* Note also the discussion of |
|
633 |
* <a href="#quadrantapproximation">Handling 90-Degree Rotations</a> |
|
634 |
* above. |
|
635 |
* @param theta the angle of rotation measured in radians |
|
636 |
* @return an <code>AffineTransform</code> object that is a rotation |
|
637 |
* transformation, created with the specified angle of rotation. |
|
638 |
* @since 1.2 |
|
639 |
*/ |
|
640 |
public static AffineTransform getRotateInstance(double theta) { |
|
641 |
AffineTransform Tx = new AffineTransform(); |
|
642 |
Tx.setToRotation(theta); |
|
643 |
return Tx; |
|
644 |
} |
|
645 |
||
646 |
/** |
|
647 |
* Returns a transform that rotates coordinates around an anchor point. |
|
648 |
* This operation is equivalent to translating the coordinates so |
|
649 |
* that the anchor point is at the origin (S1), then rotating them |
|
650 |
* about the new origin (S2), and finally translating so that the |
|
651 |
* intermediate origin is restored to the coordinates of the original |
|
652 |
* anchor point (S3). |
|
653 |
* <p> |
|
654 |
* This operation is equivalent to the following sequence of calls: |
|
655 |
* <pre> |
|
656 |
* AffineTransform Tx = new AffineTransform(); |
|
657 |
* Tx.translate(anchorx, anchory); // S3: final translation |
|
658 |
* Tx.rotate(theta); // S2: rotate around anchor |
|
659 |
* Tx.translate(-anchorx, -anchory); // S1: translate anchor to origin |
|
660 |
* </pre> |
|
661 |
* The matrix representing the returned transform is: |
|
662 |
* <pre> |
|
663 |
* [ cos(theta) -sin(theta) x-x*cos+y*sin ] |
|
664 |
* [ sin(theta) cos(theta) y-x*sin-y*cos ] |
|
665 |
* [ 0 0 1 ] |
|
666 |
* </pre> |
|
667 |
* Rotating by a positive angle theta rotates points on the positive |
|
668 |
* X axis toward the positive Y axis. |
|
669 |
* Note also the discussion of |
|
670 |
* <a href="#quadrantapproximation">Handling 90-Degree Rotations</a> |
|
671 |
* above. |
|
672 |
* |
|
673 |
* @param theta the angle of rotation measured in radians |
|
674 |
* @param anchorx the X coordinate of the rotation anchor point |
|
675 |
* @param anchory the Y coordinate of the rotation anchor point |
|
676 |
* @return an <code>AffineTransform</code> object that rotates |
|
677 |
* coordinates around the specified point by the specified angle of |
|
678 |
* rotation. |
|
679 |
* @since 1.2 |
|
680 |
*/ |
|
681 |
public static AffineTransform getRotateInstance(double theta, |
|
682 |
double anchorx, |
|
683 |
double anchory) |
|
684 |
{ |
|
685 |
AffineTransform Tx = new AffineTransform(); |
|
686 |
Tx.setToRotation(theta, anchorx, anchory); |
|
687 |
return Tx; |
|
688 |
} |
|
689 |
||
690 |
/** |
|
691 |
* Returns a transform that rotates coordinates according to |
|
692 |
* a rotation vector. |
|
693 |
* All coordinates rotate about the origin by the same amount. |
|
694 |
* The amount of rotation is such that coordinates along the former |
|
695 |
* positive X axis will subsequently align with the vector pointing |
|
696 |
* from the origin to the specified vector coordinates. |
|
697 |
* If both <code>vecx</code> and <code>vecy</code> are 0.0, |
|
698 |
* an identity transform is returned. |
|
699 |
* This operation is equivalent to calling: |
|
700 |
* <pre> |
|
701 |
* AffineTransform.getRotateInstance(Math.atan2(vecy, vecx)); |
|
702 |
* </pre> |
|
703 |
* |
|
704 |
* @param vecx the X coordinate of the rotation vector |
|
705 |
* @param vecy the Y coordinate of the rotation vector |
|
706 |
* @return an <code>AffineTransform</code> object that rotates |
|
707 |
* coordinates according to the specified rotation vector. |
|
708 |
* @since 1.6 |
|
709 |
*/ |
|
710 |
public static AffineTransform getRotateInstance(double vecx, double vecy) { |
|
711 |
AffineTransform Tx = new AffineTransform(); |
|
712 |
Tx.setToRotation(vecx, vecy); |
|
713 |
return Tx; |
|
714 |
} |
|
715 |
||
716 |
/** |
|
717 |
* Returns a transform that rotates coordinates around an anchor |
|
718 |
* point accordinate to a rotation vector. |
|
719 |
* All coordinates rotate about the specified anchor coordinates |
|
720 |
* by the same amount. |
|
721 |
* The amount of rotation is such that coordinates along the former |
|
722 |
* positive X axis will subsequently align with the vector pointing |
|
723 |
* from the origin to the specified vector coordinates. |
|
724 |
* If both <code>vecx</code> and <code>vecy</code> are 0.0, |
|
725 |
* an identity transform is returned. |
|
726 |
* This operation is equivalent to calling: |
|
727 |
* <pre> |
|
728 |
* AffineTransform.getRotateInstance(Math.atan2(vecy, vecx), |
|
729 |
* anchorx, anchory); |
|
730 |
* </pre> |
|
731 |
* |
|
732 |
* @param vecx the X coordinate of the rotation vector |
|
733 |
* @param vecy the Y coordinate of the rotation vector |
|
734 |
* @param anchorx the X coordinate of the rotation anchor point |
|
735 |
* @param anchory the Y coordinate of the rotation anchor point |
|
736 |
* @return an <code>AffineTransform</code> object that rotates |
|
737 |
* coordinates around the specified point according to the |
|
738 |
* specified rotation vector. |
|
739 |
* @since 1.6 |
|
740 |
*/ |
|
741 |
public static AffineTransform getRotateInstance(double vecx, |
|
742 |
double vecy, |
|
743 |
double anchorx, |
|
744 |
double anchory) |
|
745 |
{ |
|
746 |
AffineTransform Tx = new AffineTransform(); |
|
747 |
Tx.setToRotation(vecx, vecy, anchorx, anchory); |
|
748 |
return Tx; |
|
749 |
} |
|
750 |
||
751 |
/** |
|
752 |
* Returns a transform that rotates coordinates by the specified |
|
753 |
* number of quadrants. |
|
754 |
* This operation is equivalent to calling: |
|
755 |
* <pre> |
|
756 |
* AffineTransform.getRotateInstance(numquadrants * Math.PI / 2.0); |
|
757 |
* </pre> |
|
758 |
* Rotating by a positive number of quadrants rotates points on |
|
759 |
* the positive X axis toward the positive Y axis. |
|
760 |
* @param numquadrants the number of 90 degree arcs to rotate by |
|
761 |
* @return an <code>AffineTransform</code> object that rotates |
|
762 |
* coordinates by the specified number of quadrants. |
|
763 |
* @since 1.6 |
|
764 |
*/ |
|
765 |
public static AffineTransform getQuadrantRotateInstance(int numquadrants) { |
|
766 |
AffineTransform Tx = new AffineTransform(); |
|
767 |
Tx.setToQuadrantRotation(numquadrants); |
|
768 |
return Tx; |
|
769 |
} |
|
770 |
||
771 |
/** |
|
772 |
* Returns a transform that rotates coordinates by the specified |
|
773 |
* number of quadrants around the specified anchor point. |
|
774 |
* This operation is equivalent to calling: |
|
775 |
* <pre> |
|
776 |
* AffineTransform.getRotateInstance(numquadrants * Math.PI / 2.0, |
|
777 |
* anchorx, anchory); |
|
778 |
* </pre> |
|
779 |
* Rotating by a positive number of quadrants rotates points on |
|
780 |
* the positive X axis toward the positive Y axis. |
|
781 |
* |
|
782 |
* @param numquadrants the number of 90 degree arcs to rotate by |
|
783 |
* @param anchorx the X coordinate of the rotation anchor point |
|
784 |
* @param anchory the Y coordinate of the rotation anchor point |
|
785 |
* @return an <code>AffineTransform</code> object that rotates |
|
786 |
* coordinates by the specified number of quadrants around the |
|
787 |
* specified anchor point. |
|
788 |
* @since 1.6 |
|
789 |
*/ |
|
790 |
public static AffineTransform getQuadrantRotateInstance(int numquadrants, |
|
791 |
double anchorx, |
|
792 |
double anchory) |
|
793 |
{ |
|
794 |
AffineTransform Tx = new AffineTransform(); |
|
795 |
Tx.setToQuadrantRotation(numquadrants, anchorx, anchory); |
|
796 |
return Tx; |
|
797 |
} |
|
798 |
||
799 |
/** |
|
800 |
* Returns a transform representing a scaling transformation. |
|
801 |
* The matrix representing the returned transform is: |
|
802 |
* <pre> |
|
803 |
* [ sx 0 0 ] |
|
804 |
* [ 0 sy 0 ] |
|
805 |
* [ 0 0 1 ] |
|
806 |
* </pre> |
|
807 |
* @param sx the factor by which coordinates are scaled along the |
|
808 |
* X axis direction |
|
809 |
* @param sy the factor by which coordinates are scaled along the |
|
810 |
* Y axis direction |
|
811 |
* @return an <code>AffineTransform</code> object that scales |
|
812 |
* coordinates by the specified factors. |
|
813 |
* @since 1.2 |
|
814 |
*/ |
|
815 |
public static AffineTransform getScaleInstance(double sx, double sy) { |
|
816 |
AffineTransform Tx = new AffineTransform(); |
|
817 |
Tx.setToScale(sx, sy); |
|
818 |
return Tx; |
|
819 |
} |
|
820 |
||
821 |
/** |
|
822 |
* Returns a transform representing a shearing transformation. |
|
823 |
* The matrix representing the returned transform is: |
|
824 |
* <pre> |
|
825 |
* [ 1 shx 0 ] |
|
826 |
* [ shy 1 0 ] |
|
827 |
* [ 0 0 1 ] |
|
828 |
* </pre> |
|
829 |
* @param shx the multiplier by which coordinates are shifted in the |
|
830 |
* direction of the positive X axis as a factor of their Y coordinate |
|
831 |
* @param shy the multiplier by which coordinates are shifted in the |
|
832 |
* direction of the positive Y axis as a factor of their X coordinate |
|
833 |
* @return an <code>AffineTransform</code> object that shears |
|
834 |
* coordinates by the specified multipliers. |
|
835 |
* @since 1.2 |
|
836 |
*/ |
|
837 |
public static AffineTransform getShearInstance(double shx, double shy) { |
|
838 |
AffineTransform Tx = new AffineTransform(); |
|
839 |
Tx.setToShear(shx, shy); |
|
840 |
return Tx; |
|
841 |
} |
|
842 |
||
843 |
/** |
|
844 |
* Retrieves the flag bits describing the conversion properties of |
|
845 |
* this transform. |
|
846 |
* The return value is either one of the constants TYPE_IDENTITY |
|
847 |
* or TYPE_GENERAL_TRANSFORM, or a combination of the |
|
848 |
* appriopriate flag bits. |
|
849 |
* A valid combination of flag bits is an exclusive OR operation |
|
850 |
* that can combine |
|
851 |
* the TYPE_TRANSLATION flag bit |
|
852 |
* in addition to either of the |
|
853 |
* TYPE_UNIFORM_SCALE or TYPE_GENERAL_SCALE flag bits |
|
854 |
* as well as either of the |
|
855 |
* TYPE_QUADRANT_ROTATION or TYPE_GENERAL_ROTATION flag bits. |
|
856 |
* @return the OR combination of any of the indicated flags that |
|
857 |
* apply to this transform |
|
858 |
* @see #TYPE_IDENTITY |
|
859 |
* @see #TYPE_TRANSLATION |
|
860 |
* @see #TYPE_UNIFORM_SCALE |
|
861 |
* @see #TYPE_GENERAL_SCALE |
|
862 |
* @see #TYPE_QUADRANT_ROTATION |
|
863 |
* @see #TYPE_GENERAL_ROTATION |
|
864 |
* @see #TYPE_GENERAL_TRANSFORM |
|
865 |
* @since 1.2 |
|
866 |
*/ |
|
867 |
public int getType() { |
|
868 |
if (type == TYPE_UNKNOWN) { |
|
869 |
calculateType(); |
|
870 |
} |
|
871 |
return type; |
|
872 |
} |
|
873 |
||
874 |
/** |
|
875 |
* This is the utility function to calculate the flag bits when |
|
876 |
* they have not been cached. |
|
877 |
* @see #getType |
|
878 |
*/ |
|
879 |
private void calculateType() { |
|
880 |
int ret = TYPE_IDENTITY; |
|
881 |
boolean sgn0, sgn1; |
|
882 |
double M0, M1, M2, M3; |
|
883 |
updateState(); |
|
884 |
switch (state) { |
|
885 |
default: |
|
886 |
stateError(); |
|
887 |
/* NOTREACHED */ |
|
888 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
889 |
ret = TYPE_TRANSLATION; |
|
890 |
/* NOBREAK */ |
|
891 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
892 |
if ((M0 = m00) * (M2 = m01) + (M3 = m10) * (M1 = m11) != 0) { |
|
893 |
// Transformed unit vectors are not perpendicular... |
|
894 |
this.type = TYPE_GENERAL_TRANSFORM; |
|
895 |
return; |
|
896 |
} |
|
897 |
sgn0 = (M0 >= 0.0); |
|
898 |
sgn1 = (M1 >= 0.0); |
|
899 |
if (sgn0 == sgn1) { |
|
900 |
// sgn(M0) == sgn(M1) therefore sgn(M2) == -sgn(M3) |
|
901 |
// This is the "unflipped" (right-handed) state |
|
902 |
if (M0 != M1 || M2 != -M3) { |
|
903 |
ret |= (TYPE_GENERAL_ROTATION | TYPE_GENERAL_SCALE); |
|
904 |
} else if (M0 * M1 - M2 * M3 != 1.0) { |
|
905 |
ret |= (TYPE_GENERAL_ROTATION | TYPE_UNIFORM_SCALE); |
|
906 |
} else { |
|
907 |
ret |= TYPE_GENERAL_ROTATION; |
|
908 |
} |
|
909 |
} else { |
|
910 |
// sgn(M0) == -sgn(M1) therefore sgn(M2) == sgn(M3) |
|
911 |
// This is the "flipped" (left-handed) state |
|
912 |
if (M0 != -M1 || M2 != M3) { |
|
913 |
ret |= (TYPE_GENERAL_ROTATION | |
|
914 |
TYPE_FLIP | |
|
915 |
TYPE_GENERAL_SCALE); |
|
916 |
} else if (M0 * M1 - M2 * M3 != 1.0) { |
|
917 |
ret |= (TYPE_GENERAL_ROTATION | |
|
918 |
TYPE_FLIP | |
|
919 |
TYPE_UNIFORM_SCALE); |
|
920 |
} else { |
|
921 |
ret |= (TYPE_GENERAL_ROTATION | TYPE_FLIP); |
|
922 |
} |
|
923 |
} |
|
924 |
break; |
|
925 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
926 |
ret = TYPE_TRANSLATION; |
|
927 |
/* NOBREAK */ |
|
928 |
case (APPLY_SHEAR): |
|
929 |
sgn0 = ((M0 = m01) >= 0.0); |
|
930 |
sgn1 = ((M1 = m10) >= 0.0); |
|
931 |
if (sgn0 != sgn1) { |
|
932 |
// Different signs - simple 90 degree rotation |
|
933 |
if (M0 != -M1) { |
|
934 |
ret |= (TYPE_QUADRANT_ROTATION | TYPE_GENERAL_SCALE); |
|
935 |
} else if (M0 != 1.0 && M0 != -1.0) { |
|
936 |
ret |= (TYPE_QUADRANT_ROTATION | TYPE_UNIFORM_SCALE); |
|
937 |
} else { |
|
938 |
ret |= TYPE_QUADRANT_ROTATION; |
|
939 |
} |
|
940 |
} else { |
|
941 |
// Same signs - 90 degree rotation plus an axis flip too |
|
942 |
if (M0 == M1) { |
|
943 |
ret |= (TYPE_QUADRANT_ROTATION | |
|
944 |
TYPE_FLIP | |
|
945 |
TYPE_UNIFORM_SCALE); |
|
946 |
} else { |
|
947 |
ret |= (TYPE_QUADRANT_ROTATION | |
|
948 |
TYPE_FLIP | |
|
949 |
TYPE_GENERAL_SCALE); |
|
950 |
} |
|
951 |
} |
|
952 |
break; |
|
953 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
954 |
ret = TYPE_TRANSLATION; |
|
955 |
/* NOBREAK */ |
|
956 |
case (APPLY_SCALE): |
|
957 |
sgn0 = ((M0 = m00) >= 0.0); |
|
958 |
sgn1 = ((M1 = m11) >= 0.0); |
|
959 |
if (sgn0 == sgn1) { |
|
960 |
if (sgn0) { |
|
961 |
// Both scaling factors non-negative - simple scale |
|
962 |
// Note: APPLY_SCALE implies M0, M1 are not both 1 |
|
963 |
if (M0 == M1) { |
|
964 |
ret |= TYPE_UNIFORM_SCALE; |
|
965 |
} else { |
|
966 |
ret |= TYPE_GENERAL_SCALE; |
|
967 |
} |
|
968 |
} else { |
|
969 |
// Both scaling factors negative - 180 degree rotation |
|
970 |
if (M0 != M1) { |
|
971 |
ret |= (TYPE_QUADRANT_ROTATION | TYPE_GENERAL_SCALE); |
|
972 |
} else if (M0 != -1.0) { |
|
973 |
ret |= (TYPE_QUADRANT_ROTATION | TYPE_UNIFORM_SCALE); |
|
974 |
} else { |
|
975 |
ret |= TYPE_QUADRANT_ROTATION; |
|
976 |
} |
|
977 |
} |
|
978 |
} else { |
|
979 |
// Scaling factor signs different - flip about some axis |
|
980 |
if (M0 == -M1) { |
|
981 |
if (M0 == 1.0 || M0 == -1.0) { |
|
982 |
ret |= TYPE_FLIP; |
|
983 |
} else { |
|
984 |
ret |= (TYPE_FLIP | TYPE_UNIFORM_SCALE); |
|
985 |
} |
|
986 |
} else { |
|
987 |
ret |= (TYPE_FLIP | TYPE_GENERAL_SCALE); |
|
988 |
} |
|
989 |
} |
|
990 |
break; |
|
991 |
case (APPLY_TRANSLATE): |
|
992 |
ret = TYPE_TRANSLATION; |
|
993 |
break; |
|
994 |
case (APPLY_IDENTITY): |
|
995 |
break; |
|
996 |
} |
|
997 |
this.type = ret; |
|
998 |
} |
|
999 |
||
1000 |
/** |
|
1001 |
* Returns the determinant of the matrix representation of the transform. |
|
1002 |
* The determinant is useful both to determine if the transform can |
|
1003 |
* be inverted and to get a single value representing the |
|
1004 |
* combined X and Y scaling of the transform. |
|
1005 |
* <p> |
|
1006 |
* If the determinant is non-zero, then this transform is |
|
1007 |
* invertible and the various methods that depend on the inverse |
|
1008 |
* transform do not need to throw a |
|
1009 |
* {@link NoninvertibleTransformException}. |
|
1010 |
* If the determinant is zero then this transform can not be |
|
1011 |
* inverted since the transform maps all input coordinates onto |
|
1012 |
* a line or a point. |
|
1013 |
* If the determinant is near enough to zero then inverse transform |
|
1014 |
* operations might not carry enough precision to produce meaningful |
|
1015 |
* results. |
|
1016 |
* <p> |
|
1017 |
* If this transform represents a uniform scale, as indicated by |
|
1018 |
* the <code>getType</code> method then the determinant also |
|
1019 |
* represents the square of the uniform scale factor by which all of |
|
1020 |
* the points are expanded from or contracted towards the origin. |
|
1021 |
* If this transform represents a non-uniform scale or more general |
|
1022 |
* transform then the determinant is not likely to represent a |
|
1023 |
* value useful for any purpose other than determining if inverse |
|
1024 |
* transforms are possible. |
|
1025 |
* <p> |
|
1026 |
* Mathematically, the determinant is calculated using the formula: |
|
1027 |
* <pre> |
|
1028 |
* | m00 m01 m02 | |
|
1029 |
* | m10 m11 m12 | = m00 * m11 - m01 * m10 |
|
1030 |
* | 0 0 1 | |
|
1031 |
* </pre> |
|
1032 |
* |
|
1033 |
* @return the determinant of the matrix used to transform the |
|
1034 |
* coordinates. |
|
1035 |
* @see #getType |
|
1036 |
* @see #createInverse |
|
1037 |
* @see #inverseTransform |
|
1038 |
* @see #TYPE_UNIFORM_SCALE |
|
1039 |
* @since 1.2 |
|
1040 |
*/ |
|
1041 |
public double getDeterminant() { |
|
1042 |
switch (state) { |
|
1043 |
default: |
|
1044 |
stateError(); |
|
1045 |
/* NOTREACHED */ |
|
1046 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
1047 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
1048 |
return m00 * m11 - m01 * m10; |
|
1049 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
1050 |
case (APPLY_SHEAR): |
|
1051 |
return -(m01 * m10); |
|
1052 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
1053 |
case (APPLY_SCALE): |
|
1054 |
return m00 * m11; |
|
1055 |
case (APPLY_TRANSLATE): |
|
1056 |
case (APPLY_IDENTITY): |
|
1057 |
return 1.0; |
|
1058 |
} |
|
1059 |
} |
|
1060 |
||
1061 |
/** |
|
1062 |
* Manually recalculates the state of the transform when the matrix |
|
1063 |
* changes too much to predict the effects on the state. |
|
1064 |
* The following table specifies what the various settings of the |
|
1065 |
* state field say about the values of the corresponding matrix |
|
1066 |
* element fields. |
|
1067 |
* Note that the rules governing the SCALE fields are slightly |
|
1068 |
* different depending on whether the SHEAR flag is also set. |
|
1069 |
* <pre> |
|
1070 |
* SCALE SHEAR TRANSLATE |
|
1071 |
* m00/m11 m01/m10 m02/m12 |
|
1072 |
* |
|
1073 |
* IDENTITY 1.0 0.0 0.0 |
|
1074 |
* TRANSLATE (TR) 1.0 0.0 not both 0.0 |
|
1075 |
* SCALE (SC) not both 1.0 0.0 0.0 |
|
1076 |
* TR | SC not both 1.0 0.0 not both 0.0 |
|
1077 |
* SHEAR (SH) 0.0 not both 0.0 0.0 |
|
1078 |
* TR | SH 0.0 not both 0.0 not both 0.0 |
|
1079 |
* SC | SH not both 0.0 not both 0.0 0.0 |
|
1080 |
* TR | SC | SH not both 0.0 not both 0.0 not both 0.0 |
|
1081 |
* </pre> |
|
1082 |
*/ |
|
1083 |
void updateState() { |
|
1084 |
if (m01 == 0.0 && m10 == 0.0) { |
|
1085 |
if (m00 == 1.0 && m11 == 1.0) { |
|
1086 |
if (m02 == 0.0 && m12 == 0.0) { |
|
1087 |
state = APPLY_IDENTITY; |
|
1088 |
type = TYPE_IDENTITY; |
|
1089 |
} else { |
|
1090 |
state = APPLY_TRANSLATE; |
|
1091 |
type = TYPE_TRANSLATION; |
|
1092 |
} |
|
1093 |
} else { |
|
1094 |
if (m02 == 0.0 && m12 == 0.0) { |
|
1095 |
state = APPLY_SCALE; |
|
1096 |
type = TYPE_UNKNOWN; |
|
1097 |
} else { |
|
1098 |
state = (APPLY_SCALE | APPLY_TRANSLATE); |
|
1099 |
type = TYPE_UNKNOWN; |
|
1100 |
} |
|
1101 |
} |
|
1102 |
} else { |
|
1103 |
if (m00 == 0.0 && m11 == 0.0) { |
|
1104 |
if (m02 == 0.0 && m12 == 0.0) { |
|
1105 |
state = APPLY_SHEAR; |
|
1106 |
type = TYPE_UNKNOWN; |
|
1107 |
} else { |
|
1108 |
state = (APPLY_SHEAR | APPLY_TRANSLATE); |
|
1109 |
type = TYPE_UNKNOWN; |
|
1110 |
} |
|
1111 |
} else { |
|
1112 |
if (m02 == 0.0 && m12 == 0.0) { |
|
1113 |
state = (APPLY_SHEAR | APPLY_SCALE); |
|
1114 |
type = TYPE_UNKNOWN; |
|
1115 |
} else { |
|
1116 |
state = (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE); |
|
1117 |
type = TYPE_UNKNOWN; |
|
1118 |
} |
|
1119 |
} |
|
1120 |
} |
|
1121 |
} |
|
1122 |
||
1123 |
/* |
|
1124 |
* Convenience method used internally to throw exceptions when |
|
1125 |
* a case was forgotten in a switch statement. |
|
1126 |
*/ |
|
1127 |
private void stateError() { |
|
1128 |
throw new InternalError("missing case in transform state switch"); |
|
1129 |
} |
|
1130 |
||
1131 |
/** |
|
1132 |
* Retrieves the 6 specifiable values in the 3x3 affine transformation |
|
1133 |
* matrix and places them into an array of double precisions values. |
|
1134 |
* The values are stored in the array as |
|
1135 |
* { m00 m10 m01 m11 m02 m12 }. |
|
1136 |
* An array of 4 doubles can also be specified, in which case only the |
|
1137 |
* first four elements representing the non-transform |
|
1138 |
* parts of the array are retrieved and the values are stored into |
|
1139 |
* the array as { m00 m10 m01 m11 } |
|
1140 |
* @param flatmatrix the double array used to store the returned |
|
1141 |
* values. |
|
1142 |
* @see #getScaleX |
|
1143 |
* @see #getScaleY |
|
1144 |
* @see #getShearX |
|
1145 |
* @see #getShearY |
|
1146 |
* @see #getTranslateX |
|
1147 |
* @see #getTranslateY |
|
1148 |
* @since 1.2 |
|
1149 |
*/ |
|
1150 |
public void getMatrix(double[] flatmatrix) { |
|
1151 |
flatmatrix[0] = m00; |
|
1152 |
flatmatrix[1] = m10; |
|
1153 |
flatmatrix[2] = m01; |
|
1154 |
flatmatrix[3] = m11; |
|
1155 |
if (flatmatrix.length > 5) { |
|
1156 |
flatmatrix[4] = m02; |
|
1157 |
flatmatrix[5] = m12; |
|
1158 |
} |
|
1159 |
} |
|
1160 |
||
1161 |
/** |
|
1162 |
* Returns the X coordinate scaling element (m00) of the 3x3 |
|
1163 |
* affine transformation matrix. |
|
1164 |
* @return a double value that is the X coordinate of the scaling |
|
1165 |
* element of the affine transformation matrix. |
|
1166 |
* @see #getMatrix |
|
1167 |
* @since 1.2 |
|
1168 |
*/ |
|
1169 |
public double getScaleX() { |
|
1170 |
return m00; |
|
1171 |
} |
|
1172 |
||
1173 |
/** |
|
1174 |
* Returns the Y coordinate scaling element (m11) of the 3x3 |
|
1175 |
* affine transformation matrix. |
|
1176 |
* @return a double value that is the Y coordinate of the scaling |
|
1177 |
* element of the affine transformation matrix. |
|
1178 |
* @see #getMatrix |
|
1179 |
* @since 1.2 |
|
1180 |
*/ |
|
1181 |
public double getScaleY() { |
|
1182 |
return m11; |
|
1183 |
} |
|
1184 |
||
1185 |
/** |
|
1186 |
* Returns the X coordinate shearing element (m01) of the 3x3 |
|
1187 |
* affine transformation matrix. |
|
1188 |
* @return a double value that is the X coordinate of the shearing |
|
1189 |
* element of the affine transformation matrix. |
|
1190 |
* @see #getMatrix |
|
1191 |
* @since 1.2 |
|
1192 |
*/ |
|
1193 |
public double getShearX() { |
|
1194 |
return m01; |
|
1195 |
} |
|
1196 |
||
1197 |
/** |
|
1198 |
* Returns the Y coordinate shearing element (m10) of the 3x3 |
|
1199 |
* affine transformation matrix. |
|
1200 |
* @return a double value that is the Y coordinate of the shearing |
|
1201 |
* element of the affine transformation matrix. |
|
1202 |
* @see #getMatrix |
|
1203 |
* @since 1.2 |
|
1204 |
*/ |
|
1205 |
public double getShearY() { |
|
1206 |
return m10; |
|
1207 |
} |
|
1208 |
||
1209 |
/** |
|
1210 |
* Returns the X coordinate of the translation element (m02) of the |
|
1211 |
* 3x3 affine transformation matrix. |
|
1212 |
* @return a double value that is the X coordinate of the translation |
|
1213 |
* element of the affine transformation matrix. |
|
1214 |
* @see #getMatrix |
|
1215 |
* @since 1.2 |
|
1216 |
*/ |
|
1217 |
public double getTranslateX() { |
|
1218 |
return m02; |
|
1219 |
} |
|
1220 |
||
1221 |
/** |
|
1222 |
* Returns the Y coordinate of the translation element (m12) of the |
|
1223 |
* 3x3 affine transformation matrix. |
|
1224 |
* @return a double value that is the Y coordinate of the translation |
|
1225 |
* element of the affine transformation matrix. |
|
1226 |
* @see #getMatrix |
|
1227 |
* @since 1.2 |
|
1228 |
*/ |
|
1229 |
public double getTranslateY() { |
|
1230 |
return m12; |
|
1231 |
} |
|
1232 |
||
1233 |
/** |
|
1234 |
* Concatenates this transform with a translation transformation. |
|
1235 |
* This is equivalent to calling concatenate(T), where T is an |
|
1236 |
* <code>AffineTransform</code> represented by the following matrix: |
|
1237 |
* <pre> |
|
1238 |
* [ 1 0 tx ] |
|
1239 |
* [ 0 1 ty ] |
|
1240 |
* [ 0 0 1 ] |
|
1241 |
* </pre> |
|
1242 |
* @param tx the distance by which coordinates are translated in the |
|
1243 |
* X axis direction |
|
1244 |
* @param ty the distance by which coordinates are translated in the |
|
1245 |
* Y axis direction |
|
1246 |
* @since 1.2 |
|
1247 |
*/ |
|
1248 |
public void translate(double tx, double ty) { |
|
1249 |
switch (state) { |
|
1250 |
default: |
|
1251 |
stateError(); |
|
1252 |
/* NOTREACHED */ |
|
1253 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
1254 |
m02 = tx * m00 + ty * m01 + m02; |
|
1255 |
m12 = tx * m10 + ty * m11 + m12; |
|
1256 |
if (m02 == 0.0 && m12 == 0.0) { |
|
1257 |
state = APPLY_SHEAR | APPLY_SCALE; |
|
1258 |
if (type != TYPE_UNKNOWN) { |
|
1259 |
type -= TYPE_TRANSLATION; |
|
1260 |
} |
|
1261 |
} |
|
1262 |
return; |
|
1263 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
1264 |
m02 = tx * m00 + ty * m01; |
|
1265 |
m12 = tx * m10 + ty * m11; |
|
1266 |
if (m02 != 0.0 || m12 != 0.0) { |
|
1267 |
state = APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE; |
|
1268 |
type |= TYPE_TRANSLATION; |
|
1269 |
} |
|
1270 |
return; |
|
1271 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
1272 |
m02 = ty * m01 + m02; |
|
1273 |
m12 = tx * m10 + m12; |
|
1274 |
if (m02 == 0.0 && m12 == 0.0) { |
|
1275 |
state = APPLY_SHEAR; |
|
1276 |
if (type != TYPE_UNKNOWN) { |
|
1277 |
type -= TYPE_TRANSLATION; |
|
1278 |
} |
|
1279 |
} |
|
1280 |
return; |
|
1281 |
case (APPLY_SHEAR): |
|
1282 |
m02 = ty * m01; |
|
1283 |
m12 = tx * m10; |
|
1284 |
if (m02 != 0.0 || m12 != 0.0) { |
|
1285 |
state = APPLY_SHEAR | APPLY_TRANSLATE; |
|
1286 |
type |= TYPE_TRANSLATION; |
|
1287 |
} |
|
1288 |
return; |
|
1289 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
1290 |
m02 = tx * m00 + m02; |
|
1291 |
m12 = ty * m11 + m12; |
|
1292 |
if (m02 == 0.0 && m12 == 0.0) { |
|
1293 |
state = APPLY_SCALE; |
|
1294 |
if (type != TYPE_UNKNOWN) { |
|
1295 |
type -= TYPE_TRANSLATION; |
|
1296 |
} |
|
1297 |
} |
|
1298 |
return; |
|
1299 |
case (APPLY_SCALE): |
|
1300 |
m02 = tx * m00; |
|
1301 |
m12 = ty * m11; |
|
1302 |
if (m02 != 0.0 || m12 != 0.0) { |
|
1303 |
state = APPLY_SCALE | APPLY_TRANSLATE; |
|
1304 |
type |= TYPE_TRANSLATION; |
|
1305 |
} |
|
1306 |
return; |
|
1307 |
case (APPLY_TRANSLATE): |
|
1308 |
m02 = tx + m02; |
|
1309 |
m12 = ty + m12; |
|
1310 |
if (m02 == 0.0 && m12 == 0.0) { |
|
1311 |
state = APPLY_IDENTITY; |
|
1312 |
type = TYPE_IDENTITY; |
|
1313 |
} |
|
1314 |
return; |
|
1315 |
case (APPLY_IDENTITY): |
|
1316 |
m02 = tx; |
|
1317 |
m12 = ty; |
|
1318 |
if (tx != 0.0 || ty != 0.0) { |
|
1319 |
state = APPLY_TRANSLATE; |
|
1320 |
type = TYPE_TRANSLATION; |
|
1321 |
} |
|
1322 |
return; |
|
1323 |
} |
|
1324 |
} |
|
1325 |
||
1326 |
// Utility methods to optimize rotate methods. |
|
1327 |
// These tables translate the flags during predictable quadrant |
|
1328 |
// rotations where the shear and scale values are swapped and negated. |
|
1329 |
private static final int rot90conversion[] = { |
|
1330 |
/* IDENTITY => */ APPLY_SHEAR, |
|
1331 |
/* TRANSLATE (TR) => */ APPLY_SHEAR | APPLY_TRANSLATE, |
|
1332 |
/* SCALE (SC) => */ APPLY_SHEAR, |
|
1333 |
/* SC | TR => */ APPLY_SHEAR | APPLY_TRANSLATE, |
|
1334 |
/* SHEAR (SH) => */ APPLY_SCALE, |
|
1335 |
/* SH | TR => */ APPLY_SCALE | APPLY_TRANSLATE, |
|
1336 |
/* SH | SC => */ APPLY_SHEAR | APPLY_SCALE, |
|
1337 |
/* SH | SC | TR => */ APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE, |
|
1338 |
}; |
|
1339 |
private final void rotate90() { |
|
1340 |
double M0 = m00; |
|
1341 |
m00 = m01; |
|
1342 |
m01 = -M0; |
|
1343 |
M0 = m10; |
|
1344 |
m10 = m11; |
|
1345 |
m11 = -M0; |
|
1346 |
int state = rot90conversion[this.state]; |
|
1347 |
if ((state & (APPLY_SHEAR | APPLY_SCALE)) == APPLY_SCALE && |
|
1348 |
m00 == 1.0 && m11 == 1.0) |
|
1349 |
{ |
|
1350 |
state -= APPLY_SCALE; |
|
1351 |
} |
|
1352 |
this.state = state; |
|
1353 |
type = TYPE_UNKNOWN; |
|
1354 |
} |
|
1355 |
private final void rotate180() { |
|
1356 |
m00 = -m00; |
|
1357 |
m11 = -m11; |
|
1358 |
int state = this.state; |
|
1359 |
if ((state & (APPLY_SHEAR)) != 0) { |
|
1360 |
// If there was a shear, then this rotation has no |
|
1361 |
// effect on the state. |
|
1362 |
m01 = -m01; |
|
1363 |
m10 = -m10; |
|
1364 |
} else { |
|
1365 |
// No shear means the SCALE state may toggle when |
|
1366 |
// m00 and m11 are negated. |
|
1367 |
if (m00 == 1.0 && m11 == 1.0) { |
|
1368 |
this.state = state & ~APPLY_SCALE; |
|
1369 |
} else { |
|
1370 |
this.state = state | APPLY_SCALE; |
|
1371 |
} |
|
1372 |
} |
|
1373 |
type = TYPE_UNKNOWN; |
|
1374 |
} |
|
1375 |
private final void rotate270() { |
|
1376 |
double M0 = m00; |
|
1377 |
m00 = -m01; |
|
1378 |
m01 = M0; |
|
1379 |
M0 = m10; |
|
1380 |
m10 = -m11; |
|
1381 |
m11 = M0; |
|
1382 |
int state = rot90conversion[this.state]; |
|
1383 |
if ((state & (APPLY_SHEAR | APPLY_SCALE)) == APPLY_SCALE && |
|
1384 |
m00 == 1.0 && m11 == 1.0) |
|
1385 |
{ |
|
1386 |
state -= APPLY_SCALE; |
|
1387 |
} |
|
1388 |
this.state = state; |
|
1389 |
type = TYPE_UNKNOWN; |
|
1390 |
} |
|
1391 |
||
1392 |
/** |
|
1393 |
* Concatenates this transform with a rotation transformation. |
|
1394 |
* This is equivalent to calling concatenate(R), where R is an |
|
1395 |
* <code>AffineTransform</code> represented by the following matrix: |
|
1396 |
* <pre> |
|
1397 |
* [ cos(theta) -sin(theta) 0 ] |
|
1398 |
* [ sin(theta) cos(theta) 0 ] |
|
1399 |
* [ 0 0 1 ] |
|
1400 |
* </pre> |
|
1401 |
* Rotating by a positive angle theta rotates points on the positive |
|
1402 |
* X axis toward the positive Y axis. |
|
1403 |
* Note also the discussion of |
|
1404 |
* <a href="#quadrantapproximation">Handling 90-Degree Rotations</a> |
|
1405 |
* above. |
|
1406 |
* @param theta the angle of rotation measured in radians |
|
1407 |
* @since 1.2 |
|
1408 |
*/ |
|
1409 |
public void rotate(double theta) { |
|
1410 |
double sin = Math.sin(theta); |
|
1411 |
if (sin == 1.0) { |
|
1412 |
rotate90(); |
|
1413 |
} else if (sin == -1.0) { |
|
1414 |
rotate270(); |
|
1415 |
} else { |
|
1416 |
double cos = Math.cos(theta); |
|
1417 |
if (cos == -1.0) { |
|
1418 |
rotate180(); |
|
1419 |
} else if (cos != 1.0) { |
|
1420 |
double M0, M1; |
|
1421 |
M0 = m00; |
|
1422 |
M1 = m01; |
|
1423 |
m00 = cos * M0 + sin * M1; |
|
1424 |
m01 = -sin * M0 + cos * M1; |
|
1425 |
M0 = m10; |
|
1426 |
M1 = m11; |
|
1427 |
m10 = cos * M0 + sin * M1; |
|
1428 |
m11 = -sin * M0 + cos * M1; |
|
1429 |
updateState(); |
|
1430 |
} |
|
1431 |
} |
|
1432 |
} |
|
1433 |
||
1434 |
/** |
|
1435 |
* Concatenates this transform with a transform that rotates |
|
1436 |
* coordinates around an anchor point. |
|
1437 |
* This operation is equivalent to translating the coordinates so |
|
1438 |
* that the anchor point is at the origin (S1), then rotating them |
|
1439 |
* about the new origin (S2), and finally translating so that the |
|
1440 |
* intermediate origin is restored to the coordinates of the original |
|
1441 |
* anchor point (S3). |
|
1442 |
* <p> |
|
1443 |
* This operation is equivalent to the following sequence of calls: |
|
1444 |
* <pre> |
|
1445 |
* translate(anchorx, anchory); // S3: final translation |
|
1446 |
* rotate(theta); // S2: rotate around anchor |
|
1447 |
* translate(-anchorx, -anchory); // S1: translate anchor to origin |
|
1448 |
* </pre> |
|
1449 |
* Rotating by a positive angle theta rotates points on the positive |
|
1450 |
* X axis toward the positive Y axis. |
|
1451 |
* Note also the discussion of |
|
1452 |
* <a href="#quadrantapproximation">Handling 90-Degree Rotations</a> |
|
1453 |
* above. |
|
1454 |
* |
|
1455 |
* @param theta the angle of rotation measured in radians |
|
1456 |
* @param anchorx the X coordinate of the rotation anchor point |
|
1457 |
* @param anchory the Y coordinate of the rotation anchor point |
|
1458 |
* @since 1.2 |
|
1459 |
*/ |
|
1460 |
public void rotate(double theta, double anchorx, double anchory) { |
|
1461 |
// REMIND: Simple for now - optimize later |
|
1462 |
translate(anchorx, anchory); |
|
1463 |
rotate(theta); |
|
1464 |
translate(-anchorx, -anchory); |
|
1465 |
} |
|
1466 |
||
1467 |
/** |
|
1468 |
* Concatenates this transform with a transform that rotates |
|
1469 |
* coordinates according to a rotation vector. |
|
1470 |
* All coordinates rotate about the origin by the same amount. |
|
1471 |
* The amount of rotation is such that coordinates along the former |
|
1472 |
* positive X axis will subsequently align with the vector pointing |
|
1473 |
* from the origin to the specified vector coordinates. |
|
1474 |
* If both <code>vecx</code> and <code>vecy</code> are 0.0, |
|
1475 |
* no additional rotation is added to this transform. |
|
1476 |
* This operation is equivalent to calling: |
|
1477 |
* <pre> |
|
1478 |
* rotate(Math.atan2(vecy, vecx)); |
|
1479 |
* </pre> |
|
1480 |
* |
|
1481 |
* @param vecx the X coordinate of the rotation vector |
|
1482 |
* @param vecy the Y coordinate of the rotation vector |
|
1483 |
* @since 1.6 |
|
1484 |
*/ |
|
1485 |
public void rotate(double vecx, double vecy) { |
|
1486 |
if (vecy == 0.0) { |
|
1487 |
if (vecx < 0.0) { |
|
1488 |
rotate180(); |
|
1489 |
} |
|
1490 |
// If vecx > 0.0 - no rotation |
|
1491 |
// If vecx == 0.0 - undefined rotation - treat as no rotation |
|
1492 |
} else if (vecx == 0.0) { |
|
1493 |
if (vecy > 0.0) { |
|
1494 |
rotate90(); |
|
1495 |
} else { // vecy must be < 0.0 |
|
1496 |
rotate270(); |
|
1497 |
} |
|
1498 |
} else { |
|
1499 |
double len = Math.sqrt(vecx * vecx + vecy * vecy); |
|
1500 |
double sin = vecy / len; |
|
1501 |
double cos = vecx / len; |
|
1502 |
double M0, M1; |
|
1503 |
M0 = m00; |
|
1504 |
M1 = m01; |
|
1505 |
m00 = cos * M0 + sin * M1; |
|
1506 |
m01 = -sin * M0 + cos * M1; |
|
1507 |
M0 = m10; |
|
1508 |
M1 = m11; |
|
1509 |
m10 = cos * M0 + sin * M1; |
|
1510 |
m11 = -sin * M0 + cos * M1; |
|
1511 |
updateState(); |
|
1512 |
} |
|
1513 |
} |
|
1514 |
||
1515 |
/** |
|
1516 |
* Concatenates this transform with a transform that rotates |
|
1517 |
* coordinates around an anchor point according to a rotation |
|
1518 |
* vector. |
|
1519 |
* All coordinates rotate about the specified anchor coordinates |
|
1520 |
* by the same amount. |
|
1521 |
* The amount of rotation is such that coordinates along the former |
|
1522 |
* positive X axis will subsequently align with the vector pointing |
|
1523 |
* from the origin to the specified vector coordinates. |
|
1524 |
* If both <code>vecx</code> and <code>vecy</code> are 0.0, |
|
1525 |
* the transform is not modified in any way. |
|
1526 |
* This method is equivalent to calling: |
|
1527 |
* <pre> |
|
1528 |
* rotate(Math.atan2(vecy, vecx), anchorx, anchory); |
|
1529 |
* </pre> |
|
1530 |
* |
|
1531 |
* @param vecx the X coordinate of the rotation vector |
|
1532 |
* @param vecy the Y coordinate of the rotation vector |
|
1533 |
* @param anchorx the X coordinate of the rotation anchor point |
|
1534 |
* @param anchory the Y coordinate of the rotation anchor point |
|
1535 |
* @since 1.6 |
|
1536 |
*/ |
|
1537 |
public void rotate(double vecx, double vecy, |
|
1538 |
double anchorx, double anchory) |
|
1539 |
{ |
|
1540 |
// REMIND: Simple for now - optimize later |
|
1541 |
translate(anchorx, anchory); |
|
1542 |
rotate(vecx, vecy); |
|
1543 |
translate(-anchorx, -anchory); |
|
1544 |
} |
|
1545 |
||
1546 |
/** |
|
1547 |
* Concatenates this transform with a transform that rotates |
|
1548 |
* coordinates by the specified number of quadrants. |
|
1549 |
* This is equivalent to calling: |
|
1550 |
* <pre> |
|
1551 |
* rotate(numquadrants * Math.PI / 2.0); |
|
1552 |
* </pre> |
|
1553 |
* Rotating by a positive number of quadrants rotates points on |
|
1554 |
* the positive X axis toward the positive Y axis. |
|
1555 |
* @param numquadrants the number of 90 degree arcs to rotate by |
|
1556 |
* @since 1.6 |
|
1557 |
*/ |
|
1558 |
public void quadrantRotate(int numquadrants) { |
|
1559 |
switch (numquadrants & 3) { |
|
1560 |
case 0: |
|
1561 |
break; |
|
1562 |
case 1: |
|
1563 |
rotate90(); |
|
1564 |
break; |
|
1565 |
case 2: |
|
1566 |
rotate180(); |
|
1567 |
break; |
|
1568 |
case 3: |
|
1569 |
rotate270(); |
|
1570 |
break; |
|
1571 |
} |
|
1572 |
} |
|
1573 |
||
1574 |
/** |
|
1575 |
* Concatenates this transform with a transform that rotates |
|
1576 |
* coordinates by the specified number of quadrants around |
|
1577 |
* the specified anchor point. |
|
1578 |
* This method is equivalent to calling: |
|
1579 |
* <pre> |
|
1580 |
* rotate(numquadrants * Math.PI / 2.0, anchorx, anchory); |
|
1581 |
* </pre> |
|
1582 |
* Rotating by a positive number of quadrants rotates points on |
|
1583 |
* the positive X axis toward the positive Y axis. |
|
1584 |
* |
|
1585 |
* @param numquadrants the number of 90 degree arcs to rotate by |
|
1586 |
* @param anchorx the X coordinate of the rotation anchor point |
|
1587 |
* @param anchory the Y coordinate of the rotation anchor point |
|
1588 |
* @since 1.6 |
|
1589 |
*/ |
|
1590 |
public void quadrantRotate(int numquadrants, |
|
1591 |
double anchorx, double anchory) |
|
1592 |
{ |
|
1593 |
switch (numquadrants & 3) { |
|
1594 |
case 0: |
|
1595 |
return; |
|
1596 |
case 1: |
|
1597 |
m02 += anchorx * (m00 - m01) + anchory * (m01 + m00); |
|
1598 |
m12 += anchorx * (m10 - m11) + anchory * (m11 + m10); |
|
1599 |
rotate90(); |
|
1600 |
break; |
|
1601 |
case 2: |
|
1602 |
m02 += anchorx * (m00 + m00) + anchory * (m01 + m01); |
|
1603 |
m12 += anchorx * (m10 + m10) + anchory * (m11 + m11); |
|
1604 |
rotate180(); |
|
1605 |
break; |
|
1606 |
case 3: |
|
1607 |
m02 += anchorx * (m00 + m01) + anchory * (m01 - m00); |
|
1608 |
m12 += anchorx * (m10 + m11) + anchory * (m11 - m10); |
|
1609 |
rotate270(); |
|
1610 |
break; |
|
1611 |
} |
|
1612 |
if (m02 == 0.0 && m12 == 0.0) { |
|
1613 |
state &= ~APPLY_TRANSLATE; |
|
1614 |
} else { |
|
1615 |
state |= APPLY_TRANSLATE; |
|
1616 |
} |
|
1617 |
} |
|
1618 |
||
1619 |
/** |
|
1620 |
* Concatenates this transform with a scaling transformation. |
|
1621 |
* This is equivalent to calling concatenate(S), where S is an |
|
1622 |
* <code>AffineTransform</code> represented by the following matrix: |
|
1623 |
* <pre> |
|
1624 |
* [ sx 0 0 ] |
|
1625 |
* [ 0 sy 0 ] |
|
1626 |
* [ 0 0 1 ] |
|
1627 |
* </pre> |
|
1628 |
* @param sx the factor by which coordinates are scaled along the |
|
1629 |
* X axis direction |
|
1630 |
* @param sy the factor by which coordinates are scaled along the |
|
1631 |
* Y axis direction |
|
1632 |
* @since 1.2 |
|
1633 |
*/ |
|
1634 |
public void scale(double sx, double sy) { |
|
1635 |
int state = this.state; |
|
1636 |
switch (state) { |
|
1637 |
default: |
|
1638 |
stateError(); |
|
1639 |
/* NOTREACHED */ |
|
1640 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
1641 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
1642 |
m00 *= sx; |
|
1643 |
m11 *= sy; |
|
1644 |
/* NOBREAK */ |
|
1645 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
1646 |
case (APPLY_SHEAR): |
|
1647 |
m01 *= sy; |
|
1648 |
m10 *= sx; |
|
1649 |
if (m01 == 0 && m10 == 0) { |
|
1650 |
state &= APPLY_TRANSLATE; |
|
1651 |
if (m00 == 1.0 && m11 == 1.0) { |
|
1652 |
this.type = (state == APPLY_IDENTITY |
|
1653 |
? TYPE_IDENTITY |
|
1654 |
: TYPE_TRANSLATION); |
|
1655 |
} else { |
|
1656 |
state |= APPLY_SCALE; |
|
1657 |
this.type = TYPE_UNKNOWN; |
|
1658 |
} |
|
1659 |
this.state = state; |
|
1660 |
} |
|
1661 |
return; |
|
1662 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
1663 |
case (APPLY_SCALE): |
|
1664 |
m00 *= sx; |
|
1665 |
m11 *= sy; |
|
1666 |
if (m00 == 1.0 && m11 == 1.0) { |
|
1667 |
this.state = (state &= APPLY_TRANSLATE); |
|
1668 |
this.type = (state == APPLY_IDENTITY |
|
1669 |
? TYPE_IDENTITY |
|
1670 |
: TYPE_TRANSLATION); |
|
1671 |
} else { |
|
1672 |
this.type = TYPE_UNKNOWN; |
|
1673 |
} |
|
1674 |
return; |
|
1675 |
case (APPLY_TRANSLATE): |
|
1676 |
case (APPLY_IDENTITY): |
|
1677 |
m00 = sx; |
|
1678 |
m11 = sy; |
|
1679 |
if (sx != 1.0 || sy != 1.0) { |
|
1680 |
this.state = state | APPLY_SCALE; |
|
1681 |
this.type = TYPE_UNKNOWN; |
|
1682 |
} |
|
1683 |
return; |
|
1684 |
} |
|
1685 |
} |
|
1686 |
||
1687 |
/** |
|
1688 |
* Concatenates this transform with a shearing transformation. |
|
1689 |
* This is equivalent to calling concatenate(SH), where SH is an |
|
1690 |
* <code>AffineTransform</code> represented by the following matrix: |
|
1691 |
* <pre> |
|
1692 |
* [ 1 shx 0 ] |
|
1693 |
* [ shy 1 0 ] |
|
1694 |
* [ 0 0 1 ] |
|
1695 |
* </pre> |
|
1696 |
* @param shx the multiplier by which coordinates are shifted in the |
|
1697 |
* direction of the positive X axis as a factor of their Y coordinate |
|
1698 |
* @param shy the multiplier by which coordinates are shifted in the |
|
1699 |
* direction of the positive Y axis as a factor of their X coordinate |
|
1700 |
* @since 1.2 |
|
1701 |
*/ |
|
1702 |
public void shear(double shx, double shy) { |
|
1703 |
int state = this.state; |
|
1704 |
switch (state) { |
|
1705 |
default: |
|
1706 |
stateError(); |
|
1707 |
/* NOTREACHED */ |
|
1708 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
1709 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
1710 |
double M0, M1; |
|
1711 |
M0 = m00; |
|
1712 |
M1 = m01; |
|
1713 |
m00 = M0 + M1 * shy; |
|
1714 |
m01 = M0 * shx + M1; |
|
1715 |
||
1716 |
M0 = m10; |
|
1717 |
M1 = m11; |
|
1718 |
m10 = M0 + M1 * shy; |
|
1719 |
m11 = M0 * shx + M1; |
|
1720 |
updateState(); |
|
1721 |
return; |
|
1722 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
1723 |
case (APPLY_SHEAR): |
|
1724 |
m00 = m01 * shy; |
|
1725 |
m11 = m10 * shx; |
|
1726 |
if (m00 != 0.0 || m11 != 0.0) { |
|
1727 |
this.state = state | APPLY_SCALE; |
|
1728 |
} |
|
1729 |
this.type = TYPE_UNKNOWN; |
|
1730 |
return; |
|
1731 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
1732 |
case (APPLY_SCALE): |
|
1733 |
m01 = m00 * shx; |
|
1734 |
m10 = m11 * shy; |
|
1735 |
if (m01 != 0.0 || m10 != 0.0) { |
|
1736 |
this.state = state | APPLY_SHEAR; |
|
1737 |
} |
|
1738 |
this.type = TYPE_UNKNOWN; |
|
1739 |
return; |
|
1740 |
case (APPLY_TRANSLATE): |
|
1741 |
case (APPLY_IDENTITY): |
|
1742 |
m01 = shx; |
|
1743 |
m10 = shy; |
|
1744 |
if (m01 != 0.0 || m10 != 0.0) { |
|
1745 |
this.state = state | APPLY_SCALE | APPLY_SHEAR; |
|
1746 |
this.type = TYPE_UNKNOWN; |
|
1747 |
} |
|
1748 |
return; |
|
1749 |
} |
|
1750 |
} |
|
1751 |
||
1752 |
/** |
|
1753 |
* Resets this transform to the Identity transform. |
|
1754 |
* @since 1.2 |
|
1755 |
*/ |
|
1756 |
public void setToIdentity() { |
|
1757 |
m00 = m11 = 1.0; |
|
1758 |
m10 = m01 = m02 = m12 = 0.0; |
|
1759 |
state = APPLY_IDENTITY; |
|
1760 |
type = TYPE_IDENTITY; |
|
1761 |
} |
|
1762 |
||
1763 |
/** |
|
1764 |
* Sets this transform to a translation transformation. |
|
1765 |
* The matrix representing this transform becomes: |
|
1766 |
* <pre> |
|
1767 |
* [ 1 0 tx ] |
|
1768 |
* [ 0 1 ty ] |
|
1769 |
* [ 0 0 1 ] |
|
1770 |
* </pre> |
|
1771 |
* @param tx the distance by which coordinates are translated in the |
|
1772 |
* X axis direction |
|
1773 |
* @param ty the distance by which coordinates are translated in the |
|
1774 |
* Y axis direction |
|
1775 |
* @since 1.2 |
|
1776 |
*/ |
|
1777 |
public void setToTranslation(double tx, double ty) { |
|
1778 |
m00 = 1.0; |
|
1779 |
m10 = 0.0; |
|
1780 |
m01 = 0.0; |
|
1781 |
m11 = 1.0; |
|
1782 |
m02 = tx; |
|
1783 |
m12 = ty; |
|
1784 |
if (tx != 0.0 || ty != 0.0) { |
|
1785 |
state = APPLY_TRANSLATE; |
|
1786 |
type = TYPE_TRANSLATION; |
|
1787 |
} else { |
|
1788 |
state = APPLY_IDENTITY; |
|
1789 |
type = TYPE_IDENTITY; |
|
1790 |
} |
|
1791 |
} |
|
1792 |
||
1793 |
/** |
|
1794 |
* Sets this transform to a rotation transformation. |
|
1795 |
* The matrix representing this transform becomes: |
|
1796 |
* <pre> |
|
1797 |
* [ cos(theta) -sin(theta) 0 ] |
|
1798 |
* [ sin(theta) cos(theta) 0 ] |
|
1799 |
* [ 0 0 1 ] |
|
1800 |
* </pre> |
|
1801 |
* Rotating by a positive angle theta rotates points on the positive |
|
1802 |
* X axis toward the positive Y axis. |
|
1803 |
* Note also the discussion of |
|
1804 |
* <a href="#quadrantapproximation">Handling 90-Degree Rotations</a> |
|
1805 |
* above. |
|
1806 |
* @param theta the angle of rotation measured in radians |
|
1807 |
* @since 1.2 |
|
1808 |
*/ |
|
1809 |
public void setToRotation(double theta) { |
|
1810 |
double sin = Math.sin(theta); |
|
1811 |
double cos; |
|
1812 |
if (sin == 1.0 || sin == -1.0) { |
|
1813 |
cos = 0.0; |
|
1814 |
state = APPLY_SHEAR; |
|
1815 |
type = TYPE_QUADRANT_ROTATION; |
|
1816 |
} else { |
|
1817 |
cos = Math.cos(theta); |
|
1818 |
if (cos == -1.0) { |
|
1819 |
sin = 0.0; |
|
1820 |
state = APPLY_SCALE; |
|
1821 |
type = TYPE_QUADRANT_ROTATION; |
|
1822 |
} else if (cos == 1.0) { |
|
1823 |
sin = 0.0; |
|
1824 |
state = APPLY_IDENTITY; |
|
1825 |
type = TYPE_IDENTITY; |
|
1826 |
} else { |
|
1827 |
state = APPLY_SHEAR | APPLY_SCALE; |
|
1828 |
type = TYPE_GENERAL_ROTATION; |
|
1829 |
} |
|
1830 |
} |
|
1831 |
m00 = cos; |
|
1832 |
m10 = sin; |
|
1833 |
m01 = -sin; |
|
1834 |
m11 = cos; |
|
1835 |
m02 = 0.0; |
|
1836 |
m12 = 0.0; |
|
1837 |
} |
|
1838 |
||
1839 |
/** |
|
1840 |
* Sets this transform to a translated rotation transformation. |
|
1841 |
* This operation is equivalent to translating the coordinates so |
|
1842 |
* that the anchor point is at the origin (S1), then rotating them |
|
1843 |
* about the new origin (S2), and finally translating so that the |
|
1844 |
* intermediate origin is restored to the coordinates of the original |
|
1845 |
* anchor point (S3). |
|
1846 |
* <p> |
|
1847 |
* This operation is equivalent to the following sequence of calls: |
|
1848 |
* <pre> |
|
1849 |
* setToTranslation(anchorx, anchory); // S3: final translation |
|
1850 |
* rotate(theta); // S2: rotate around anchor |
|
1851 |
* translate(-anchorx, -anchory); // S1: translate anchor to origin |
|
1852 |
* </pre> |
|
1853 |
* The matrix representing this transform becomes: |
|
1854 |
* <pre> |
|
1855 |
* [ cos(theta) -sin(theta) x-x*cos+y*sin ] |
|
1856 |
* [ sin(theta) cos(theta) y-x*sin-y*cos ] |
|
1857 |
* [ 0 0 1 ] |
|
1858 |
* </pre> |
|
1859 |
* Rotating by a positive angle theta rotates points on the positive |
|
1860 |
* X axis toward the positive Y axis. |
|
1861 |
* Note also the discussion of |
|
1862 |
* <a href="#quadrantapproximation">Handling 90-Degree Rotations</a> |
|
1863 |
* above. |
|
1864 |
* |
|
1865 |
* @param theta the angle of rotation measured in radians |
|
1866 |
* @param anchorx the X coordinate of the rotation anchor point |
|
1867 |
* @param anchory the Y coordinate of the rotation anchor point |
|
1868 |
* @since 1.2 |
|
1869 |
*/ |
|
1870 |
public void setToRotation(double theta, double anchorx, double anchory) { |
|
1871 |
setToRotation(theta); |
|
1872 |
double sin = m10; |
|
1873 |
double oneMinusCos = 1.0 - m00; |
|
1874 |
m02 = anchorx * oneMinusCos + anchory * sin; |
|
1875 |
m12 = anchory * oneMinusCos - anchorx * sin; |
|
1876 |
if (m02 != 0.0 || m12 != 0.0) { |
|
1877 |
state |= APPLY_TRANSLATE; |
|
1878 |
type |= TYPE_TRANSLATION; |
|
1879 |
} |
|
1880 |
} |
|
1881 |
||
1882 |
/** |
|
1883 |
* Sets this transform to a rotation transformation that rotates |
|
1884 |
* coordinates according to a rotation vector. |
|
1885 |
* All coordinates rotate about the origin by the same amount. |
|
1886 |
* The amount of rotation is such that coordinates along the former |
|
1887 |
* positive X axis will subsequently align with the vector pointing |
|
1888 |
* from the origin to the specified vector coordinates. |
|
1889 |
* If both <code>vecx</code> and <code>vecy</code> are 0.0, |
|
1890 |
* the transform is set to an identity transform. |
|
1891 |
* This operation is equivalent to calling: |
|
1892 |
* <pre> |
|
1893 |
* setToRotation(Math.atan2(vecy, vecx)); |
|
1894 |
* </pre> |
|
1895 |
* |
|
1896 |
* @param vecx the X coordinate of the rotation vector |
|
1897 |
* @param vecy the Y coordinate of the rotation vector |
|
1898 |
* @since 1.6 |
|
1899 |
*/ |
|
1900 |
public void setToRotation(double vecx, double vecy) { |
|
1901 |
double sin, cos; |
|
1902 |
if (vecy == 0) { |
|
1903 |
sin = 0.0; |
|
1904 |
if (vecx < 0.0) { |
|
1905 |
cos = -1.0; |
|
1906 |
state = APPLY_SCALE; |
|
1907 |
type = TYPE_QUADRANT_ROTATION; |
|
1908 |
} else { |
|
1909 |
cos = 1.0; |
|
1910 |
state = APPLY_IDENTITY; |
|
1911 |
type = TYPE_IDENTITY; |
|
1912 |
} |
|
1913 |
} else if (vecx == 0) { |
|
1914 |
cos = 0.0; |
|
1915 |
sin = (vecy > 0.0) ? 1.0 : -1.0; |
|
1916 |
state = APPLY_SHEAR; |
|
1917 |
type = TYPE_QUADRANT_ROTATION; |
|
1918 |
} else { |
|
1919 |
double len = Math.sqrt(vecx * vecx + vecy * vecy); |
|
1920 |
cos = vecx / len; |
|
1921 |
sin = vecy / len; |
|
1922 |
state = APPLY_SHEAR | APPLY_SCALE; |
|
1923 |
type = TYPE_GENERAL_ROTATION; |
|
1924 |
} |
|
1925 |
m00 = cos; |
|
1926 |
m10 = sin; |
|
1927 |
m01 = -sin; |
|
1928 |
m11 = cos; |
|
1929 |
m02 = 0.0; |
|
1930 |
m12 = 0.0; |
|
1931 |
} |
|
1932 |
||
1933 |
/** |
|
1934 |
* Sets this transform to a rotation transformation that rotates |
|
1935 |
* coordinates around an anchor point according to a rotation |
|
1936 |
* vector. |
|
1937 |
* All coordinates rotate about the specified anchor coordinates |
|
1938 |
* by the same amount. |
|
1939 |
* The amount of rotation is such that coordinates along the former |
|
1940 |
* positive X axis will subsequently align with the vector pointing |
|
1941 |
* from the origin to the specified vector coordinates. |
|
1942 |
* If both <code>vecx</code> and <code>vecy</code> are 0.0, |
|
1943 |
* the transform is set to an identity transform. |
|
1944 |
* This operation is equivalent to calling: |
|
1945 |
* <pre> |
|
1946 |
* setToTranslation(Math.atan2(vecy, vecx), anchorx, anchory); |
|
1947 |
* </pre> |
|
1948 |
* |
|
1949 |
* @param vecx the X coordinate of the rotation vector |
|
1950 |
* @param vecy the Y coordinate of the rotation vector |
|
1951 |
* @param anchorx the X coordinate of the rotation anchor point |
|
1952 |
* @param anchory the Y coordinate of the rotation anchor point |
|
1953 |
* @since 1.6 |
|
1954 |
*/ |
|
1955 |
public void setToRotation(double vecx, double vecy, |
|
1956 |
double anchorx, double anchory) |
|
1957 |
{ |
|
1958 |
setToRotation(vecx, vecy); |
|
1959 |
double sin = m10; |
|
1960 |
double oneMinusCos = 1.0 - m00; |
|
1961 |
m02 = anchorx * oneMinusCos + anchory * sin; |
|
1962 |
m12 = anchory * oneMinusCos - anchorx * sin; |
|
1963 |
if (m02 != 0.0 || m12 != 0.0) { |
|
1964 |
state |= APPLY_TRANSLATE; |
|
1965 |
type |= TYPE_TRANSLATION; |
|
1966 |
} |
|
1967 |
} |
|
1968 |
||
1969 |
/** |
|
1970 |
* Sets this transform to a rotation transformation that rotates |
|
1971 |
* coordinates by the specified number of quadrants. |
|
1972 |
* This operation is equivalent to calling: |
|
1973 |
* <pre> |
|
1974 |
* setToRotation(numquadrants * Math.PI / 2.0); |
|
1975 |
* </pre> |
|
1976 |
* Rotating by a positive number of quadrants rotates points on |
|
1977 |
* the positive X axis toward the positive Y axis. |
|
1978 |
* @param numquadrants the number of 90 degree arcs to rotate by |
|
1979 |
* @since 1.6 |
|
1980 |
*/ |
|
1981 |
public void setToQuadrantRotation(int numquadrants) { |
|
1982 |
switch (numquadrants & 3) { |
|
1983 |
case 0: |
|
1984 |
m00 = 1.0; |
|
1985 |
m10 = 0.0; |
|
1986 |
m01 = 0.0; |
|
1987 |
m11 = 1.0; |
|
1988 |
m02 = 0.0; |
|
1989 |
m12 = 0.0; |
|
1990 |
state = APPLY_IDENTITY; |
|
1991 |
type = TYPE_IDENTITY; |
|
1992 |
break; |
|
1993 |
case 1: |
|
1994 |
m00 = 0.0; |
|
1995 |
m10 = 1.0; |
|
1996 |
m01 = -1.0; |
|
1997 |
m11 = 0.0; |
|
1998 |
m02 = 0.0; |
|
1999 |
m12 = 0.0; |
|
2000 |
state = APPLY_SHEAR; |
|
2001 |
type = TYPE_QUADRANT_ROTATION; |
|
2002 |
break; |
|
2003 |
case 2: |
|
2004 |
m00 = -1.0; |
|
2005 |
m10 = 0.0; |
|
2006 |
m01 = 0.0; |
|
2007 |
m11 = -1.0; |
|
2008 |
m02 = 0.0; |
|
2009 |
m12 = 0.0; |
|
2010 |
state = APPLY_SCALE; |
|
2011 |
type = TYPE_QUADRANT_ROTATION; |
|
2012 |
break; |
|
2013 |
case 3: |
|
2014 |
m00 = 0.0; |
|
2015 |
m10 = -1.0; |
|
2016 |
m01 = 1.0; |
|
2017 |
m11 = 0.0; |
|
2018 |
m02 = 0.0; |
|
2019 |
m12 = 0.0; |
|
2020 |
state = APPLY_SHEAR; |
|
2021 |
type = TYPE_QUADRANT_ROTATION; |
|
2022 |
break; |
|
2023 |
} |
|
2024 |
} |
|
2025 |
||
2026 |
/** |
|
2027 |
* Sets this transform to a translated rotation transformation |
|
2028 |
* that rotates coordinates by the specified number of quadrants |
|
2029 |
* around the specified anchor point. |
|
2030 |
* This operation is equivalent to calling: |
|
2031 |
* <pre> |
|
2032 |
* setToRotation(numquadrants * Math.PI / 2.0, anchorx, anchory); |
|
2033 |
* </pre> |
|
2034 |
* Rotating by a positive number of quadrants rotates points on |
|
2035 |
* the positive X axis toward the positive Y axis. |
|
2036 |
* |
|
2037 |
* @param numquadrants the number of 90 degree arcs to rotate by |
|
2038 |
* @param anchorx the X coordinate of the rotation anchor point |
|
2039 |
* @param anchory the Y coordinate of the rotation anchor point |
|
2040 |
* @since 1.6 |
|
2041 |
*/ |
|
2042 |
public void setToQuadrantRotation(int numquadrants, |
|
2043 |
double anchorx, double anchory) |
|
2044 |
{ |
|
2045 |
switch (numquadrants & 3) { |
|
2046 |
case 0: |
|
2047 |
m00 = 1.0; |
|
2048 |
m10 = 0.0; |
|
2049 |
m01 = 0.0; |
|
2050 |
m11 = 1.0; |
|
2051 |
m02 = 0.0; |
|
2052 |
m12 = 0.0; |
|
2053 |
state = APPLY_IDENTITY; |
|
2054 |
type = TYPE_IDENTITY; |
|
2055 |
break; |
|
2056 |
case 1: |
|
2057 |
m00 = 0.0; |
|
2058 |
m10 = 1.0; |
|
2059 |
m01 = -1.0; |
|
2060 |
m11 = 0.0; |
|
2061 |
m02 = anchorx + anchory; |
|
2062 |
m12 = anchory - anchorx; |
|
2063 |
if (m02 == 0.0 && m12 == 0.0) { |
|
2064 |
state = APPLY_SHEAR; |
|
2065 |
type = TYPE_QUADRANT_ROTATION; |
|
2066 |
} else { |
|
2067 |
state = APPLY_SHEAR | APPLY_TRANSLATE; |
|
2068 |
type = TYPE_QUADRANT_ROTATION | TYPE_TRANSLATION; |
|
2069 |
} |
|
2070 |
break; |
|
2071 |
case 2: |
|
2072 |
m00 = -1.0; |
|
2073 |
m10 = 0.0; |
|
2074 |
m01 = 0.0; |
|
2075 |
m11 = -1.0; |
|
2076 |
m02 = anchorx + anchorx; |
|
2077 |
m12 = anchory + anchory; |
|
2078 |
if (m02 == 0.0 && m12 == 0.0) { |
|
2079 |
state = APPLY_SCALE; |
|
2080 |
type = TYPE_QUADRANT_ROTATION; |
|
2081 |
} else { |
|
2082 |
state = APPLY_SCALE | APPLY_TRANSLATE; |
|
2083 |
type = TYPE_QUADRANT_ROTATION | TYPE_TRANSLATION; |
|
2084 |
} |
|
2085 |
break; |
|
2086 |
case 3: |
|
2087 |
m00 = 0.0; |
|
2088 |
m10 = -1.0; |
|
2089 |
m01 = 1.0; |
|
2090 |
m11 = 0.0; |
|
2091 |
m02 = anchorx - anchory; |
|
2092 |
m12 = anchory + anchorx; |
|
2093 |
if (m02 == 0.0 && m12 == 0.0) { |
|
2094 |
state = APPLY_SHEAR; |
|
2095 |
type = TYPE_QUADRANT_ROTATION; |
|
2096 |
} else { |
|
2097 |
state = APPLY_SHEAR | APPLY_TRANSLATE; |
|
2098 |
type = TYPE_QUADRANT_ROTATION | TYPE_TRANSLATION; |
|
2099 |
} |
|
2100 |
break; |
|
2101 |
} |
|
2102 |
} |
|
2103 |
||
2104 |
/** |
|
2105 |
* Sets this transform to a scaling transformation. |
|
2106 |
* The matrix representing this transform becomes: |
|
2107 |
* <pre> |
|
2108 |
* [ sx 0 0 ] |
|
2109 |
* [ 0 sy 0 ] |
|
2110 |
* [ 0 0 1 ] |
|
2111 |
* </pre> |
|
2112 |
* @param sx the factor by which coordinates are scaled along the |
|
2113 |
* X axis direction |
|
2114 |
* @param sy the factor by which coordinates are scaled along the |
|
2115 |
* Y axis direction |
|
2116 |
* @since 1.2 |
|
2117 |
*/ |
|
2118 |
public void setToScale(double sx, double sy) { |
|
2119 |
m00 = sx; |
|
2120 |
m10 = 0.0; |
|
2121 |
m01 = 0.0; |
|
2122 |
m11 = sy; |
|
2123 |
m02 = 0.0; |
|
2124 |
m12 = 0.0; |
|
2125 |
if (sx != 1.0 || sy != 1.0) { |
|
2126 |
state = APPLY_SCALE; |
|
2127 |
type = TYPE_UNKNOWN; |
|
2128 |
} else { |
|
2129 |
state = APPLY_IDENTITY; |
|
2130 |
type = TYPE_IDENTITY; |
|
2131 |
} |
|
2132 |
} |
|
2133 |
||
2134 |
/** |
|
2135 |
* Sets this transform to a shearing transformation. |
|
2136 |
* The matrix representing this transform becomes: |
|
2137 |
* <pre> |
|
2138 |
* [ 1 shx 0 ] |
|
2139 |
* [ shy 1 0 ] |
|
2140 |
* [ 0 0 1 ] |
|
2141 |
* </pre> |
|
2142 |
* @param shx the multiplier by which coordinates are shifted in the |
|
2143 |
* direction of the positive X axis as a factor of their Y coordinate |
|
2144 |
* @param shy the multiplier by which coordinates are shifted in the |
|
2145 |
* direction of the positive Y axis as a factor of their X coordinate |
|
2146 |
* @since 1.2 |
|
2147 |
*/ |
|
2148 |
public void setToShear(double shx, double shy) { |
|
2149 |
m00 = 1.0; |
|
2150 |
m01 = shx; |
|
2151 |
m10 = shy; |
|
2152 |
m11 = 1.0; |
|
2153 |
m02 = 0.0; |
|
2154 |
m12 = 0.0; |
|
2155 |
if (shx != 0.0 || shy != 0.0) { |
|
2156 |
state = (APPLY_SHEAR | APPLY_SCALE); |
|
2157 |
type = TYPE_UNKNOWN; |
|
2158 |
} else { |
|
2159 |
state = APPLY_IDENTITY; |
|
2160 |
type = TYPE_IDENTITY; |
|
2161 |
} |
|
2162 |
} |
|
2163 |
||
2164 |
/** |
|
2165 |
* Sets this transform to a copy of the transform in the specified |
|
2166 |
* <code>AffineTransform</code> object. |
|
2167 |
* @param Tx the <code>AffineTransform</code> object from which to |
|
2168 |
* copy the transform |
|
2169 |
* @since 1.2 |
|
2170 |
*/ |
|
2171 |
public void setTransform(AffineTransform Tx) { |
|
2172 |
this.m00 = Tx.m00; |
|
2173 |
this.m10 = Tx.m10; |
|
2174 |
this.m01 = Tx.m01; |
|
2175 |
this.m11 = Tx.m11; |
|
2176 |
this.m02 = Tx.m02; |
|
2177 |
this.m12 = Tx.m12; |
|
2178 |
this.state = Tx.state; |
|
2179 |
this.type = Tx.type; |
|
2180 |
} |
|
2181 |
||
2182 |
/** |
|
2183 |
* Sets this transform to the matrix specified by the 6 |
|
2184 |
* double precision values. |
|
2185 |
* |
|
2186 |
* @param m00 the X coordinate scaling element of the 3x3 matrix |
|
2187 |
* @param m10 the Y coordinate shearing element of the 3x3 matrix |
|
2188 |
* @param m01 the X coordinate shearing element of the 3x3 matrix |
|
2189 |
* @param m11 the Y coordinate scaling element of the 3x3 matrix |
|
2190 |
* @param m02 the X coordinate translation element of the 3x3 matrix |
|
2191 |
* @param m12 the Y coordinate translation element of the 3x3 matrix |
|
2192 |
* @since 1.2 |
|
2193 |
*/ |
|
2194 |
public void setTransform(double m00, double m10, |
|
2195 |
double m01, double m11, |
|
2196 |
double m02, double m12) { |
|
2197 |
this.m00 = m00; |
|
2198 |
this.m10 = m10; |
|
2199 |
this.m01 = m01; |
|
2200 |
this.m11 = m11; |
|
2201 |
this.m02 = m02; |
|
2202 |
this.m12 = m12; |
|
2203 |
updateState(); |
|
2204 |
} |
|
2205 |
||
2206 |
/** |
|
2207 |
* Concatenates an <code>AffineTransform</code> <code>Tx</code> to |
|
2208 |
* this <code>AffineTransform</code> Cx in the most commonly useful |
|
2209 |
* way to provide a new user space |
|
2210 |
* that is mapped to the former user space by <code>Tx</code>. |
|
2211 |
* Cx is updated to perform the combined transformation. |
|
2212 |
* Transforming a point p by the updated transform Cx' is |
|
2213 |
* equivalent to first transforming p by <code>Tx</code> and then |
|
2214 |
* transforming the result by the original transform Cx like this: |
|
2215 |
* Cx'(p) = Cx(Tx(p)) |
|
2216 |
* In matrix notation, if this transform Cx is |
|
2217 |
* represented by the matrix [this] and <code>Tx</code> is represented |
|
2218 |
* by the matrix [Tx] then this method does the following: |
|
2219 |
* <pre> |
|
2220 |
* [this] = [this] x [Tx] |
|
2221 |
* </pre> |
|
2222 |
* @param Tx the <code>AffineTransform</code> object to be |
|
2223 |
* concatenated with this <code>AffineTransform</code> object. |
|
2224 |
* @see #preConcatenate |
|
2225 |
* @since 1.2 |
|
2226 |
*/ |
|
2227 |
public void concatenate(AffineTransform Tx) { |
|
2228 |
double M0, M1; |
|
2229 |
double T00, T01, T10, T11; |
|
2230 |
double T02, T12; |
|
2231 |
int mystate = state; |
|
2232 |
int txstate = Tx.state; |
|
2233 |
switch ((txstate << HI_SHIFT) | mystate) { |
|
2234 |
||
2235 |
/* ---------- Tx == IDENTITY cases ---------- */ |
|
2236 |
case (HI_IDENTITY | APPLY_IDENTITY): |
|
2237 |
case (HI_IDENTITY | APPLY_TRANSLATE): |
|
2238 |
case (HI_IDENTITY | APPLY_SCALE): |
|
2239 |
case (HI_IDENTITY | APPLY_SCALE | APPLY_TRANSLATE): |
|
2240 |
case (HI_IDENTITY | APPLY_SHEAR): |
|
2241 |
case (HI_IDENTITY | APPLY_SHEAR | APPLY_TRANSLATE): |
|
2242 |
case (HI_IDENTITY | APPLY_SHEAR | APPLY_SCALE): |
|
2243 |
case (HI_IDENTITY | APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2244 |
return; |
|
2245 |
||
2246 |
/* ---------- this == IDENTITY cases ---------- */ |
|
2247 |
case (HI_SHEAR | HI_SCALE | HI_TRANSLATE | APPLY_IDENTITY): |
|
2248 |
m01 = Tx.m01; |
|
2249 |
m10 = Tx.m10; |
|
2250 |
/* NOBREAK */ |
|
2251 |
case (HI_SCALE | HI_TRANSLATE | APPLY_IDENTITY): |
|
2252 |
m00 = Tx.m00; |
|
2253 |
m11 = Tx.m11; |
|
2254 |
/* NOBREAK */ |
|
2255 |
case (HI_TRANSLATE | APPLY_IDENTITY): |
|
2256 |
m02 = Tx.m02; |
|
2257 |
m12 = Tx.m12; |
|
2258 |
state = txstate; |
|
2259 |
type = Tx.type; |
|
2260 |
return; |
|
2261 |
case (HI_SHEAR | HI_SCALE | APPLY_IDENTITY): |
|
2262 |
m01 = Tx.m01; |
|
2263 |
m10 = Tx.m10; |
|
2264 |
/* NOBREAK */ |
|
2265 |
case (HI_SCALE | APPLY_IDENTITY): |
|
2266 |
m00 = Tx.m00; |
|
2267 |
m11 = Tx.m11; |
|
2268 |
state = txstate; |
|
2269 |
type = Tx.type; |
|
2270 |
return; |
|
2271 |
case (HI_SHEAR | HI_TRANSLATE | APPLY_IDENTITY): |
|
2272 |
m02 = Tx.m02; |
|
2273 |
m12 = Tx.m12; |
|
2274 |
/* NOBREAK */ |
|
2275 |
case (HI_SHEAR | APPLY_IDENTITY): |
|
2276 |
m01 = Tx.m01; |
|
2277 |
m10 = Tx.m10; |
|
2278 |
m00 = m11 = 0.0; |
|
2279 |
state = txstate; |
|
2280 |
type = Tx.type; |
|
2281 |
return; |
|
2282 |
||
2283 |
/* ---------- Tx == TRANSLATE cases ---------- */ |
|
2284 |
case (HI_TRANSLATE | APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2285 |
case (HI_TRANSLATE | APPLY_SHEAR | APPLY_SCALE): |
|
2286 |
case (HI_TRANSLATE | APPLY_SHEAR | APPLY_TRANSLATE): |
|
2287 |
case (HI_TRANSLATE | APPLY_SHEAR): |
|
2288 |
case (HI_TRANSLATE | APPLY_SCALE | APPLY_TRANSLATE): |
|
2289 |
case (HI_TRANSLATE | APPLY_SCALE): |
|
2290 |
case (HI_TRANSLATE | APPLY_TRANSLATE): |
|
2291 |
translate(Tx.m02, Tx.m12); |
|
2292 |
return; |
|
2293 |
||
2294 |
/* ---------- Tx == SCALE cases ---------- */ |
|
2295 |
case (HI_SCALE | APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2296 |
case (HI_SCALE | APPLY_SHEAR | APPLY_SCALE): |
|
2297 |
case (HI_SCALE | APPLY_SHEAR | APPLY_TRANSLATE): |
|
2298 |
case (HI_SCALE | APPLY_SHEAR): |
|
2299 |
case (HI_SCALE | APPLY_SCALE | APPLY_TRANSLATE): |
|
2300 |
case (HI_SCALE | APPLY_SCALE): |
|
2301 |
case (HI_SCALE | APPLY_TRANSLATE): |
|
2302 |
scale(Tx.m00, Tx.m11); |
|
2303 |
return; |
|
2304 |
||
2305 |
/* ---------- Tx == SHEAR cases ---------- */ |
|
2306 |
case (HI_SHEAR | APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2307 |
case (HI_SHEAR | APPLY_SHEAR | APPLY_SCALE): |
|
2308 |
T01 = Tx.m01; T10 = Tx.m10; |
|
2309 |
M0 = m00; |
|
2310 |
m00 = m01 * T10; |
|
2311 |
m01 = M0 * T01; |
|
2312 |
M0 = m10; |
|
2313 |
m10 = m11 * T10; |
|
2314 |
m11 = M0 * T01; |
|
2315 |
type = TYPE_UNKNOWN; |
|
2316 |
return; |
|
2317 |
case (HI_SHEAR | APPLY_SHEAR | APPLY_TRANSLATE): |
|
2318 |
case (HI_SHEAR | APPLY_SHEAR): |
|
2319 |
m00 = m01 * Tx.m10; |
|
2320 |
m01 = 0.0; |
|
2321 |
m11 = m10 * Tx.m01; |
|
2322 |
m10 = 0.0; |
|
2323 |
state = mystate ^ (APPLY_SHEAR | APPLY_SCALE); |
|
2324 |
type = TYPE_UNKNOWN; |
|
2325 |
return; |
|
2326 |
case (HI_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2327 |
case (HI_SHEAR | APPLY_SCALE): |
|
2328 |
m01 = m00 * Tx.m01; |
|
2329 |
m00 = 0.0; |
|
2330 |
m10 = m11 * Tx.m10; |
|
2331 |
m11 = 0.0; |
|
2332 |
state = mystate ^ (APPLY_SHEAR | APPLY_SCALE); |
|
2333 |
type = TYPE_UNKNOWN; |
|
2334 |
return; |
|
2335 |
case (HI_SHEAR | APPLY_TRANSLATE): |
|
2336 |
m00 = 0.0; |
|
2337 |
m01 = Tx.m01; |
|
2338 |
m10 = Tx.m10; |
|
2339 |
m11 = 0.0; |
|
2340 |
state = APPLY_TRANSLATE | APPLY_SHEAR; |
|
2341 |
type = TYPE_UNKNOWN; |
|
2342 |
return; |
|
2343 |
} |
|
2344 |
// If Tx has more than one attribute, it is not worth optimizing |
|
2345 |
// all of those cases... |
|
2346 |
T00 = Tx.m00; T01 = Tx.m01; T02 = Tx.m02; |
|
2347 |
T10 = Tx.m10; T11 = Tx.m11; T12 = Tx.m12; |
|
2348 |
switch (mystate) { |
|
2349 |
default: |
|
2350 |
stateError(); |
|
2351 |
/* NOTREACHED */ |
|
2352 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
2353 |
state = mystate | txstate; |
|
2354 |
/* NOBREAK */ |
|
2355 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2356 |
M0 = m00; |
|
2357 |
M1 = m01; |
|
2358 |
m00 = T00 * M0 + T10 * M1; |
|
2359 |
m01 = T01 * M0 + T11 * M1; |
|
2360 |
m02 += T02 * M0 + T12 * M1; |
|
2361 |
||
2362 |
M0 = m10; |
|
2363 |
M1 = m11; |
|
2364 |
m10 = T00 * M0 + T10 * M1; |
|
2365 |
m11 = T01 * M0 + T11 * M1; |
|
2366 |
m12 += T02 * M0 + T12 * M1; |
|
2367 |
type = TYPE_UNKNOWN; |
|
2368 |
return; |
|
2369 |
||
2370 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
2371 |
case (APPLY_SHEAR): |
|
2372 |
M0 = m01; |
|
2373 |
m00 = T10 * M0; |
|
2374 |
m01 = T11 * M0; |
|
2375 |
m02 += T12 * M0; |
|
2376 |
||
2377 |
M0 = m10; |
|
2378 |
m10 = T00 * M0; |
|
2379 |
m11 = T01 * M0; |
|
2380 |
m12 += T02 * M0; |
|
2381 |
break; |
|
2382 |
||
2383 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
2384 |
case (APPLY_SCALE): |
|
2385 |
M0 = m00; |
|
2386 |
m00 = T00 * M0; |
|
2387 |
m01 = T01 * M0; |
|
2388 |
m02 += T02 * M0; |
|
2389 |
||
2390 |
M0 = m11; |
|
2391 |
m10 = T10 * M0; |
|
2392 |
m11 = T11 * M0; |
|
2393 |
m12 += T12 * M0; |
|
2394 |
break; |
|
2395 |
||
2396 |
case (APPLY_TRANSLATE): |
|
2397 |
m00 = T00; |
|
2398 |
m01 = T01; |
|
2399 |
m02 += T02; |
|
2400 |
||
2401 |
m10 = T10; |
|
2402 |
m11 = T11; |
|
2403 |
m12 += T12; |
|
2404 |
state = txstate | APPLY_TRANSLATE; |
|
2405 |
type = TYPE_UNKNOWN; |
|
2406 |
return; |
|
2407 |
} |
|
2408 |
updateState(); |
|
2409 |
} |
|
2410 |
||
2411 |
/** |
|
2412 |
* Concatenates an <code>AffineTransform</code> <code>Tx</code> to |
|
2413 |
* this <code>AffineTransform</code> Cx |
|
2414 |
* in a less commonly used way such that <code>Tx</code> modifies the |
|
2415 |
* coordinate transformation relative to the absolute pixel |
|
2416 |
* space rather than relative to the existing user space. |
|
2417 |
* Cx is updated to perform the combined transformation. |
|
2418 |
* Transforming a point p by the updated transform Cx' is |
|
2419 |
* equivalent to first transforming p by the original transform |
|
2420 |
* Cx and then transforming the result by |
|
2421 |
* <code>Tx</code> like this: |
|
2422 |
* Cx'(p) = Tx(Cx(p)) |
|
2423 |
* In matrix notation, if this transform Cx |
|
2424 |
* is represented by the matrix [this] and <code>Tx</code> is |
|
2425 |
* represented by the matrix [Tx] then this method does the |
|
2426 |
* following: |
|
2427 |
* <pre> |
|
2428 |
* [this] = [Tx] x [this] |
|
2429 |
* </pre> |
|
2430 |
* @param Tx the <code>AffineTransform</code> object to be |
|
2431 |
* concatenated with this <code>AffineTransform</code> object. |
|
2432 |
* @see #concatenate |
|
2433 |
* @since 1.2 |
|
2434 |
*/ |
|
2435 |
public void preConcatenate(AffineTransform Tx) { |
|
2436 |
double M0, M1; |
|
2437 |
double T00, T01, T10, T11; |
|
2438 |
double T02, T12; |
|
2439 |
int mystate = state; |
|
2440 |
int txstate = Tx.state; |
|
2441 |
switch ((txstate << HI_SHIFT) | mystate) { |
|
2442 |
case (HI_IDENTITY | APPLY_IDENTITY): |
|
2443 |
case (HI_IDENTITY | APPLY_TRANSLATE): |
|
2444 |
case (HI_IDENTITY | APPLY_SCALE): |
|
2445 |
case (HI_IDENTITY | APPLY_SCALE | APPLY_TRANSLATE): |
|
2446 |
case (HI_IDENTITY | APPLY_SHEAR): |
|
2447 |
case (HI_IDENTITY | APPLY_SHEAR | APPLY_TRANSLATE): |
|
2448 |
case (HI_IDENTITY | APPLY_SHEAR | APPLY_SCALE): |
|
2449 |
case (HI_IDENTITY | APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2450 |
// Tx is IDENTITY... |
|
2451 |
return; |
|
2452 |
||
2453 |
case (HI_TRANSLATE | APPLY_IDENTITY): |
|
2454 |
case (HI_TRANSLATE | APPLY_SCALE): |
|
2455 |
case (HI_TRANSLATE | APPLY_SHEAR): |
|
2456 |
case (HI_TRANSLATE | APPLY_SHEAR | APPLY_SCALE): |
|
2457 |
// Tx is TRANSLATE, this has no TRANSLATE |
|
2458 |
m02 = Tx.m02; |
|
2459 |
m12 = Tx.m12; |
|
2460 |
state = mystate | APPLY_TRANSLATE; |
|
2461 |
type |= TYPE_TRANSLATION; |
|
2462 |
return; |
|
2463 |
||
2464 |
case (HI_TRANSLATE | APPLY_TRANSLATE): |
|
2465 |
case (HI_TRANSLATE | APPLY_SCALE | APPLY_TRANSLATE): |
|
2466 |
case (HI_TRANSLATE | APPLY_SHEAR | APPLY_TRANSLATE): |
|
2467 |
case (HI_TRANSLATE | APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2468 |
// Tx is TRANSLATE, this has one too |
|
2469 |
m02 = m02 + Tx.m02; |
|
2470 |
m12 = m12 + Tx.m12; |
|
2471 |
return; |
|
2472 |
||
2473 |
case (HI_SCALE | APPLY_TRANSLATE): |
|
2474 |
case (HI_SCALE | APPLY_IDENTITY): |
|
2475 |
// Only these two existing states need a new state |
|
2476 |
state = mystate | APPLY_SCALE; |
|
2477 |
/* NOBREAK */ |
|
2478 |
case (HI_SCALE | APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2479 |
case (HI_SCALE | APPLY_SHEAR | APPLY_SCALE): |
|
2480 |
case (HI_SCALE | APPLY_SHEAR | APPLY_TRANSLATE): |
|
2481 |
case (HI_SCALE | APPLY_SHEAR): |
|
2482 |
case (HI_SCALE | APPLY_SCALE | APPLY_TRANSLATE): |
|
2483 |
case (HI_SCALE | APPLY_SCALE): |
|
2484 |
// Tx is SCALE, this is anything |
|
2485 |
T00 = Tx.m00; |
|
2486 |
T11 = Tx.m11; |
|
2487 |
if ((mystate & APPLY_SHEAR) != 0) { |
|
2488 |
m01 = m01 * T00; |
|
2489 |
m10 = m10 * T11; |
|
2490 |
if ((mystate & APPLY_SCALE) != 0) { |
|
2491 |
m00 = m00 * T00; |
|
2492 |
m11 = m11 * T11; |
|
2493 |
} |
|
2494 |
} else { |
|
2495 |
m00 = m00 * T00; |
|
2496 |
m11 = m11 * T11; |
|
2497 |
} |
|
2498 |
if ((mystate & APPLY_TRANSLATE) != 0) { |
|
2499 |
m02 = m02 * T00; |
|
2500 |
m12 = m12 * T11; |
|
2501 |
} |
|
2502 |
type = TYPE_UNKNOWN; |
|
2503 |
return; |
|
2504 |
case (HI_SHEAR | APPLY_SHEAR | APPLY_TRANSLATE): |
|
2505 |
case (HI_SHEAR | APPLY_SHEAR): |
|
2506 |
mystate = mystate | APPLY_SCALE; |
|
2507 |
/* NOBREAK */ |
|
2508 |
case (HI_SHEAR | APPLY_TRANSLATE): |
|
2509 |
case (HI_SHEAR | APPLY_IDENTITY): |
|
2510 |
case (HI_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2511 |
case (HI_SHEAR | APPLY_SCALE): |
|
2512 |
state = mystate ^ APPLY_SHEAR; |
|
2513 |
/* NOBREAK */ |
|
2514 |
case (HI_SHEAR | APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2515 |
case (HI_SHEAR | APPLY_SHEAR | APPLY_SCALE): |
|
2516 |
// Tx is SHEAR, this is anything |
|
2517 |
T01 = Tx.m01; |
|
2518 |
T10 = Tx.m10; |
|
2519 |
||
2520 |
M0 = m00; |
|
2521 |
m00 = m10 * T01; |
|
2522 |
m10 = M0 * T10; |
|
2523 |
||
2524 |
M0 = m01; |
|
2525 |
m01 = m11 * T01; |
|
2526 |
m11 = M0 * T10; |
|
2527 |
||
2528 |
M0 = m02; |
|
2529 |
m02 = m12 * T01; |
|
2530 |
m12 = M0 * T10; |
|
2531 |
type = TYPE_UNKNOWN; |
|
2532 |
return; |
|
2533 |
} |
|
2534 |
// If Tx has more than one attribute, it is not worth optimizing |
|
2535 |
// all of those cases... |
|
2536 |
T00 = Tx.m00; T01 = Tx.m01; T02 = Tx.m02; |
|
2537 |
T10 = Tx.m10; T11 = Tx.m11; T12 = Tx.m12; |
|
2538 |
switch (mystate) { |
|
2539 |
default: |
|
2540 |
stateError(); |
|
2541 |
/* NOTREACHED */ |
|
2542 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2543 |
M0 = m02; |
|
2544 |
M1 = m12; |
|
2545 |
T02 += M0 * T00 + M1 * T01; |
|
2546 |
T12 += M0 * T10 + M1 * T11; |
|
2547 |
||
2548 |
/* NOBREAK */ |
|
2549 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
2550 |
m02 = T02; |
|
2551 |
m12 = T12; |
|
2552 |
||
2553 |
M0 = m00; |
|
2554 |
M1 = m10; |
|
2555 |
m00 = M0 * T00 + M1 * T01; |
|
2556 |
m10 = M0 * T10 + M1 * T11; |
|
2557 |
||
2558 |
M0 = m01; |
|
2559 |
M1 = m11; |
|
2560 |
m01 = M0 * T00 + M1 * T01; |
|
2561 |
m11 = M0 * T10 + M1 * T11; |
|
2562 |
break; |
|
2563 |
||
2564 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
2565 |
M0 = m02; |
|
2566 |
M1 = m12; |
|
2567 |
T02 += M0 * T00 + M1 * T01; |
|
2568 |
T12 += M0 * T10 + M1 * T11; |
|
2569 |
||
2570 |
/* NOBREAK */ |
|
2571 |
case (APPLY_SHEAR): |
|
2572 |
m02 = T02; |
|
2573 |
m12 = T12; |
|
2574 |
||
2575 |
M0 = m10; |
|
2576 |
m00 = M0 * T01; |
|
2577 |
m10 = M0 * T11; |
|
2578 |
||
2579 |
M0 = m01; |
|
2580 |
m01 = M0 * T00; |
|
2581 |
m11 = M0 * T10; |
|
2582 |
break; |
|
2583 |
||
2584 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
2585 |
M0 = m02; |
|
2586 |
M1 = m12; |
|
2587 |
T02 += M0 * T00 + M1 * T01; |
|
2588 |
T12 += M0 * T10 + M1 * T11; |
|
2589 |
||
2590 |
/* NOBREAK */ |
|
2591 |
case (APPLY_SCALE): |
|
2592 |
m02 = T02; |
|
2593 |
m12 = T12; |
|
2594 |
||
2595 |
M0 = m00; |
|
2596 |
m00 = M0 * T00; |
|
2597 |
m10 = M0 * T10; |
|
2598 |
||
2599 |
M0 = m11; |
|
2600 |
m01 = M0 * T01; |
|
2601 |
m11 = M0 * T11; |
|
2602 |
break; |
|
2603 |
||
2604 |
case (APPLY_TRANSLATE): |
|
2605 |
M0 = m02; |
|
2606 |
M1 = m12; |
|
2607 |
T02 += M0 * T00 + M1 * T01; |
|
2608 |
T12 += M0 * T10 + M1 * T11; |
|
2609 |
||
2610 |
/* NOBREAK */ |
|
2611 |
case (APPLY_IDENTITY): |
|
2612 |
m02 = T02; |
|
2613 |
m12 = T12; |
|
2614 |
||
2615 |
m00 = T00; |
|
2616 |
m10 = T10; |
|
2617 |
||
2618 |
m01 = T01; |
|
2619 |
m11 = T11; |
|
2620 |
||
2621 |
state = mystate | txstate; |
|
2622 |
type = TYPE_UNKNOWN; |
|
2623 |
return; |
|
2624 |
} |
|
2625 |
updateState(); |
|
2626 |
} |
|
2627 |
||
2628 |
/** |
|
2629 |
* Returns an <code>AffineTransform</code> object representing the |
|
2630 |
* inverse transformation. |
|
2631 |
* The inverse transform Tx' of this transform Tx |
|
2632 |
* maps coordinates transformed by Tx back |
|
2633 |
* to their original coordinates. |
|
2634 |
* In other words, Tx'(Tx(p)) = p = Tx(Tx'(p)). |
|
2635 |
* <p> |
|
2636 |
* If this transform maps all coordinates onto a point or a line |
|
2637 |
* then it will not have an inverse, since coordinates that do |
|
2638 |
* not lie on the destination point or line will not have an inverse |
|
2639 |
* mapping. |
|
2640 |
* The <code>getDeterminant</code> method can be used to determine if this |
|
2641 |
* transform has no inverse, in which case an exception will be |
|
2642 |
* thrown if the <code>createInverse</code> method is called. |
|
2643 |
* @return a new <code>AffineTransform</code> object representing the |
|
2644 |
* inverse transformation. |
|
2645 |
* @see #getDeterminant |
|
2646 |
* @exception NoninvertibleTransformException |
|
2647 |
* if the matrix cannot be inverted. |
|
2648 |
* @since 1.2 |
|
2649 |
*/ |
|
2650 |
public AffineTransform createInverse() |
|
2651 |
throws NoninvertibleTransformException |
|
2652 |
{ |
|
2653 |
double det; |
|
2654 |
switch (state) { |
|
2655 |
default: |
|
2656 |
stateError(); |
|
2657 |
/* NOTREACHED */ |
|
2658 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2659 |
det = m00 * m11 - m01 * m10; |
|
2660 |
if (Math.abs(det) <= Double.MIN_VALUE) { |
|
2661 |
throw new NoninvertibleTransformException("Determinant is "+ |
|
2662 |
det); |
|
2663 |
} |
|
2664 |
return new AffineTransform( m11 / det, -m10 / det, |
|
2665 |
-m01 / det, m00 / det, |
|
2666 |
(m01 * m12 - m11 * m02) / det, |
|
2667 |
(m10 * m02 - m00 * m12) / det, |
|
2668 |
(APPLY_SHEAR | |
|
2669 |
APPLY_SCALE | |
|
2670 |
APPLY_TRANSLATE)); |
|
2671 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
2672 |
det = m00 * m11 - m01 * m10; |
|
2673 |
if (Math.abs(det) <= Double.MIN_VALUE) { |
|
2674 |
throw new NoninvertibleTransformException("Determinant is "+ |
|
2675 |
det); |
|
2676 |
} |
|
2677 |
return new AffineTransform( m11 / det, -m10 / det, |
|
2678 |
-m01 / det, m00 / det, |
|
2679 |
0.0, 0.0, |
|
2680 |
(APPLY_SHEAR | APPLY_SCALE)); |
|
2681 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
2682 |
if (m01 == 0.0 || m10 == 0.0) { |
|
2683 |
throw new NoninvertibleTransformException("Determinant is 0"); |
|
2684 |
} |
|
2685 |
return new AffineTransform( 0.0, 1.0 / m01, |
|
2686 |
1.0 / m10, 0.0, |
|
2687 |
-m12 / m10, -m02 / m01, |
|
2688 |
(APPLY_SHEAR | APPLY_TRANSLATE)); |
|
2689 |
case (APPLY_SHEAR): |
|
2690 |
if (m01 == 0.0 || m10 == 0.0) { |
|
2691 |
throw new NoninvertibleTransformException("Determinant is 0"); |
|
2692 |
} |
|
2693 |
return new AffineTransform(0.0, 1.0 / m01, |
|
2694 |
1.0 / m10, 0.0, |
|
2695 |
0.0, 0.0, |
|
2696 |
(APPLY_SHEAR)); |
|
2697 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
2698 |
if (m00 == 0.0 || m11 == 0.0) { |
|
2699 |
throw new NoninvertibleTransformException("Determinant is 0"); |
|
2700 |
} |
|
2701 |
return new AffineTransform( 1.0 / m00, 0.0, |
|
2702 |
0.0, 1.0 / m11, |
|
2703 |
-m02 / m00, -m12 / m11, |
|
2704 |
(APPLY_SCALE | APPLY_TRANSLATE)); |
|
2705 |
case (APPLY_SCALE): |
|
2706 |
if (m00 == 0.0 || m11 == 0.0) { |
|
2707 |
throw new NoninvertibleTransformException("Determinant is 0"); |
|
2708 |
} |
|
2709 |
return new AffineTransform(1.0 / m00, 0.0, |
|
2710 |
0.0, 1.0 / m11, |
|
2711 |
0.0, 0.0, |
|
2712 |
(APPLY_SCALE)); |
|
2713 |
case (APPLY_TRANSLATE): |
|
2714 |
return new AffineTransform( 1.0, 0.0, |
|
2715 |
0.0, 1.0, |
|
2716 |
-m02, -m12, |
|
2717 |
(APPLY_TRANSLATE)); |
|
2718 |
case (APPLY_IDENTITY): |
|
2719 |
return new AffineTransform(); |
|
2720 |
} |
|
2721 |
||
2722 |
/* NOTREACHED */ |
|
2723 |
} |
|
2724 |
||
2725 |
/** |
|
2726 |
* Sets this transform to the inverse of itself. |
|
2727 |
* The inverse transform Tx' of this transform Tx |
|
2728 |
* maps coordinates transformed by Tx back |
|
2729 |
* to their original coordinates. |
|
2730 |
* In other words, Tx'(Tx(p)) = p = Tx(Tx'(p)). |
|
2731 |
* <p> |
|
2732 |
* If this transform maps all coordinates onto a point or a line |
|
2733 |
* then it will not have an inverse, since coordinates that do |
|
2734 |
* not lie on the destination point or line will not have an inverse |
|
2735 |
* mapping. |
|
2736 |
* The <code>getDeterminant</code> method can be used to determine if this |
|
2737 |
* transform has no inverse, in which case an exception will be |
|
2738 |
* thrown if the <code>invert</code> method is called. |
|
2739 |
* @see #getDeterminant |
|
2740 |
* @exception NoninvertibleTransformException |
|
2741 |
* if the matrix cannot be inverted. |
|
2742 |
* @since 1.6 |
|
2743 |
*/ |
|
2744 |
public void invert() |
|
2745 |
throws NoninvertibleTransformException |
|
2746 |
{ |
|
2747 |
double M00, M01, M02; |
|
2748 |
double M10, M11, M12; |
|
2749 |
double det; |
|
2750 |
switch (state) { |
|
2751 |
default: |
|
2752 |
stateError(); |
|
2753 |
/* NOTREACHED */ |
|
2754 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2755 |
M00 = m00; M01 = m01; M02 = m02; |
|
2756 |
M10 = m10; M11 = m11; M12 = m12; |
|
2757 |
det = M00 * M11 - M01 * M10; |
|
2758 |
if (Math.abs(det) <= Double.MIN_VALUE) { |
|
2759 |
throw new NoninvertibleTransformException("Determinant is "+ |
|
2760 |
det); |
|
2761 |
} |
|
2762 |
m00 = M11 / det; |
|
2763 |
m10 = -M10 / det; |
|
2764 |
m01 = -M01 / det; |
|
2765 |
m11 = M00 / det; |
|
2766 |
m02 = (M01 * M12 - M11 * M02) / det; |
|
2767 |
m12 = (M10 * M02 - M00 * M12) / det; |
|
2768 |
break; |
|
2769 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
2770 |
M00 = m00; M01 = m01; |
|
2771 |
M10 = m10; M11 = m11; |
|
2772 |
det = M00 * M11 - M01 * M10; |
|
2773 |
if (Math.abs(det) <= Double.MIN_VALUE) { |
|
2774 |
throw new NoninvertibleTransformException("Determinant is "+ |
|
2775 |
det); |
|
2776 |
} |
|
2777 |
m00 = M11 / det; |
|
2778 |
m10 = -M10 / det; |
|
2779 |
m01 = -M01 / det; |
|
2780 |
m11 = M00 / det; |
|
2781 |
// m02 = 0.0; |
|
2782 |
// m12 = 0.0; |
|
2783 |
break; |
|
2784 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
2785 |
M01 = m01; M02 = m02; |
|
2786 |
M10 = m10; M12 = m12; |
|
2787 |
if (M01 == 0.0 || M10 == 0.0) { |
|
2788 |
throw new NoninvertibleTransformException("Determinant is 0"); |
|
2789 |
} |
|
2790 |
// m00 = 0.0; |
|
2791 |
m10 = 1.0 / M01; |
|
2792 |
m01 = 1.0 / M10; |
|
2793 |
// m11 = 0.0; |
|
2794 |
m02 = -M12 / M10; |
|
2795 |
m12 = -M02 / M01; |
|
2796 |
break; |
|
2797 |
case (APPLY_SHEAR): |
|
2798 |
M01 = m01; |
|
2799 |
M10 = m10; |
|
2800 |
if (M01 == 0.0 || M10 == 0.0) { |
|
2801 |
throw new NoninvertibleTransformException("Determinant is 0"); |
|
2802 |
} |
|
2803 |
// m00 = 0.0; |
|
2804 |
m10 = 1.0 / M01; |
|
2805 |
m01 = 1.0 / M10; |
|
2806 |
// m11 = 0.0; |
|
2807 |
// m02 = 0.0; |
|
2808 |
// m12 = 0.0; |
|
2809 |
break; |
|
2810 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
2811 |
M00 = m00; M02 = m02; |
|
2812 |
M11 = m11; M12 = m12; |
|
2813 |
if (M00 == 0.0 || M11 == 0.0) { |
|
2814 |
throw new NoninvertibleTransformException("Determinant is 0"); |
|
2815 |
} |
|
2816 |
m00 = 1.0 / M00; |
|
2817 |
// m10 = 0.0; |
|
2818 |
// m01 = 0.0; |
|
2819 |
m11 = 1.0 / M11; |
|
2820 |
m02 = -M02 / M00; |
|
2821 |
m12 = -M12 / M11; |
|
2822 |
break; |
|
2823 |
case (APPLY_SCALE): |
|
2824 |
M00 = m00; |
|
2825 |
M11 = m11; |
|
2826 |
if (M00 == 0.0 || M11 == 0.0) { |
|
2827 |
throw new NoninvertibleTransformException("Determinant is 0"); |
|
2828 |
} |
|
2829 |
m00 = 1.0 / M00; |
|
2830 |
// m10 = 0.0; |
|
2831 |
// m01 = 0.0; |
|
2832 |
m11 = 1.0 / M11; |
|
2833 |
// m02 = 0.0; |
|
2834 |
// m12 = 0.0; |
|
2835 |
break; |
|
2836 |
case (APPLY_TRANSLATE): |
|
2837 |
// m00 = 1.0; |
|
2838 |
// m10 = 0.0; |
|
2839 |
// m01 = 0.0; |
|
2840 |
// m11 = 1.0; |
|
2841 |
m02 = -m02; |
|
2842 |
m12 = -m12; |
|
2843 |
break; |
|
2844 |
case (APPLY_IDENTITY): |
|
2845 |
// m00 = 1.0; |
|
2846 |
// m10 = 0.0; |
|
2847 |
// m01 = 0.0; |
|
2848 |
// m11 = 1.0; |
|
2849 |
// m02 = 0.0; |
|
2850 |
// m12 = 0.0; |
|
2851 |
break; |
|
2852 |
} |
|
2853 |
} |
|
2854 |
||
2855 |
/** |
|
2856 |
* Transforms the specified <code>ptSrc</code> and stores the result |
|
2857 |
* in <code>ptDst</code>. |
|
2858 |
* If <code>ptDst</code> is <code>null</code>, a new {@link Point2D} |
|
2859 |
* object is allocated and then the result of the transformation is |
|
2860 |
* stored in this object. |
|
2861 |
* In either case, <code>ptDst</code>, which contains the |
|
2862 |
* transformed point, is returned for convenience. |
|
2863 |
* If <code>ptSrc</code> and <code>ptDst</code> are the same |
|
2864 |
* object, the input point is correctly overwritten with |
|
2865 |
* the transformed point. |
|
2866 |
* @param ptSrc the specified <code>Point2D</code> to be transformed |
|
2867 |
* @param ptDst the specified <code>Point2D</code> that stores the |
|
2868 |
* result of transforming <code>ptSrc</code> |
|
2869 |
* @return the <code>ptDst</code> after transforming |
|
2870 |
* <code>ptSrc</code> and stroring the result in <code>ptDst</code>. |
|
2871 |
* @since 1.2 |
|
2872 |
*/ |
|
2873 |
public Point2D transform(Point2D ptSrc, Point2D ptDst) { |
|
2874 |
if (ptDst == null) { |
|
2875 |
if (ptSrc instanceof Point2D.Double) { |
|
2876 |
ptDst = new Point2D.Double(); |
|
2877 |
} else { |
|
2878 |
ptDst = new Point2D.Float(); |
|
2879 |
} |
|
2880 |
} |
|
2881 |
// Copy source coords into local variables in case src == dst |
|
2882 |
double x = ptSrc.getX(); |
|
2883 |
double y = ptSrc.getY(); |
|
2884 |
switch (state) { |
|
2885 |
default: |
|
2886 |
stateError(); |
|
2887 |
/* NOTREACHED */ |
|
2888 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2889 |
ptDst.setLocation(x * m00 + y * m01 + m02, |
|
2890 |
x * m10 + y * m11 + m12); |
|
2891 |
return ptDst; |
|
2892 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
2893 |
ptDst.setLocation(x * m00 + y * m01, x * m10 + y * m11); |
|
2894 |
return ptDst; |
|
2895 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
2896 |
ptDst.setLocation(y * m01 + m02, x * m10 + m12); |
|
2897 |
return ptDst; |
|
2898 |
case (APPLY_SHEAR): |
|
2899 |
ptDst.setLocation(y * m01, x * m10); |
|
2900 |
return ptDst; |
|
2901 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
2902 |
ptDst.setLocation(x * m00 + m02, y * m11 + m12); |
|
2903 |
return ptDst; |
|
2904 |
case (APPLY_SCALE): |
|
2905 |
ptDst.setLocation(x * m00, y * m11); |
|
2906 |
return ptDst; |
|
2907 |
case (APPLY_TRANSLATE): |
|
2908 |
ptDst.setLocation(x + m02, y + m12); |
|
2909 |
return ptDst; |
|
2910 |
case (APPLY_IDENTITY): |
|
2911 |
ptDst.setLocation(x, y); |
|
2912 |
return ptDst; |
|
2913 |
} |
|
2914 |
||
2915 |
/* NOTREACHED */ |
|
2916 |
} |
|
2917 |
||
2918 |
/** |
|
2919 |
* Transforms an array of point objects by this transform. |
|
2920 |
* If any element of the <code>ptDst</code> array is |
|
2921 |
* <code>null</code>, a new <code>Point2D</code> object is allocated |
|
2922 |
* and stored into that element before storing the results of the |
|
2923 |
* transformation. |
|
2924 |
* <p> |
|
2925 |
* Note that this method does not take any precautions to |
|
2926 |
* avoid problems caused by storing results into <code>Point2D</code> |
|
2927 |
* objects that will be used as the source for calculations |
|
2928 |
* further down the source array. |
|
2929 |
* This method does guarantee that if a specified <code>Point2D</code> |
|
2930 |
* object is both the source and destination for the same single point |
|
2931 |
* transform operation then the results will not be stored until |
|
2932 |
* the calculations are complete to avoid storing the results on |
|
2933 |
* top of the operands. |
|
2934 |
* If, however, the destination <code>Point2D</code> object for one |
|
2935 |
* operation is the same object as the source <code>Point2D</code> |
|
2936 |
* object for another operation further down the source array then |
|
2937 |
* the original coordinates in that point are overwritten before |
|
2938 |
* they can be converted. |
|
2939 |
* @param ptSrc the array containing the source point objects |
|
2940 |
* @param ptDst the array into which the transform point objects are |
|
2941 |
* returned |
|
2942 |
* @param srcOff the offset to the first point object to be |
|
2943 |
* transformed in the source array |
|
2944 |
* @param dstOff the offset to the location of the first |
|
2945 |
* transformed point object that is stored in the destination array |
|
2946 |
* @param numPts the number of point objects to be transformed |
|
2947 |
* @since 1.2 |
|
2948 |
*/ |
|
2949 |
public void transform(Point2D[] ptSrc, int srcOff, |
|
2950 |
Point2D[] ptDst, int dstOff, |
|
2951 |
int numPts) { |
|
2952 |
int state = this.state; |
|
2953 |
while (--numPts >= 0) { |
|
2954 |
// Copy source coords into local variables in case src == dst |
|
2955 |
Point2D src = ptSrc[srcOff++]; |
|
2956 |
double x = src.getX(); |
|
2957 |
double y = src.getY(); |
|
2958 |
Point2D dst = ptDst[dstOff++]; |
|
2959 |
if (dst == null) { |
|
2960 |
if (src instanceof Point2D.Double) { |
|
2961 |
dst = new Point2D.Double(); |
|
2962 |
} else { |
|
2963 |
dst = new Point2D.Float(); |
|
2964 |
} |
|
2965 |
ptDst[dstOff - 1] = dst; |
|
2966 |
} |
|
2967 |
switch (state) { |
|
2968 |
default: |
|
2969 |
stateError(); |
|
2970 |
/* NOTREACHED */ |
|
2971 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
2972 |
dst.setLocation(x * m00 + y * m01 + m02, |
|
2973 |
x * m10 + y * m11 + m12); |
|
2974 |
break; |
|
2975 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
2976 |
dst.setLocation(x * m00 + y * m01, x * m10 + y * m11); |
|
2977 |
break; |
|
2978 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
2979 |
dst.setLocation(y * m01 + m02, x * m10 + m12); |
|
2980 |
break; |
|
2981 |
case (APPLY_SHEAR): |
|
2982 |
dst.setLocation(y * m01, x * m10); |
|
2983 |
break; |
|
2984 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
2985 |
dst.setLocation(x * m00 + m02, y * m11 + m12); |
|
2986 |
break; |
|
2987 |
case (APPLY_SCALE): |
|
2988 |
dst.setLocation(x * m00, y * m11); |
|
2989 |
break; |
|
2990 |
case (APPLY_TRANSLATE): |
|
2991 |
dst.setLocation(x + m02, y + m12); |
|
2992 |
break; |
|
2993 |
case (APPLY_IDENTITY): |
|
2994 |
dst.setLocation(x, y); |
|
2995 |
break; |
|
2996 |
} |
|
2997 |
} |
|
2998 |
||
2999 |
/* NOTREACHED */ |
|
3000 |
} |
|
3001 |
||
3002 |
/** |
|
3003 |
* Transforms an array of floating point coordinates by this transform. |
|
3004 |
* The two coordinate array sections can be exactly the same or |
|
3005 |
* can be overlapping sections of the same array without affecting the |
|
3006 |
* validity of the results. |
|
3007 |
* This method ensures that no source coordinates are overwritten by a |
|
3008 |
* previous operation before they can be transformed. |
|
3009 |
* The coordinates are stored in the arrays starting at the specified |
|
3010 |
* offset in the order <code>[x0, y0, x1, y1, ..., xn, yn]</code>. |
|
3011 |
* @param srcPts the array containing the source point coordinates. |
|
3012 |
* Each point is stored as a pair of x, y coordinates. |
|
3013 |
* @param dstPts the array into which the transformed point coordinates |
|
3014 |
* are returned. Each point is stored as a pair of x, y |
|
3015 |
* coordinates. |
|
3016 |
* @param srcOff the offset to the first point to be transformed |
|
3017 |
* in the source array |
|
3018 |
* @param dstOff the offset to the location of the first |
|
3019 |
* transformed point that is stored in the destination array |
|
3020 |
* @param numPts the number of points to be transformed |
|
3021 |
* @since 1.2 |
|
3022 |
*/ |
|
3023 |
public void transform(float[] srcPts, int srcOff, |
|
3024 |
float[] dstPts, int dstOff, |
|
3025 |
int numPts) { |
|
3026 |
double M00, M01, M02, M10, M11, M12; // For caching |
|
3027 |
if (dstPts == srcPts && |
|
3028 |
dstOff > srcOff && dstOff < srcOff + numPts * 2) |
|
3029 |
{ |
|
3030 |
// If the arrays overlap partially with the destination higher |
|
3031 |
// than the source and we transform the coordinates normally |
|
3032 |
// we would overwrite some of the later source coordinates |
|
3033 |
// with results of previous transformations. |
|
3034 |
// To get around this we use arraycopy to copy the points |
|
3035 |
// to their final destination with correct overwrite |
|
3036 |
// handling and then transform them in place in the new |
|
3037 |
// safer location. |
|
3038 |
System.arraycopy(srcPts, srcOff, dstPts, dstOff, numPts * 2); |
|
3039 |
// srcPts = dstPts; // They are known to be equal. |
|
3040 |
srcOff = dstOff; |
|
3041 |
} |
|
3042 |
switch (state) { |
|
3043 |
default: |
|
3044 |
stateError(); |
|
3045 |
/* NOTREACHED */ |
|
3046 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
3047 |
M00 = m00; M01 = m01; M02 = m02; |
|
3048 |
M10 = m10; M11 = m11; M12 = m12; |
|
3049 |
while (--numPts >= 0) { |
|
3050 |
double x = srcPts[srcOff++]; |
|
3051 |
double y = srcPts[srcOff++]; |
|
3052 |
dstPts[dstOff++] = (float) (M00 * x + M01 * y + M02); |
|
3053 |
dstPts[dstOff++] = (float) (M10 * x + M11 * y + M12); |
|
3054 |
} |
|
3055 |
return; |
|
3056 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
3057 |
M00 = m00; M01 = m01; |
|
3058 |
M10 = m10; M11 = m11; |
|
3059 |
while (--numPts >= 0) { |
|
3060 |
double x = srcPts[srcOff++]; |
|
3061 |
double y = srcPts[srcOff++]; |
|
3062 |
dstPts[dstOff++] = (float) (M00 * x + M01 * y); |
|
3063 |
dstPts[dstOff++] = (float) (M10 * x + M11 * y); |
|
3064 |
} |
|
3065 |
return; |
|
3066 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
3067 |
M01 = m01; M02 = m02; |
|
3068 |
M10 = m10; M12 = m12; |
|
3069 |
while (--numPts >= 0) { |
|
3070 |
double x = srcPts[srcOff++]; |
|
3071 |
dstPts[dstOff++] = (float) (M01 * srcPts[srcOff++] + M02); |
|
3072 |
dstPts[dstOff++] = (float) (M10 * x + M12); |
|
3073 |
} |
|
3074 |
return; |
|
3075 |
case (APPLY_SHEAR): |
|
3076 |
M01 = m01; M10 = m10; |
|
3077 |
while (--numPts >= 0) { |
|
3078 |
double x = srcPts[srcOff++]; |
|
3079 |
dstPts[dstOff++] = (float) (M01 * srcPts[srcOff++]); |
|
3080 |
dstPts[dstOff++] = (float) (M10 * x); |
|
3081 |
} |
|
3082 |
return; |
|
3083 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
3084 |
M00 = m00; M02 = m02; |
|
3085 |
M11 = m11; M12 = m12; |
|
3086 |
while (--numPts >= 0) { |
|
3087 |
dstPts[dstOff++] = (float) (M00 * srcPts[srcOff++] + M02); |
|
3088 |
dstPts[dstOff++] = (float) (M11 * srcPts[srcOff++] + M12); |
|
3089 |
} |
|
3090 |
return; |
|
3091 |
case (APPLY_SCALE): |
|
3092 |
M00 = m00; M11 = m11; |
|
3093 |
while (--numPts >= 0) { |
|
3094 |
dstPts[dstOff++] = (float) (M00 * srcPts[srcOff++]); |
|
3095 |
dstPts[dstOff++] = (float) (M11 * srcPts[srcOff++]); |
|
3096 |
} |
|
3097 |
return; |
|
3098 |
case (APPLY_TRANSLATE): |
|
3099 |
M02 = m02; M12 = m12; |
|
3100 |
while (--numPts >= 0) { |
|
3101 |
dstPts[dstOff++] = (float) (srcPts[srcOff++] + M02); |
|
3102 |
dstPts[dstOff++] = (float) (srcPts[srcOff++] + M12); |
|
3103 |
} |
|
3104 |
return; |
|
3105 |
case (APPLY_IDENTITY): |
|
3106 |
if (srcPts != dstPts || srcOff != dstOff) { |
|
3107 |
System.arraycopy(srcPts, srcOff, dstPts, dstOff, |
|
3108 |
numPts * 2); |
|
3109 |
} |
|
3110 |
return; |
|
3111 |
} |
|
3112 |
||
3113 |
/* NOTREACHED */ |
|
3114 |
} |
|
3115 |
||
3116 |
/** |
|
3117 |
* Transforms an array of double precision coordinates by this transform. |
|
3118 |
* The two coordinate array sections can be exactly the same or |
|
3119 |
* can be overlapping sections of the same array without affecting the |
|
3120 |
* validity of the results. |
|
3121 |
* This method ensures that no source coordinates are |
|
3122 |
* overwritten by a previous operation before they can be transformed. |
|
3123 |
* The coordinates are stored in the arrays starting at the indicated |
|
3124 |
* offset in the order <code>[x0, y0, x1, y1, ..., xn, yn]</code>. |
|
3125 |
* @param srcPts the array containing the source point coordinates. |
|
3126 |
* Each point is stored as a pair of x, y coordinates. |
|
3127 |
* @param dstPts the array into which the transformed point |
|
3128 |
* coordinates are returned. Each point is stored as a pair of |
|
3129 |
* x, y coordinates. |
|
3130 |
* @param srcOff the offset to the first point to be transformed |
|
3131 |
* in the source array |
|
3132 |
* @param dstOff the offset to the location of the first |
|
3133 |
* transformed point that is stored in the destination array |
|
3134 |
* @param numPts the number of point objects to be transformed |
|
3135 |
* @since 1.2 |
|
3136 |
*/ |
|
3137 |
public void transform(double[] srcPts, int srcOff, |
|
3138 |
double[] dstPts, int dstOff, |
|
3139 |
int numPts) { |
|
3140 |
double M00, M01, M02, M10, M11, M12; // For caching |
|
3141 |
if (dstPts == srcPts && |
|
3142 |
dstOff > srcOff && dstOff < srcOff + numPts * 2) |
|
3143 |
{ |
|
3144 |
// If the arrays overlap partially with the destination higher |
|
3145 |
// than the source and we transform the coordinates normally |
|
3146 |
// we would overwrite some of the later source coordinates |
|
3147 |
// with results of previous transformations. |
|
3148 |
// To get around this we use arraycopy to copy the points |
|
3149 |
// to their final destination with correct overwrite |
|
3150 |
// handling and then transform them in place in the new |
|
3151 |
// safer location. |
|
3152 |
System.arraycopy(srcPts, srcOff, dstPts, dstOff, numPts * 2); |
|
3153 |
// srcPts = dstPts; // They are known to be equal. |
|
3154 |
srcOff = dstOff; |
|
3155 |
} |
|
3156 |
switch (state) { |
|
3157 |
default: |
|
3158 |
stateError(); |
|
3159 |
/* NOTREACHED */ |
|
3160 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
3161 |
M00 = m00; M01 = m01; M02 = m02; |
|
3162 |
M10 = m10; M11 = m11; M12 = m12; |
|
3163 |
while (--numPts >= 0) { |
|
3164 |
double x = srcPts[srcOff++]; |
|
3165 |
double y = srcPts[srcOff++]; |
|
3166 |
dstPts[dstOff++] = M00 * x + M01 * y + M02; |
|
3167 |
dstPts[dstOff++] = M10 * x + M11 * y + M12; |
|
3168 |
} |
|
3169 |
return; |
|
3170 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
3171 |
M00 = m00; M01 = m01; |
|
3172 |
M10 = m10; M11 = m11; |
|
3173 |
while (--numPts >= 0) { |
|
3174 |
double x = srcPts[srcOff++]; |
|
3175 |
double y = srcPts[srcOff++]; |
|
3176 |
dstPts[dstOff++] = M00 * x + M01 * y; |
|
3177 |
dstPts[dstOff++] = M10 * x + M11 * y; |
|
3178 |
} |
|
3179 |
return; |
|
3180 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
3181 |
M01 = m01; M02 = m02; |
|
3182 |
M10 = m10; M12 = m12; |
|
3183 |
while (--numPts >= 0) { |
|
3184 |
double x = srcPts[srcOff++]; |
|
3185 |
dstPts[dstOff++] = M01 * srcPts[srcOff++] + M02; |
|
3186 |
dstPts[dstOff++] = M10 * x + M12; |
|
3187 |
} |
|
3188 |
return; |
|
3189 |
case (APPLY_SHEAR): |
|
3190 |
M01 = m01; M10 = m10; |
|
3191 |
while (--numPts >= 0) { |
|
3192 |
double x = srcPts[srcOff++]; |
|
3193 |
dstPts[dstOff++] = M01 * srcPts[srcOff++]; |
|
3194 |
dstPts[dstOff++] = M10 * x; |
|
3195 |
} |
|
3196 |
return; |
|
3197 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
3198 |
M00 = m00; M02 = m02; |
|
3199 |
M11 = m11; M12 = m12; |
|
3200 |
while (--numPts >= 0) { |
|
3201 |
dstPts[dstOff++] = M00 * srcPts[srcOff++] + M02; |
|
3202 |
dstPts[dstOff++] = M11 * srcPts[srcOff++] + M12; |
|
3203 |
} |
|
3204 |
return; |
|
3205 |
case (APPLY_SCALE): |
|
3206 |
M00 = m00; M11 = m11; |
|
3207 |
while (--numPts >= 0) { |
|
3208 |
dstPts[dstOff++] = M00 * srcPts[srcOff++]; |
|
3209 |
dstPts[dstOff++] = M11 * srcPts[srcOff++]; |
|
3210 |
} |
|
3211 |
return; |
|
3212 |
case (APPLY_TRANSLATE): |
|
3213 |
M02 = m02; M12 = m12; |
|
3214 |
while (--numPts >= 0) { |
|
3215 |
dstPts[dstOff++] = srcPts[srcOff++] + M02; |
|
3216 |
dstPts[dstOff++] = srcPts[srcOff++] + M12; |
|
3217 |
} |
|
3218 |
return; |
|
3219 |
case (APPLY_IDENTITY): |
|
3220 |
if (srcPts != dstPts || srcOff != dstOff) { |
|
3221 |
System.arraycopy(srcPts, srcOff, dstPts, dstOff, |
|
3222 |
numPts * 2); |
|
3223 |
} |
|
3224 |
return; |
|
3225 |
} |
|
3226 |
||
3227 |
/* NOTREACHED */ |
|
3228 |
} |
|
3229 |
||
3230 |
/** |
|
3231 |
* Transforms an array of floating point coordinates by this transform |
|
3232 |
* and stores the results into an array of doubles. |
|
3233 |
* The coordinates are stored in the arrays starting at the specified |
|
3234 |
* offset in the order <code>[x0, y0, x1, y1, ..., xn, yn]</code>. |
|
3235 |
* @param srcPts the array containing the source point coordinates. |
|
3236 |
* Each point is stored as a pair of x, y coordinates. |
|
3237 |
* @param dstPts the array into which the transformed point coordinates |
|
3238 |
* are returned. Each point is stored as a pair of x, y |
|
3239 |
* coordinates. |
|
3240 |
* @param srcOff the offset to the first point to be transformed |
|
3241 |
* in the source array |
|
3242 |
* @param dstOff the offset to the location of the first |
|
3243 |
* transformed point that is stored in the destination array |
|
3244 |
* @param numPts the number of points to be transformed |
|
3245 |
* @since 1.2 |
|
3246 |
*/ |
|
3247 |
public void transform(float[] srcPts, int srcOff, |
|
3248 |
double[] dstPts, int dstOff, |
|
3249 |
int numPts) { |
|
3250 |
double M00, M01, M02, M10, M11, M12; // For caching |
|
3251 |
switch (state) { |
|
3252 |
default: |
|
3253 |
stateError(); |
|
3254 |
/* NOTREACHED */ |
|
3255 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
3256 |
M00 = m00; M01 = m01; M02 = m02; |
|
3257 |
M10 = m10; M11 = m11; M12 = m12; |
|
3258 |
while (--numPts >= 0) { |
|
3259 |
double x = srcPts[srcOff++]; |
|
3260 |
double y = srcPts[srcOff++]; |
|
3261 |
dstPts[dstOff++] = M00 * x + M01 * y + M02; |
|
3262 |
dstPts[dstOff++] = M10 * x + M11 * y + M12; |
|
3263 |
} |
|
3264 |
return; |
|
3265 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
3266 |
M00 = m00; M01 = m01; |
|
3267 |
M10 = m10; M11 = m11; |
|
3268 |
while (--numPts >= 0) { |
|
3269 |
double x = srcPts[srcOff++]; |
|
3270 |
double y = srcPts[srcOff++]; |
|
3271 |
dstPts[dstOff++] = M00 * x + M01 * y; |
|
3272 |
dstPts[dstOff++] = M10 * x + M11 * y; |
|
3273 |
} |
|
3274 |
return; |
|
3275 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
3276 |
M01 = m01; M02 = m02; |
|
3277 |
M10 = m10; M12 = m12; |
|
3278 |
while (--numPts >= 0) { |
|
3279 |
double x = srcPts[srcOff++]; |
|
3280 |
dstPts[dstOff++] = M01 * srcPts[srcOff++] + M02; |
|
3281 |
dstPts[dstOff++] = M10 * x + M12; |
|
3282 |
} |
|
3283 |
return; |
|
3284 |
case (APPLY_SHEAR): |
|
3285 |
M01 = m01; M10 = m10; |
|
3286 |
while (--numPts >= 0) { |
|
3287 |
double x = srcPts[srcOff++]; |
|
3288 |
dstPts[dstOff++] = M01 * srcPts[srcOff++]; |
|
3289 |
dstPts[dstOff++] = M10 * x; |
|
3290 |
} |
|
3291 |
return; |
|
3292 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
3293 |
M00 = m00; M02 = m02; |
|
3294 |
M11 = m11; M12 = m12; |
|
3295 |
while (--numPts >= 0) { |
|
3296 |
dstPts[dstOff++] = M00 * srcPts[srcOff++] + M02; |
|
3297 |
dstPts[dstOff++] = M11 * srcPts[srcOff++] + M12; |
|
3298 |
} |
|
3299 |
return; |
|
3300 |
case (APPLY_SCALE): |
|
3301 |
M00 = m00; M11 = m11; |
|
3302 |
while (--numPts >= 0) { |
|
3303 |
dstPts[dstOff++] = M00 * srcPts[srcOff++]; |
|
3304 |
dstPts[dstOff++] = M11 * srcPts[srcOff++]; |
|
3305 |
} |
|
3306 |
return; |
|
3307 |
case (APPLY_TRANSLATE): |
|
3308 |
M02 = m02; M12 = m12; |
|
3309 |
while (--numPts >= 0) { |
|
3310 |
dstPts[dstOff++] = srcPts[srcOff++] + M02; |
|
3311 |
dstPts[dstOff++] = srcPts[srcOff++] + M12; |
|
3312 |
} |
|
3313 |
return; |
|
3314 |
case (APPLY_IDENTITY): |
|
3315 |
while (--numPts >= 0) { |
|
3316 |
dstPts[dstOff++] = srcPts[srcOff++]; |
|
3317 |
dstPts[dstOff++] = srcPts[srcOff++]; |
|
3318 |
} |
|
3319 |
return; |
|
3320 |
} |
|
3321 |
||
3322 |
/* NOTREACHED */ |
|
3323 |
} |
|
3324 |
||
3325 |
/** |
|
3326 |
* Transforms an array of double precision coordinates by this transform |
|
3327 |
* and stores the results into an array of floats. |
|
3328 |
* The coordinates are stored in the arrays starting at the specified |
|
3329 |
* offset in the order <code>[x0, y0, x1, y1, ..., xn, yn]</code>. |
|
3330 |
* @param srcPts the array containing the source point coordinates. |
|
3331 |
* Each point is stored as a pair of x, y coordinates. |
|
3332 |
* @param dstPts the array into which the transformed point |
|
3333 |
* coordinates are returned. Each point is stored as a pair of |
|
3334 |
* x, y coordinates. |
|
3335 |
* @param srcOff the offset to the first point to be transformed |
|
3336 |
* in the source array |
|
3337 |
* @param dstOff the offset to the location of the first |
|
3338 |
* transformed point that is stored in the destination array |
|
3339 |
* @param numPts the number of point objects to be transformed |
|
3340 |
* @since 1.2 |
|
3341 |
*/ |
|
3342 |
public void transform(double[] srcPts, int srcOff, |
|
3343 |
float[] dstPts, int dstOff, |
|
3344 |
int numPts) { |
|
3345 |
double M00, M01, M02, M10, M11, M12; // For caching |
|
3346 |
switch (state) { |
|
3347 |
default: |
|
3348 |
stateError(); |
|
3349 |
/* NOTREACHED */ |
|
3350 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
3351 |
M00 = m00; M01 = m01; M02 = m02; |
|
3352 |
M10 = m10; M11 = m11; M12 = m12; |
|
3353 |
while (--numPts >= 0) { |
|
3354 |
double x = srcPts[srcOff++]; |
|
3355 |
double y = srcPts[srcOff++]; |
|
3356 |
dstPts[dstOff++] = (float) (M00 * x + M01 * y + M02); |
|
3357 |
dstPts[dstOff++] = (float) (M10 * x + M11 * y + M12); |
|
3358 |
} |
|
3359 |
return; |
|
3360 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
3361 |
M00 = m00; M01 = m01; |
|
3362 |
M10 = m10; M11 = m11; |
|
3363 |
while (--numPts >= 0) { |
|
3364 |
double x = srcPts[srcOff++]; |
|
3365 |
double y = srcPts[srcOff++]; |
|
3366 |
dstPts[dstOff++] = (float) (M00 * x + M01 * y); |
|
3367 |
dstPts[dstOff++] = (float) (M10 * x + M11 * y); |
|
3368 |
} |
|
3369 |
return; |
|
3370 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
3371 |
M01 = m01; M02 = m02; |
|
3372 |
M10 = m10; M12 = m12; |
|
3373 |
while (--numPts >= 0) { |
|
3374 |
double x = srcPts[srcOff++]; |
|
3375 |
dstPts[dstOff++] = (float) (M01 * srcPts[srcOff++] + M02); |
|
3376 |
dstPts[dstOff++] = (float) (M10 * x + M12); |
|
3377 |
} |
|
3378 |
return; |
|
3379 |
case (APPLY_SHEAR): |
|
3380 |
M01 = m01; M10 = m10; |
|
3381 |
while (--numPts >= 0) { |
|
3382 |
double x = srcPts[srcOff++]; |
|
3383 |
dstPts[dstOff++] = (float) (M01 * srcPts[srcOff++]); |
|
3384 |
dstPts[dstOff++] = (float) (M10 * x); |
|
3385 |
} |
|
3386 |
return; |
|
3387 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
3388 |
M00 = m00; M02 = m02; |
|
3389 |
M11 = m11; M12 = m12; |
|
3390 |
while (--numPts >= 0) { |
|
3391 |
dstPts[dstOff++] = (float) (M00 * srcPts[srcOff++] + M02); |
|
3392 |
dstPts[dstOff++] = (float) (M11 * srcPts[srcOff++] + M12); |
|
3393 |
} |
|
3394 |
return; |
|
3395 |
case (APPLY_SCALE): |
|
3396 |
M00 = m00; M11 = m11; |
|
3397 |
while (--numPts >= 0) { |
|
3398 |
dstPts[dstOff++] = (float) (M00 * srcPts[srcOff++]); |
|
3399 |
dstPts[dstOff++] = (float) (M11 * srcPts[srcOff++]); |
|
3400 |
} |
|
3401 |
return; |
|
3402 |
case (APPLY_TRANSLATE): |
|
3403 |
M02 = m02; M12 = m12; |
|
3404 |
while (--numPts >= 0) { |
|
3405 |
dstPts[dstOff++] = (float) (srcPts[srcOff++] + M02); |
|
3406 |
dstPts[dstOff++] = (float) (srcPts[srcOff++] + M12); |
|
3407 |
} |
|
3408 |
return; |
|
3409 |
case (APPLY_IDENTITY): |
|
3410 |
while (--numPts >= 0) { |
|
3411 |
dstPts[dstOff++] = (float) (srcPts[srcOff++]); |
|
3412 |
dstPts[dstOff++] = (float) (srcPts[srcOff++]); |
|
3413 |
} |
|
3414 |
return; |
|
3415 |
} |
|
3416 |
||
3417 |
/* NOTREACHED */ |
|
3418 |
} |
|
3419 |
||
3420 |
/** |
|
3421 |
* Inverse transforms the specified <code>ptSrc</code> and stores the |
|
3422 |
* result in <code>ptDst</code>. |
|
3423 |
* If <code>ptDst</code> is <code>null</code>, a new |
|
3424 |
* <code>Point2D</code> object is allocated and then the result of the |
|
3425 |
* transform is stored in this object. |
|
3426 |
* In either case, <code>ptDst</code>, which contains the transformed |
|
3427 |
* point, is returned for convenience. |
|
3428 |
* If <code>ptSrc</code> and <code>ptDst</code> are the same |
|
3429 |
* object, the input point is correctly overwritten with the |
|
3430 |
* transformed point. |
|
3431 |
* @param ptSrc the point to be inverse transformed |
|
3432 |
* @param ptDst the resulting transformed point |
|
3433 |
* @return <code>ptDst</code>, which contains the result of the |
|
3434 |
* inverse transform. |
|
3435 |
* @exception NoninvertibleTransformException if the matrix cannot be |
|
3436 |
* inverted. |
|
3437 |
* @since 1.2 |
|
3438 |
*/ |
|
3439 |
public Point2D inverseTransform(Point2D ptSrc, Point2D ptDst) |
|
3440 |
throws NoninvertibleTransformException |
|
3441 |
{ |
|
3442 |
if (ptDst == null) { |
|
3443 |
if (ptSrc instanceof Point2D.Double) { |
|
3444 |
ptDst = new Point2D.Double(); |
|
3445 |
} else { |
|
3446 |
ptDst = new Point2D.Float(); |
|
3447 |
} |
|
3448 |
} |
|
3449 |
// Copy source coords into local variables in case src == dst |
|
3450 |
double x = ptSrc.getX(); |
|
3451 |
double y = ptSrc.getY(); |
|
3452 |
switch (state) { |
|
3453 |
default: |
|
3454 |
stateError(); |
|
3455 |
/* NOTREACHED */ |
|
3456 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
3457 |
x -= m02; |
|
3458 |
y -= m12; |
|
3459 |
/* NOBREAK */ |
|
3460 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
3461 |
double det = m00 * m11 - m01 * m10; |
|
3462 |
if (Math.abs(det) <= Double.MIN_VALUE) { |
|
3463 |
throw new NoninvertibleTransformException("Determinant is "+ |
|
3464 |
det); |
|
3465 |
} |
|
3466 |
ptDst.setLocation((x * m11 - y * m01) / det, |
|
3467 |
(y * m00 - x * m10) / det); |
|
3468 |
return ptDst; |
|
3469 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
3470 |
x -= m02; |
|
3471 |
y -= m12; |
|
3472 |
/* NOBREAK */ |
|
3473 |
case (APPLY_SHEAR): |
|
3474 |
if (m01 == 0.0 || m10 == 0.0) { |
|
3475 |
throw new NoninvertibleTransformException("Determinant is 0"); |
|
3476 |
} |
|
3477 |
ptDst.setLocation(y / m10, x / m01); |
|
3478 |
return ptDst; |
|
3479 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
3480 |
x -= m02; |
|
3481 |
y -= m12; |
|
3482 |
/* NOBREAK */ |
|
3483 |
case (APPLY_SCALE): |
|
3484 |
if (m00 == 0.0 || m11 == 0.0) { |
|
3485 |
throw new NoninvertibleTransformException("Determinant is 0"); |
|
3486 |
} |
|
3487 |
ptDst.setLocation(x / m00, y / m11); |
|
3488 |
return ptDst; |
|
3489 |
case (APPLY_TRANSLATE): |
|
3490 |
ptDst.setLocation(x - m02, y - m12); |
|
3491 |
return ptDst; |
|
3492 |
case (APPLY_IDENTITY): |
|
3493 |
ptDst.setLocation(x, y); |
|
3494 |
return ptDst; |
|
3495 |
} |
|
3496 |
||
3497 |
/* NOTREACHED */ |
|
3498 |
} |
|
3499 |
||
3500 |
/** |
|
3501 |
* Inverse transforms an array of double precision coordinates by |
|
3502 |
* this transform. |
|
3503 |
* The two coordinate array sections can be exactly the same or |
|
3504 |
* can be overlapping sections of the same array without affecting the |
|
3505 |
* validity of the results. |
|
3506 |
* This method ensures that no source coordinates are |
|
3507 |
* overwritten by a previous operation before they can be transformed. |
|
3508 |
* The coordinates are stored in the arrays starting at the specified |
|
3509 |
* offset in the order <code>[x0, y0, x1, y1, ..., xn, yn]</code>. |
|
3510 |
* @param srcPts the array containing the source point coordinates. |
|
3511 |
* Each point is stored as a pair of x, y coordinates. |
|
3512 |
* @param dstPts the array into which the transformed point |
|
3513 |
* coordinates are returned. Each point is stored as a pair of |
|
3514 |
* x, y coordinates. |
|
3515 |
* @param srcOff the offset to the first point to be transformed |
|
3516 |
* in the source array |
|
3517 |
* @param dstOff the offset to the location of the first |
|
3518 |
* transformed point that is stored in the destination array |
|
3519 |
* @param numPts the number of point objects to be transformed |
|
3520 |
* @exception NoninvertibleTransformException if the matrix cannot be |
|
3521 |
* inverted. |
|
3522 |
* @since 1.2 |
|
3523 |
*/ |
|
3524 |
public void inverseTransform(double[] srcPts, int srcOff, |
|
3525 |
double[] dstPts, int dstOff, |
|
3526 |
int numPts) |
|
3527 |
throws NoninvertibleTransformException |
|
3528 |
{ |
|
3529 |
double M00, M01, M02, M10, M11, M12; // For caching |
|
3530 |
double det; |
|
3531 |
if (dstPts == srcPts && |
|
3532 |
dstOff > srcOff && dstOff < srcOff + numPts * 2) |
|
3533 |
{ |
|
3534 |
// If the arrays overlap partially with the destination higher |
|
3535 |
// than the source and we transform the coordinates normally |
|
3536 |
// we would overwrite some of the later source coordinates |
|
3537 |
// with results of previous transformations. |
|
3538 |
// To get around this we use arraycopy to copy the points |
|
3539 |
// to their final destination with correct overwrite |
|
3540 |
// handling and then transform them in place in the new |
|
3541 |
// safer location. |
|
3542 |
System.arraycopy(srcPts, srcOff, dstPts, dstOff, numPts * 2); |
|
3543 |
// srcPts = dstPts; // They are known to be equal. |
|
3544 |
srcOff = dstOff; |
|
3545 |
} |
|
3546 |
switch (state) { |
|
3547 |
default: |
|
3548 |
stateError(); |
|
3549 |
/* NOTREACHED */ |
|
3550 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
3551 |
M00 = m00; M01 = m01; M02 = m02; |
|
3552 |
M10 = m10; M11 = m11; M12 = m12; |
|
3553 |
det = M00 * M11 - M01 * M10; |
|
3554 |
if (Math.abs(det) <= Double.MIN_VALUE) { |
|
3555 |
throw new NoninvertibleTransformException("Determinant is "+ |
|
3556 |
det); |
|
3557 |
} |
|
3558 |
while (--numPts >= 0) { |
|
3559 |
double x = srcPts[srcOff++] - M02; |
|
3560 |
double y = srcPts[srcOff++] - M12; |
|
3561 |
dstPts[dstOff++] = (x * M11 - y * M01) / det; |
|
3562 |
dstPts[dstOff++] = (y * M00 - x * M10) / det; |
|
3563 |
} |
|
3564 |
return; |
|
3565 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
3566 |
M00 = m00; M01 = m01; |
|
3567 |
M10 = m10; M11 = m11; |
|
3568 |
det = M00 * M11 - M01 * M10; |
|
3569 |
if (Math.abs(det) <= Double.MIN_VALUE) { |
|
3570 |
throw new NoninvertibleTransformException("Determinant is "+ |
|
3571 |
det); |
|
3572 |
} |
|
3573 |
while (--numPts >= 0) { |
|
3574 |
double x = srcPts[srcOff++]; |
|
3575 |
double y = srcPts[srcOff++]; |
|
3576 |
dstPts[dstOff++] = (x * M11 - y * M01) / det; |
|
3577 |
dstPts[dstOff++] = (y * M00 - x * M10) / det; |
|
3578 |
} |
|
3579 |
return; |
|
3580 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
3581 |
M01 = m01; M02 = m02; |
|
3582 |
M10 = m10; M12 = m12; |
|
3583 |
if (M01 == 0.0 || M10 == 0.0) { |
|
3584 |
throw new NoninvertibleTransformException("Determinant is 0"); |
|
3585 |
} |
|
3586 |
while (--numPts >= 0) { |
|
3587 |
double x = srcPts[srcOff++] - M02; |
|
3588 |
dstPts[dstOff++] = (srcPts[srcOff++] - M12) / M10; |
|
3589 |
dstPts[dstOff++] = x / M01; |
|
3590 |
} |
|
3591 |
return; |
|
3592 |
case (APPLY_SHEAR): |
|
3593 |
M01 = m01; M10 = m10; |
|
3594 |
if (M01 == 0.0 || M10 == 0.0) { |
|
3595 |
throw new NoninvertibleTransformException("Determinant is 0"); |
|
3596 |
} |
|
3597 |
while (--numPts >= 0) { |
|
3598 |
double x = srcPts[srcOff++]; |
|
3599 |
dstPts[dstOff++] = srcPts[srcOff++] / M10; |
|
3600 |
dstPts[dstOff++] = x / M01; |
|
3601 |
} |
|
3602 |
return; |
|
3603 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
3604 |
M00 = m00; M02 = m02; |
|
3605 |
M11 = m11; M12 = m12; |
|
3606 |
if (M00 == 0.0 || M11 == 0.0) { |
|
3607 |
throw new NoninvertibleTransformException("Determinant is 0"); |
|
3608 |
} |
|
3609 |
while (--numPts >= 0) { |
|
3610 |
dstPts[dstOff++] = (srcPts[srcOff++] - M02) / M00; |
|
3611 |
dstPts[dstOff++] = (srcPts[srcOff++] - M12) / M11; |
|
3612 |
} |
|
3613 |
return; |
|
3614 |
case (APPLY_SCALE): |
|
3615 |
M00 = m00; M11 = m11; |
|
3616 |
if (M00 == 0.0 || M11 == 0.0) { |
|
3617 |
throw new NoninvertibleTransformException("Determinant is 0"); |
|
3618 |
} |
|
3619 |
while (--numPts >= 0) { |
|
3620 |
dstPts[dstOff++] = srcPts[srcOff++] / M00; |
|
3621 |
dstPts[dstOff++] = srcPts[srcOff++] / M11; |
|
3622 |
} |
|
3623 |
return; |
|
3624 |
case (APPLY_TRANSLATE): |
|
3625 |
M02 = m02; M12 = m12; |
|
3626 |
while (--numPts >= 0) { |
|
3627 |
dstPts[dstOff++] = srcPts[srcOff++] - M02; |
|
3628 |
dstPts[dstOff++] = srcPts[srcOff++] - M12; |
|
3629 |
} |
|
3630 |
return; |
|
3631 |
case (APPLY_IDENTITY): |
|
3632 |
if (srcPts != dstPts || srcOff != dstOff) { |
|
3633 |
System.arraycopy(srcPts, srcOff, dstPts, dstOff, |
|
3634 |
numPts * 2); |
|
3635 |
} |
|
3636 |
return; |
|
3637 |
} |
|
3638 |
||
3639 |
/* NOTREACHED */ |
|
3640 |
} |
|
3641 |
||
3642 |
/** |
|
3643 |
* Transforms the relative distance vector specified by |
|
3644 |
* <code>ptSrc</code> and stores the result in <code>ptDst</code>. |
|
3645 |
* A relative distance vector is transformed without applying the |
|
3646 |
* translation components of the affine transformation matrix |
|
3647 |
* using the following equations: |
|
3648 |
* <pre> |
|
3649 |
* [ x' ] [ m00 m01 (m02) ] [ x ] [ m00x + m01y ] |
|
3650 |
* [ y' ] = [ m10 m11 (m12) ] [ y ] = [ m10x + m11y ] |
|
3651 |
* [ (1) ] [ (0) (0) ( 1 ) ] [ (1) ] [ (1) ] |
|
3652 |
* </pre> |
|
3653 |
* If <code>ptDst</code> is <code>null</code>, a new |
|
3654 |
* <code>Point2D</code> object is allocated and then the result of the |
|
3655 |
* transform is stored in this object. |
|
3656 |
* In either case, <code>ptDst</code>, which contains the |
|
3657 |
* transformed point, is returned for convenience. |
|
3658 |
* If <code>ptSrc</code> and <code>ptDst</code> are the same object, |
|
3659 |
* the input point is correctly overwritten with the transformed |
|
3660 |
* point. |
|
3661 |
* @param ptSrc the distance vector to be delta transformed |
|
3662 |
* @param ptDst the resulting transformed distance vector |
|
3663 |
* @return <code>ptDst</code>, which contains the result of the |
|
3664 |
* transformation. |
|
3665 |
* @since 1.2 |
|
3666 |
*/ |
|
3667 |
public Point2D deltaTransform(Point2D ptSrc, Point2D ptDst) { |
|
3668 |
if (ptDst == null) { |
|
3669 |
if (ptSrc instanceof Point2D.Double) { |
|
3670 |
ptDst = new Point2D.Double(); |
|
3671 |
} else { |
|
3672 |
ptDst = new Point2D.Float(); |
|
3673 |
} |
|
3674 |
} |
|
3675 |
// Copy source coords into local variables in case src == dst |
|
3676 |
double x = ptSrc.getX(); |
|
3677 |
double y = ptSrc.getY(); |
|
3678 |
switch (state) { |
|
3679 |
default: |
|
3680 |
stateError(); |
|
3681 |
/* NOTREACHED */ |
|
3682 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
3683 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
3684 |
ptDst.setLocation(x * m00 + y * m01, x * m10 + y * m11); |
|
3685 |
return ptDst; |
|
3686 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
3687 |
case (APPLY_SHEAR): |
|
3688 |
ptDst.setLocation(y * m01, x * m10); |
|
3689 |
return ptDst; |
|
3690 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
3691 |
case (APPLY_SCALE): |
|
3692 |
ptDst.setLocation(x * m00, y * m11); |
|
3693 |
return ptDst; |
|
3694 |
case (APPLY_TRANSLATE): |
|
3695 |
case (APPLY_IDENTITY): |
|
3696 |
ptDst.setLocation(x, y); |
|
3697 |
return ptDst; |
|
3698 |
} |
|
3699 |
||
3700 |
/* NOTREACHED */ |
|
3701 |
} |
|
3702 |
||
3703 |
/** |
|
3704 |
* Transforms an array of relative distance vectors by this |
|
3705 |
* transform. |
|
3706 |
* A relative distance vector is transformed without applying the |
|
3707 |
* translation components of the affine transformation matrix |
|
3708 |
* using the following equations: |
|
3709 |
* <pre> |
|
3710 |
* [ x' ] [ m00 m01 (m02) ] [ x ] [ m00x + m01y ] |
|
3711 |
* [ y' ] = [ m10 m11 (m12) ] [ y ] = [ m10x + m11y ] |
|
3712 |
* [ (1) ] [ (0) (0) ( 1 ) ] [ (1) ] [ (1) ] |
|
3713 |
* </pre> |
|
3714 |
* The two coordinate array sections can be exactly the same or |
|
3715 |
* can be overlapping sections of the same array without affecting the |
|
3716 |
* validity of the results. |
|
3717 |
* This method ensures that no source coordinates are |
|
3718 |
* overwritten by a previous operation before they can be transformed. |
|
3719 |
* The coordinates are stored in the arrays starting at the indicated |
|
3720 |
* offset in the order <code>[x0, y0, x1, y1, ..., xn, yn]</code>. |
|
3721 |
* @param srcPts the array containing the source distance vectors. |
|
3722 |
* Each vector is stored as a pair of relative x, y coordinates. |
|
3723 |
* @param dstPts the array into which the transformed distance vectors |
|
3724 |
* are returned. Each vector is stored as a pair of relative |
|
3725 |
* x, y coordinates. |
|
3726 |
* @param srcOff the offset to the first vector to be transformed |
|
3727 |
* in the source array |
|
3728 |
* @param dstOff the offset to the location of the first |
|
3729 |
* transformed vector that is stored in the destination array |
|
3730 |
* @param numPts the number of vector coordinate pairs to be |
|
3731 |
* transformed |
|
3732 |
* @since 1.2 |
|
3733 |
*/ |
|
3734 |
public void deltaTransform(double[] srcPts, int srcOff, |
|
3735 |
double[] dstPts, int dstOff, |
|
3736 |
int numPts) { |
|
3737 |
double M00, M01, M10, M11; // For caching |
|
3738 |
if (dstPts == srcPts && |
|
3739 |
dstOff > srcOff && dstOff < srcOff + numPts * 2) |
|
3740 |
{ |
|
3741 |
// If the arrays overlap partially with the destination higher |
|
3742 |
// than the source and we transform the coordinates normally |
|
3743 |
// we would overwrite some of the later source coordinates |
|
3744 |
// with results of previous transformations. |
|
3745 |
// To get around this we use arraycopy to copy the points |
|
3746 |
// to their final destination with correct overwrite |
|
3747 |
// handling and then transform them in place in the new |
|
3748 |
// safer location. |
|
3749 |
System.arraycopy(srcPts, srcOff, dstPts, dstOff, numPts * 2); |
|
3750 |
// srcPts = dstPts; // They are known to be equal. |
|
3751 |
srcOff = dstOff; |
|
3752 |
} |
|
3753 |
switch (state) { |
|
3754 |
default: |
|
3755 |
stateError(); |
|
3756 |
/* NOTREACHED */ |
|
3757 |
case (APPLY_SHEAR | APPLY_SCALE | APPLY_TRANSLATE): |
|
3758 |
case (APPLY_SHEAR | APPLY_SCALE): |
|
3759 |
M00 = m00; M01 = m01; |
|
3760 |
M10 = m10; M11 = m11; |
|
3761 |
while (--numPts >= 0) { |
|
3762 |
double x = srcPts[srcOff++]; |
|
3763 |
double y = srcPts[srcOff++]; |
|
3764 |
dstPts[dstOff++] = x * M00 + y * M01; |
|
3765 |
dstPts[dstOff++] = x * M10 + y * M11; |
|
3766 |
} |
|
3767 |
return; |
|
3768 |
case (APPLY_SHEAR | APPLY_TRANSLATE): |
|
3769 |
case (APPLY_SHEAR): |
|
3770 |
M01 = m01; M10 = m10; |
|
3771 |
while (--numPts >= 0) { |
|
3772 |
double x = srcPts[srcOff++]; |
|
3773 |
dstPts[dstOff++] = srcPts[srcOff++] * M01; |
|
3774 |
dstPts[dstOff++] = x * M10; |
|
3775 |
} |
|
3776 |
return; |
|
3777 |
case (APPLY_SCALE | APPLY_TRANSLATE): |
|
3778 |
case (APPLY_SCALE): |
|
3779 |
M00 = m00; M11 = m11; |
|
3780 |
while (--numPts >= 0) { |
|
3781 |
dstPts[dstOff++] = srcPts[srcOff++] * M00; |
|
3782 |
dstPts[dstOff++] = srcPts[srcOff++] * M11; |
|
3783 |
} |
|
3784 |
return; |
|
3785 |
case (APPLY_TRANSLATE): |
|
3786 |
case (APPLY_IDENTITY): |
|
3787 |
if (srcPts != dstPts || srcOff != dstOff) { |
|
3788 |
System.arraycopy(srcPts, srcOff, dstPts, dstOff, |
|
3789 |
numPts * 2); |
|
3790 |
} |
|
3791 |
return; |
|
3792 |
} |
|
3793 |
||
3794 |
/* NOTREACHED */ |
|
3795 |
} |
|
3796 |
||
3797 |
/** |
|
3798 |
* Returns a new {@link Shape} object defined by the geometry of the |
|
3799 |
* specified <code>Shape</code> after it has been transformed by |
|
3800 |
* this transform. |
|
3801 |
* @param pSrc the specified <code>Shape</code> object to be |
|
3802 |
* transformed by this transform. |
|
3803 |
* @return a new <code>Shape</code> object that defines the geometry |
|
3804 |
* of the transformed <code>Shape</code>, or null if {@code pSrc} is null. |
|
3805 |
* @since 1.2 |
|
3806 |
*/ |
|
3807 |
public Shape createTransformedShape(Shape pSrc) { |
|
3808 |
if (pSrc == null) { |
|
3809 |
return null; |
|
3810 |
} |
|
3811 |
return new Path2D.Double(pSrc, this); |
|
3812 |
} |
|
3813 |
||
3814 |
// Round values to sane precision for printing |
|
3815 |
// Note that Math.sin(Math.PI) has an error of about 10^-16 |
|
3816 |
private static double _matround(double matval) { |
|
3817 |
return Math.rint(matval * 1E15) / 1E15; |
|
3818 |
} |
|
3819 |
||
3820 |
/** |
|
3821 |
* Returns a <code>String</code> that represents the value of this |
|
3822 |
* {@link Object}. |
|
3823 |
* @return a <code>String</code> representing the value of this |
|
3824 |
* <code>Object</code>. |
|
3825 |
* @since 1.2 |
|
3826 |
*/ |
|
3827 |
public String toString() { |
|
3828 |
return ("AffineTransform[[" |
|
3829 |
+ _matround(m00) + ", " |
|
3830 |
+ _matround(m01) + ", " |
|
3831 |
+ _matround(m02) + "], [" |
|
3832 |
+ _matround(m10) + ", " |
|
3833 |
+ _matround(m11) + ", " |
|
3834 |
+ _matround(m12) + "]]"); |
|
3835 |
} |
|
3836 |
||
3837 |
/** |
|
3838 |
* Returns <code>true</code> if this <code>AffineTransform</code> is |
|
3839 |
* an identity transform. |
|
3840 |
* @return <code>true</code> if this <code>AffineTransform</code> is |
|
3841 |
* an identity transform; <code>false</code> otherwise. |
|
3842 |
* @since 1.2 |
|
3843 |
*/ |
|
3844 |
public boolean isIdentity() { |
|
3845 |
return (state == APPLY_IDENTITY || (getType() == TYPE_IDENTITY)); |
|
3846 |
} |
|
3847 |
||
3848 |
/** |
|
3849 |
* Returns a copy of this <code>AffineTransform</code> object. |
|
3850 |
* @return an <code>Object</code> that is a copy of this |
|
3851 |
* <code>AffineTransform</code> object. |
|
3852 |
* @since 1.2 |
|
3853 |
*/ |
|
3854 |
public Object clone() { |
|
3855 |
try { |
|
3856 |
return super.clone(); |
|
3857 |
} catch (CloneNotSupportedException e) { |
|
3858 |
// this shouldn't happen, since we are Cloneable |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
7006
diff
changeset
|
3859 |
throw new InternalError(e); |
2 | 3860 |
} |
3861 |
} |
|
3862 |
||
3863 |
/** |
|
3864 |
* Returns the hashcode for this transform. |
|
3865 |
* @return a hash code for this transform. |
|
3866 |
* @since 1.2 |
|
3867 |
*/ |
|
3868 |
public int hashCode() { |
|
3869 |
long bits = Double.doubleToLongBits(m00); |
|
3870 |
bits = bits * 31 + Double.doubleToLongBits(m01); |
|
3871 |
bits = bits * 31 + Double.doubleToLongBits(m02); |
|
3872 |
bits = bits * 31 + Double.doubleToLongBits(m10); |
|
3873 |
bits = bits * 31 + Double.doubleToLongBits(m11); |
|
3874 |
bits = bits * 31 + Double.doubleToLongBits(m12); |
|
3875 |
return (((int) bits) ^ ((int) (bits >> 32))); |
|
3876 |
} |
|
3877 |
||
3878 |
/** |
|
3879 |
* Returns <code>true</code> if this <code>AffineTransform</code> |
|
3880 |
* represents the same affine coordinate transform as the specified |
|
3881 |
* argument. |
|
3882 |
* @param obj the <code>Object</code> to test for equality with this |
|
3883 |
* <code>AffineTransform</code> |
|
3884 |
* @return <code>true</code> if <code>obj</code> equals this |
|
3885 |
* <code>AffineTransform</code> object; <code>false</code> otherwise. |
|
3886 |
* @since 1.2 |
|
3887 |
*/ |
|
3888 |
public boolean equals(Object obj) { |
|
3889 |
if (!(obj instanceof AffineTransform)) { |
|
3890 |
return false; |
|
3891 |
} |
|
3892 |
||
3893 |
AffineTransform a = (AffineTransform)obj; |
|
3894 |
||
3895 |
return ((m00 == a.m00) && (m01 == a.m01) && (m02 == a.m02) && |
|
3896 |
(m10 == a.m10) && (m11 == a.m11) && (m12 == a.m12)); |
|
3897 |
} |
|
3898 |
||
3899 |
/* Serialization support. A readObject method is neccessary because |
|
3900 |
* the state field is part of the implementation of this particular |
|
3901 |
* AffineTransform and not part of the public specification. The |
|
3902 |
* state variable's value needs to be recalculated on the fly by the |
|
3903 |
* readObject method as it is in the 6-argument matrix constructor. |
|
3904 |
*/ |
|
3905 |
||
3906 |
/* |
|
3907 |
* JDK 1.2 serialVersionUID |
|
3908 |
*/ |
|
3909 |
private static final long serialVersionUID = 1330973210523860834L; |
|
3910 |
||
3911 |
private void writeObject(java.io.ObjectOutputStream s) |
|
3912 |
throws java.lang.ClassNotFoundException, java.io.IOException |
|
3913 |
{ |
|
3914 |
s.defaultWriteObject(); |
|
3915 |
} |
|
3916 |
||
3917 |
private void readObject(java.io.ObjectInputStream s) |
|
3918 |
throws java.lang.ClassNotFoundException, java.io.IOException |
|
3919 |
{ |
|
3920 |
s.defaultReadObject(); |
|
3921 |
updateState(); |
|
3922 |
} |
|
3923 |
} |