01: package com.etymon.pj.util;
02:
03: public class TMatrix {
04:
05: // all methods assume a valid 3x3 matrix!
06:
07: /**
08: Returns a new transformation matrix, initialized to the identity matrix.
09: */
10: public static float[][] init() {
11: float[][] id = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
12: return id;
13: }
14:
15: public static String toString(float[][] m) {
16: return "{ {" + m[0][0] + ", " + m[0][1] + ", " + m[0][2]
17: + "},\n" + " {" + m[1][0] + ", " + m[1][1] + ", "
18: + m[1][2] + "},\n" + " {" + m[2][0] + ", " + m[2][1]
19: + ", " + m[2][2] + "} }";
20: }
21:
22: public static float[][] toMatrix(float a, float b, float c,
23: 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 }, { 0, 1, 0 },
69: { 0, 0, 1 } };
70:
71: }
|