1 31 package org.pdfbox.util; 32 33 import java.awt.geom.AffineTransform ; 34 35 41 public class Matrix implements Cloneable 42 { 43 private float[] single = 44 { 45 1,0,0, 46 0,1,0, 47 0,0,1 48 }; 49 50 53 public Matrix() 54 { 55 } 57 58 63 public AffineTransform createAffineTransform() 64 { 65 AffineTransform retval = new AffineTransform ( 66 single[0], single[1], 67 single[3], single[4], 68 single[6], single[7] ); 69 return retval; 70 } 71 72 77 public void setFromAffineTransform( AffineTransform af ) 78 { 79 single[0] = (float)af.getScaleX(); 80 single[1] = (float)af.getShearY(); 81 single[3] = (float)af.getShearX(); 82 single[4] = (float)af.getScaleY(); 83 single[6] = (float)af.getTranslateX(); 84 single[7] = (float)af.getTranslateY(); 85 } 86 87 95 public float getValue( int row, int column ) 96 { 97 return single[row*3+column]; 98 } 99 100 107 public void setValue( int row, int column, float value ) 108 { 109 single[row*3+column] = value; 110 } 111 112 117 public float[][] getValues() 118 { 119 float[][] retval = new float[3][3]; 120 retval[0][0] = single[0]; 121 retval[0][1] = single[1]; 122 retval[0][2] = single[2]; 123 retval[1][0] = single[3]; 124 retval[1][1] = single[4]; 125 retval[1][2] = single[5]; 126 retval[2][0] = single[6]; 127 retval[2][1] = single[7]; 128 retval[2][2] = single[8]; 129 return retval; 130 } 131 132 137 public double[][] getValuesAsDouble() 138 { 139 double[][] retval = new double[3][3]; 140 retval[0][0] = single[0]; 141 retval[0][1] = single[1]; 142 retval[0][2] = single[2]; 143 retval[1][0] = single[3]; 144 retval[1][1] = single[4]; 145 retval[1][2] = single[5]; 146 retval[2][0] = single[6]; 147 retval[2][1] = single[7]; 148 retval[2][2] = single[8]; 149 return retval; 150 } 151 152 159 public Matrix multiply( Matrix b ) 160 { 161 Matrix result = new Matrix(); 162 163 float[] bMatrix = b.single; 164 float[] resultMatrix = result.single; 165 resultMatrix[0] = single[0] * bMatrix[0] + single[1] * bMatrix[3] + single[2] * bMatrix[6]; 166 resultMatrix[1] = single[0] * bMatrix[1] + single[1] * bMatrix[4] + single[2] * bMatrix[7]; 167 resultMatrix[2] = single[0] * bMatrix[2] + single[1] * bMatrix[5] + single[2] * bMatrix[8]; 168 resultMatrix[3] = single[3] * bMatrix[0] + single[4] * bMatrix[3] + single[5] * bMatrix[6]; 169 resultMatrix[4] = single[3] * bMatrix[1] + single[4] * bMatrix[4] + single[5] * bMatrix[7]; 170 resultMatrix[5] = single[3] * bMatrix[2] + single[4] * bMatrix[5] + single[5] * bMatrix[8]; 171 resultMatrix[6] = single[6] * bMatrix[0] + single[7] * bMatrix[3] + single[8] * bMatrix[6]; 172 resultMatrix[7] = single[6] * bMatrix[1] + single[7] * bMatrix[4] + single[8] * bMatrix[7]; 173 resultMatrix[8] = single[6] * bMatrix[2] + single[7] * bMatrix[5] + single[8] * bMatrix[8]; 174 175 return result; 176 } 177 178 183 public Matrix extractScaling() 184 { 185 Matrix retval = new Matrix(); 186 187 retval.single[0] = this.single[0]; 188 retval.single[4] = this.single[4]; 189 190 return retval; 191 } 192 193 200 public static Matrix getScaleInstance( float x, float y) 201 { 202 Matrix retval = new Matrix(); 203 204 retval.single[0] = x; 205 retval.single[4] = y; 206 207 return retval; 208 } 209 210 215 public Matrix extractTranslating() 216 { 217 Matrix retval = new Matrix(); 218 219 retval.single[6] = this.single[6]; 220 retval.single[7] = this.single[7]; 221 222 return retval; 223 } 224 225 232 public static Matrix getTranslatingInstance( float x, float y) 233 { 234 Matrix retval = new Matrix(); 235 236 retval.single[6] = x; 237 retval.single[7] = y; 238 239 return retval; 240 } 241 242 246 public Object clone() 247 { 248 Matrix clone = new Matrix(); 249 System.arraycopy( single, 0, clone.single, 0, 9 ); 250 return clone; 251 } 252 253 258 public Matrix copy() 259 { 260 return (Matrix) clone(); 261 } 262 263 268 public String toString() 269 { 270 StringBuffer result = new StringBuffer ( "" ); 271 result.append( "[[" ); 272 result.append( single[0] + "," ); 273 result.append( single[1] + "," ); 274 result.append( single[2] + "]["); 275 result.append( single[3] + "," ); 276 result.append( single[4] + "," ); 277 result.append( single[5] + "]["); 278 result.append( single[6] + "," ); 279 result.append( single[7] + "," ); 280 result.append( single[8] + "]]"); 281 282 return result.toString(); 283 } 284 285 289 public float getXScale() 290 { 291 float xScale = single[0]; 292 293 310 if( !(single[1]==0.0f && single[3]==0.0f) ) 311 { 312 xScale = (float)Math.sqrt(Math.pow(single[0], 2)+ 313 Math.pow(single[1], 2)); 314 } 315 return xScale; 316 } 317 318 322 public float getYScale() 323 { 324 float yScale = single[4]; 325 if( !(single[1]==0.0f && single[3]==0.0f) ) 326 { 327 yScale = (float)Math.sqrt(Math.pow(single[3], 2)+ 328 Math.pow(single[4], 2)); 329 } 330 return yScale; 331 } 332 333 337 public float getXPosition() 338 { 339 return single[6]; 340 } 341 342 346 public float getYPosition() 347 { 348 return single[7]; 349 } 350 } | Popular Tags |