1 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 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 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 122 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 151 public int gety(int sourcex, int sourcey) { 152 return sourcex * m21 + sourcey * m22 + m23; 153 } 154 155 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 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 |