1 /* 2 * This program is free software; you can redistribute it and/or 3 * modify it under the terms of the GNU General Public License as 4 * published by the Free Software Foundation; either version 2 of the 5 * License, or (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, but 8 * WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOUSE. See the GNU 10 * General Public License for more details. 11 * 12 * You should have recieved a copy of the GNU General Public License 13 * along with this program; if not write to the Free Software 14 * Foundation, inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 15 * USA 16 */ 17 package j3d; 18 import java.io.*; 19 /** 20 * A fairly conventional 3D matrix object that can transform sets of 21 * 3D points and perform a variety of manipulations on the transform 22 * 23 * @author: Yuriy Mikhaylovskiy 24 */ 25 26 public class Matrix2D implements Serializable{ 27 float sx, sy; 28 //float angle; 29 float xx, xy, xo; 30 float yx, yy, yo; 31 static final double pi = 3.14159265; 32 /** Create a new unit matrix */ Matrix2D()33 public Matrix2D () { 34 xx = 1.0f; 35 yy = 1.0f; 36 sx = 1.0f; 37 sy = 1.0f; 38 } 39 /** Scale by f in all dimensions */ scale(float f)40 public void scale(float f) { 41 sx *= f; 42 sy *= f; 43 xx *= f; 44 xy *= f; 45 xo *= f; 46 yx *= f; 47 yy *= f; 48 yo *= f; 49 } 50 /** Scale along each axis independently */ scale(float xf, float yf, float zf)51 void scale(float xf, float yf, float zf) { 52 sx *= xf; 53 sy *= yf; 54 xx *= xf; 55 xy *= xf; 56 xo *= xf; 57 yx *= yf; 58 yy *= yf; 59 yo *= yf; 60 } 61 /** Translate the origin */ translate(float x, float y)62 public void translate(float x, float y) { 63 xo += x; 64 yo += y; 65 } 66 /** rotate theta degrees about the 0,0 point */ rot(double theta)67 public void rot(double theta) { 68 //angle += theta; 69 theta *= (pi / 180); 70 double ct = Math.cos(theta); 71 double st = Math.sin(theta); 72 73 float Nyx = (float) (yx * ct + xx * st); 74 float Nyy = (float) (yy * ct + xy * st); 75 float Nyo = (float) (yo * ct + xo * st); 76 77 float Nxx = (float) (xx * ct - yx * st); 78 float Nxy = (float) (xy * ct - yy * st); 79 float Nxo = (float) (xo * ct - yo * st); 80 81 yo = Nyo; 82 yx = Nyx; 83 yy = Nyy; 84 xo = Nxo; 85 xx = Nxx; 86 xy = Nxy; 87 } rot(double st, double ct)88 public void rot(double st, double ct) { 89 90 float Nyx = (float) (yx * ct + xx * st); 91 float Nyy = (float) (yy * ct + xy * st); 92 float Nyo = (float) (yo * ct + xo * st); 93 94 float Nxx = (float) (xx * ct - yx * st); 95 float Nxy = (float) (xy * ct - yy * st); 96 float Nxo = (float) (xo * ct - yo * st); 97 98 yo = Nyo; 99 yx = Nyx; 100 yy = Nyy; 101 xo = Nxo; 102 xx = Nxx; 103 xy = Nxy; 104 } 105 /** Multiply this matrix by a second: M = M*R */ mult(Matrix3D rhs)106 public void mult(Matrix3D rhs) { 107 float lxx = xx * rhs.xx + yx * rhs.xy; 108 float lxy = xy * rhs.xx + yy * rhs.xy; 109 float lxo = xo * rhs.xx + yo * rhs.xy + rhs.xo; 110 111 float lyx = xx * rhs.yx + yx * rhs.yy; 112 float lyy = xy * rhs.yx + yy * rhs.yy; 113 float lyo = xo * rhs.yx + yo * rhs.yy + rhs.yo; 114 115 xx = lxx; 116 xy = lxy; 117 xo = lxo; 118 119 yx = lyx; 120 yy = lyy; 121 yo = lyo; 122 } 123 124 /** Reinitialize to the unit matrix */ unit()125 public void unit() { 126 xo = 0; 127 xx = 1; 128 xy = 0; 129 yo = 0; 130 yx = 0; 131 yy = 1; 132 } 133 /** Transform nvert points from v into tv. v contains the input 134 coordinates in floating point. Three successive entries in 135 the array constitute a point. tv ends up holding the transformed 136 points as integers; three successive entries per point */ transform(float v[], int tv[], int nvert)137 public void transform(float v[], int tv[], int nvert) { 138 float lxx = xx, lxy = xy, lxo = xo; 139 float lyx = yx, lyy = yy, lyo = yo; 140 for (int i = nvert * 2; (i -= 2) >= 0;) { 141 float x = v[i]; 142 float y = v[i + 1]; 143 tv[i ] = (int) (x * lxx + y * lxy + lxo); 144 tv[i + 1] = (int) (x * lyx + y * lyy + lyo); 145 } 146 } toString()147 public String toString() { 148 return ("\n["+xx+"\t"+xy+"\t"+xo+"]\n"+ 149 "["+yx+"\t"+yy+"\t"+yo+"]\n"); 150 } 151 } 152