/Users/lyon/j4p/src/math/Mat3.java

1    package math; 
2     
3    import java.awt.*; 
4     
5    public class Mat3 { 
6        double a[] [] = 
7                {{1d, 0, 0}, 
8                 {0d, 1, 0}, 
9                 {0d, 0, 1}}; 
10    
11    
12       public void setTranslation(double tx, double ty) { 
13           a[0][0] = 1; 
14           a[1][1] = 1; 
15           a[2][2] = 1; 
16           a[0][2] = tx; 
17           a[1][2] = ty; 
18       } 
19    
20       public void setScale(double sx, double sy) { 
21           a[0][0] = sx; 
22           a[1][1] = sy; 
23           a[2][2] = 1; 
24       } 
25    
26       public void setRotation(double theta) { 
27           theta = theta * Math.PI / 180; 
28           double cas = Math.cos(theta); 
29           double sas = Math.sin(theta); 
30           a[0][0] = cas; 
31           a[1][1] = cas; 
32           a[0][1] = -sas; 
33           a[1][0] = sas; 
34       } 
35    
36       public void setShear(double shx, double shy) { 
37           a[0][0] = 1; 
38           a[1][1] = 1; 
39           a[2][2] = 1; 
40           a[0][1] = shx; 
41           a[1][0] = shy; 
42       } 
43   // a00 a01 a02 
44   // a10 a11 a12 
45   // a20 a21 a22 
46       public void setPerspective(double px, double py) { 
47           a[0][0] = 1; 
48           a[1][1] = 1; 
49           a[2][2] = 1; 
50           a[2][0] = px; 
51           a[2][1] = py; 
52       } 
53    
54       public static void main(String args[]) { 
55           Mat3 tr1 = new Mat3(); 
56           Mat3 tr2 = new Mat3(); 
57           Mat3 sc = new Mat3(); 
58           Mat3 at; 
59    
60           tr1.setTranslation(1, 1); 
61           sc.setScale(2, 2); 
62           tr2.setTranslation(-1, -1); 
63    
64           at = tr1.multiply(sc); 
65           at = at.multiply(tr2); 
66           System.out.println("tr1="); 
67           tr1.print(); 
68           System.out.println("tr2="); 
69           tr2.print(); 
70           System.out.println("sc="); 
71           sc.print(); 
72           System.out.println("at="); 
73           at.print(); 
74    
75           // at.print(); 
76           //at = new Mat3(); 
77           //int x[] = at.multiply(1, 2, 1); 
78           // for (int i = 0; i < x.length; i++) 
79           //    System.out.println(x[i]); 
80    
81       } 
82    
83       public Polygon transform(Polygon p) { 
84           Polygon pp = new Polygon(); 
85           int x[]; 
86           for (int i = 0; i < p.npoints; i++) { 
87               x = multiply(p.xpoints[i], p.ypoints[i], 1); 
88               pp.addPoint(x[0], x[1]); 
89           } 
90           return pp; 
91       } 
92    
93       public static int[] centroid(Polygon p) { 
94           int x[] = new int[2]; 
95           int xsum = 0; 
96           int ysum = 0; 
97           for (int i = 0; i < p.npoints; i++) { 
98               xsum += p.xpoints[i]; 
99               ysum += p.ypoints[i]; 
100          } 
101          x[0] = xsum / p.npoints; 
102          x[1] = ysum / p.npoints; 
103          return x; 
104      } 
105   
106      public Mat3() { 
107      }; 
108      public Mat3(double a_[][]) { 
109          a = a_; 
110      } 
111   
112      public double[][] getArray() { 
113          return a; 
114      } 
115   
116  /* 
117  To generate a time-optimal 3x3 matrix inversion, use maple: 
118  with(linalg):readlib(C): 
119  a:=array(0..2,0..2,[]): 
120   b:=array(0..2,0..2,[]): 
121   b:=inverse(matrix(a)): 
122   C(b,optimized); 
123   
124  */ 
125      public Mat3 invert() { 
126          double b[] [] = new double[3][3]; 
127          double t4 = a[0][0] * a[1][1]; 
128          double t6 = a[0][0] * a[1][2]; 
129          double t8 = a[0][1] * a[1][0]; 
130          double t10 = a[0][2] * a[1][0]; 
131          double t12 = a[0][1] * a[2][0]; 
132          double t14 = a[0][2] * a[2][0]; 
133          double t17 = 
134                  1 / (-t4 * a[2][2] + t6 * a[2][1] + t8 * a[2][2] - t10 * a[2][1] - t12 * a[1][2] + t14 * a 
135                  [1][1]); 
136          b[0][0] = -(a[1][1] * a[2][2] - a[1][2] * a[2][1]) * t17; 
137          b[0][1] = -(-a[0][1] * a[2][2] + a[0][2] * a[2][1]) * t17; 
138          b[0][2] = (-a[0][1] * a[1][2] + a[0][2] * a[1][1]) * t17; 
139          b[1][0] = (a[1][0] * a[2][2] - a[1][2] * a[2][0]) * t17; 
140          b[1][1] = (-a[0][0] * a[2][2] + t14) * t17; 
141          b[1][2] = -(-t6 + t10) * t17; 
142          b[2][0] = (-a[1][0] * a[2][1] + a[1][1] * a[2][0]) * t17; 
143          b[2][1] = -(-a[0][0] * a[2][1] + t12) * t17; 
144          b[2][2] = (-t4 + t8) * t17; 
145   
146          return new Mat3(b); 
147      } 
148  /* To generate a new matrix, transposed: 
149  with(linalg):readlib(C):a:=matrix(3,3); 
150   b:=transpose(a);C(b,optimized); 
151  */ 
152      public Mat3 transpose() { 
153          double b[][] = new double[3][3]; 
154          b[0][0] = a[0][0]; 
155          b[0][1] = a[1][0]; 
156          b[0][2] = a[2][0]; 
157          b[1][0] = a[0][1]; 
158          b[1][1] = a[1][1]; 
159          b[1][2] = a[2][1]; 
160          b[2][0] = a[0][2]; 
161          b[2][1] = a[1][2]; 
162          b[2][2] = a[2][2]; 
163          return new Mat3(b); 
164      } 
165   
166   
167      public Mat3 multiply(Mat3 bmat3) { 
168          double WW [][] = new double[3][3]; 
169          double b [][] = bmat3.getArray(); 
170          WW[0][0] = a[0][0] * b[0][0] + a[0][1] * b[1][0] + a[0][2] * b[2][0]; 
171          WW[0][1] = a[0][0] * b[0][1] + a[0][1] * b[1][1] + a[0][2] * b[2][1]; 
172          WW[0][2] = a[0][0] * b[0][2] + a[0][1] * b[1][2] + a[0][2] * b[2][2]; 
173          WW[1][0] = a[1][0] * b[0][0] + a[1][1] * b[1][0] + a[1][2] * b[2][0]; 
174          WW[1][1] = a[1][0] * b[0][1] + a[1][1] * b[1][1] + a[1][2] * b[2][1]; 
175          WW[1][2] = a[1][0] * b[0][2] + a[1][1] * b[1][2] + a[1][2] * b[2][2]; 
176          WW[2][0] = a[2][0] * b[0][0] + a[2][1] * b[1][0] + a[2][2] * b[2][0]; 
177          WW[2][1] = a[2][0] * b[0][1] + a[2][1] * b[1][1] + a[2][2] * b[2][1]; 
178          WW[2][2] = a[2][0] * b[0][2] + a[2][1] * b[1][2] + a[2][2] * b[2][2]; 
179          return (new Mat3(WW)); 
180      } 
181   
182  /* 
183  In maple: 
184  vec3:=vector([v1, v2, v3]): 
185  C(multiply(matrix(a),vec3),optimized): 
186   
187  does a post-multiplication, v*A 
188  */ 
189   
190      public int[] multiply(int v1, int v2, int v3) { 
191          int x[] = new int[3]; 
192          x[0] = (int) (a[0][0] * v1 + a[0][1] * v2 + a[0][2] * v3); 
193          x[1] = (int) (a[1][0] * v1 + a[1][1] * v2 + a[1][2] * v3); 
194          x[2] = (int) (a[2][0] * v1 + a[2][1] * v2 + a[2][2] * v3); 
195          return x; 
196      } 
197   
198      public float[] multiply(float v1, float v2, float v3) { 
199          float x[] = new float[3]; 
200          x[0] = (int) (a[0][0] * v1 + a[0][1] * v2 + a[0][2] * v3); 
201          x[1] = (int) (a[1][0] * v1 + a[1][1] * v2 + a[1][2] * v3); 
202          x[2] = (int) (a[2][0] * v1 + a[2][1] * v2 + a[2][2] * v3); 
203          return x; 
204      } 
205   
206  // The following is premultiplied! 
207      public int[] premultiply(int v1, int v2) { 
208          int x[] = new int[2]; 
209          x[0] = (int) (v1 * a[0][0] + v2 * a[1][0] + a[2][0]); 
210          x[1] = (int) (v1 * a[0][1] + v2 * a[1][1] + a[2][1]); 
211          return x; 
212      } 
213   
214  /* 
215  In maple: 
216  vec3:=vector([v1, v2, v3]): 
217  C(multiply(vec3,matrix(a)),optimized): 
218   
219  does a pre-multiplication, v*A 
220  */ 
221      public double[] premultiply(double v1, double v2, double v3) { 
222          double x[] = new double[3]; 
223          x[0] = v1 * a[0][0] + v2 * a[1][0] + v3 * a[2][0]; 
224          x[1] = v1 * a[0][1] + v2 * a[1][1] + v3 * a[2][1]; 
225          x[2] = v1 * a[0][2] + v2 * a[1][2] + v3 * a[2][2]; 
226          return x; 
227      } 
228   
229      public float[] premultiply(float v1, float v2, float v3) { 
230          float x[] = new float[3]; 
231          x[0] = (float) (v1 * a[0][0] + v2 * a[1][0] + v3 * a[2][0]); 
232          x[1] = (float) (v1 * a[0][1] + v2 * a[1][1] + v3 * a[2][1]); 
233          x[2] = (float) (v1 * a[0][2] + v2 * a[1][2] + v3 * a[2][2]); 
234          return x; 
235      } 
236   
237      public void print(String s) { 
238          System.out.println(s); 
239          print(); 
240      } 
241   
242      public void print() { 
243          for (int i = 0; i < a.length; i++) { 
244              for (int j = 0; j < a[0].length; j++) 
245                  System.out.print(a[i][j] + " "); 
246              System.out.println(); 
247          } 
248      } 
249   
250  } 
251   
252