KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pj > util > TMatrix


1 package com.etymon.pj.util;
2
3 public class TMatrix {
4
5     // all methods assume a valid 3x3 matrix!
6

7     /**
8        Returns a new transformation matrix, initialized to the identity matrix.
9     */

10     public static float[][] init() {
11         float[][] id = { {1, 0, 0},
12                  {0, 1, 0},
13                  {0, 0, 1} };
14         return id;
15     }
16
17     public static String JavaDoc toString(float[][] m) {
18         return "{ {" + m[0][0] + ", " + m[0][1] + ", " + m[0][2] + "},\n" +
19             " {" + m[1][0] + ", " + m[1][1] + ", " + m[1][2] + "},\n" +
20             " {" + m[2][0] + ", " + m[2][1] + ", " + m[2][2] + "} }";
21     }
22     
23     public static float[][] toMatrix(float a, float b, float c, float d, float x, float y) {
24         float[][] m = init();
25         m[0][0] = a;
26         m[0][1] = b;
27         m[1][0] = c;
28         m[1][1] = d;
29         m[2][0] = x;
30         m[2][1] = y;
31         return m;
32     }
33
34     public static float[][] clone(float[][] m) {
35         float[][] n = init();
36         for (int r = 0; r < 3; r++) {
37             for (int c = 0; c < 3; c++) {
38                 n[r][c] = m[r][c];
39             }
40         }
41         return n;
42     }
43     
44     public static float[][] toMatrix(float x, float y) {
45         float[][] m = init();
46         m[2][0] = x;
47         m[2][1] = y;
48         return m;
49     }
50
51     public static float[][] multiply(float[][] a, float[][] b) {
52         if (a[0].length != b.length) {
53             return null;
54         }
55         float[][] c = new float[a.length][b[0].length];
56         for (int i = 0; i < a.length; i++) {
57             for (int k = 0; k < b[0].length; k++) {
58                 float s = 0;
59                 for (int j = 0; j < b.length; j++) {
60                     s = s + a[i][j] * b[j][k];
61                 }
62                 c[i][k] = s;
63             }
64         }
65         return c;
66     }
67     
68     public static float[][] IDENTITY = { {1, 0, 0},
69                          {0, 1, 0},
70                          {0, 0, 1} };
71
72 }
73
Popular Tags