KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > IntAffineMatrix


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.Point;
15
16 public final class IntAffineMatrix {
17     
18     public static final IntAffineMatrix IDENTITY = new IntAffineMatrix(1, 0, 0, 0, 1, 0);
19     public static final IntAffineMatrix ROT_90 = new IntAffineMatrix(0, -1, 0, 1, 0, 0);
20     public static final IntAffineMatrix ROT_180 = new IntAffineMatrix(-1, 0, 0, 0, -1, 0);
21     public static final IntAffineMatrix ROT_270 = new IntAffineMatrix(0, 1, 0, -1, 0, 0);
22     public static final IntAffineMatrix FLIP_XAXIS = new IntAffineMatrix(1, 0, 0, 0, -1, 0);
23     public static final IntAffineMatrix FLIP_YAXIS = new IntAffineMatrix(-1, 0, 0, 0, 1, 0);
24     
25     int m11;
26     int m12;
27     int m13;
28     int m21;
29     int m22;
30     int m23;
31     
32     public IntAffineMatrix() {
33         this(1, 0, 0, 0, 1, 0);
34     }
35     
36     public IntAffineMatrix(int _m11, int _m12, int _m13, int _m21, int _m22, int _m23) {
37         m11 = _m11;
38         m12 = _m12;
39         m13 = _m13;
40         m21 = _m21;
41         m22 = _m22;
42         m23 = _m23;
43     }
44     
45     public IntAffineMatrix(IntAffineMatrix toCopy) {
46         this(toCopy.m11, toCopy.m12, toCopy.m13, toCopy.m21, toCopy.m22, toCopy.m23);
47     }
48     
49     public IntAffineMatrix inverse() {
50         int det = det();
51         
52         return new IntAffineMatrix(
53                 m22 / det, -m12 / det, (m12 * m23 - m22 * m13) / det,
54                 -m21 / det, m11 / det, (m21 * m13 - m11 * m23) / det);
55     }
56     
57     /**
58      * Returns a rotation matrix that would make a vector pointing directly up
59      * point in the given direction.
60      *
61      * @param swtDirectionConstant
62      * @return a rotation matrix that would make a vector pointing directly up
63      * point in the given direction
64      */

65     public static IntAffineMatrix getRotation(int swtDirectionConstant) {
66         switch(swtDirectionConstant) {
67         case SWT.RIGHT: return ROT_90;
68         case SWT.BOTTOM: return ROT_180;
69         case SWT.LEFT: return ROT_270;
70         }
71         
72         return IDENTITY;
73     }
74     
75     public int det() {
76         return m11 * m22 - m12 * m21;
77     }
78     
79     public static IntAffineMatrix translation(int x, int y) {
80         return new IntAffineMatrix(1, 0, x, 0, 1, y);
81     }
82  
83     public static IntAffineMatrix translation(Point vector) {
84         return new IntAffineMatrix(1, 0, vector.x, 0, 1, vector.y);
85     }
86     
87     /**
88      * Returns a new matrix formed by applying the argument transformation followed
89      * by the receiver. The receiver and the argument are unmodified.
90      *
91      * @param matrix matrix to transform
92      * @return a new transformed matrix
93      */

94     public IntAffineMatrix multiply(IntAffineMatrix m) {
95         IntAffineMatrix result = new IntAffineMatrix(
96                 m11 * m.m11 + m12 * m.m21,
97                 m11 * m.m12 + m12 * m.m22,
98                 m11 * m.m13 + m12 * m.m23 + m13,
99                 m21 * m.m11 + m22 * m.m21,
100                 m21 * m.m12 + m22 * m.m22,
101                 m21 * m.m13 + m22 * m.m23 + m23
102         );
103         return result;
104     }
105     
106 // /**
107
// * Applies the reciever's transformation to the given matrix. For example, if the
108
// * argument is a translation and the reciever is a rotation about the origin,
109
// * the argument will become a translation followed by a rotation about the origin.
110
// *
111
// * @param m matrix to be modified
112
// */
113
// public void transform (IntAffineMatrix m) {
114
// m.m11 = m11 * m.m11 + m12 * m.m21;
115
// m.m12 = m11 * m.m12 + m12 * m.m22;
116
// m.m13 = m11 * m.m13 + m12 * m.m23 + m13;
117
// m.m21 = m21 * m.m11 + m22 * m.m21;
118
// m.m22 = m21 * m.m12 + m22 * m.m22;
119
// m.m23 = m21 * m.m13 + m22 * m.m23 + m23;
120
// }
121

122     /**
123      * Copies and transforms a set of points from the source shape to the dest
124      *
125      * @param source source shape (alternating x and y values, starting with an x value)
126      * @param sourcePos index of the first point in the source array to transform (the value
127      * 3 indicates the 3rd x, y pair in the array, which is at position 6)
128      * @param dest destination shape (alternating x and y values, starting with an x value). A
129      * portion of this array will be overwritten.
130      * @param destPos first point to overwrite in the dest array (each x, y pair is considered
131      * 1 position)
132      * @param length number of points to copy
133      */

134     public void transform(int[] source, int sourcePos, int[] dest, int destPos, int length) {
135         int nextSrc = sourcePos * 2;
136         int nextDest = destPos * 2;
137         for(int i = 0; i < length; i++) {
138             int sourcex = source[nextSrc++];
139             int sourcey = source[nextSrc++];
140             
141             dest[nextDest++] = getx(sourcex, sourcey);
142             dest[nextDest++] = gety(sourcex, sourcey);
143         }
144     }
145
146     /**
147      * @param sourcex
148      * @param sourcey
149      * @return
150      */

151     public int gety(int sourcex, int sourcey) {
152         return sourcex * m21 + sourcey * m22 + m23;
153     }
154
155     /**
156      * @param sourcex
157      * @param sourcey
158      * @return
159      */

160     public int getx(int sourcex, int sourcey) {
161         return sourcex * m11 + sourcey * m12 + m13;
162     }
163     
164     public Shape transform(Shape toTransform) {
165         int[] newArray = new int[toTransform.used];
166         transform(toTransform.data, 0, newArray, 0, toTransform.used / 2);
167         return new Shape(newArray);
168     }
169     
170     /**
171      * Transforms a point, returning the newly transformed point. The original
172      * point is unmodified.
173      *
174      * @param toTransform point to transform
175      * @return trantsformed point
176      */

177     public Point multiply(Point toTransform) {
178         return new Point(toTransform.x * m11 + toTransform.y * m12 + m13,
179                 toTransform.x * m21 + toTransform.y * m22 + m23);
180     }
181 }
182
Popular Tags