summaryrefslogtreecommitdiff
path: root/platform/java/src/com/artifex/mupdf/fitz/Matrix.java
blob: ac78f91b1b2e7162c6d3f54410e89f3c93ebeef5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package com.artifex.mupdf.fitz;

public class Matrix
{
	public float a, b, c, d, e, f;

	public Matrix(float a, float b, float c, float d, float e, float f) {
		this.a = a;
		this.b = b;
		this.c = c;
		this.d = d;
		this.e = e;
		this.f = f;
	}

	public Matrix(float a, float b, float c, float d) {
		this(a, b, c, d, 0, 0);
	}

	public Matrix(float a, float d) {
		this(a, 0, 0, d, 0, 0);
	}

	public Matrix(float a) {
		this(a, 0, 0, a, 0, 0);
	}

	public Matrix() {
		this(1, 0, 0, 1, 0, 0);
	}

	public Matrix(Matrix copy) {
		this(copy.a, copy.b, copy.c, copy.d, copy.e, copy.f);
	}

	public Matrix(Matrix one, Matrix two) {
		a = one.a * two.a + one.b * two.c;
		b = one.a * two.b + one.b * two.d;
		c = one.c * two.a + one.d * two.c;
		d = one.c * two.b + one.d * two.d;
		e = one.e * two.a + one.f * two.c + two.e;
		f = one.e * two.b + one.f * two.d + two.f;
	}

	public Matrix concat(Matrix m) {
		float a = this.a * m.a + this.b * m.c;
		float b = this.a * m.b + this.b * m.d;
		float c = this.c * m.a + this.d * m.c;
		float d = this.c * m.b + this.d * m.d;
		float e = this.e * m.a + this.f * m.c + m.e;
		this.f = this.e * m.b + this.f * m.d + m.f;

		this.a = a;
		this.b = b;
		this.c = c;
		this.d = d;
		this.e = e;

		return this;
	}

	public Matrix scale(float sx, float sy) {
		a *= sx;
		b *= sx;
		c *= sy;
		d *= sy;
		return this;
	}

	public Matrix scale(float s) {
		return scale(s, s);
	}

	public Matrix translate(float tx, float ty) {
		e += tx * a + ty * c;
		f += tx * b + ty * d;
		return this;
	}

	public Matrix rotate(float degrees) {
		while (degrees < 0)
			degrees += 360;
		while (degrees >= 360)
			degrees -= 360;

		if (Math.abs(0 - degrees) < 0.0001) {
			// Nothing to do
		} else if (Math.abs(90 - degrees) < 0.0001) {
			float save_a = a;
			float save_b = b;
			a = c;
			b = d;
			c = -save_a;
			d = -save_b;
		} else if (Math.abs(180 - degrees) < 0.0001) {
			a = -a;
			b = -b;
			c = -c;
			d = -d;
		} else if (Math.abs(270 - degrees) < 0.0001) {
			float save_a = a;
			float save_b = b;
			a = -c;
			b = -d;
			c = save_a;
			d = save_b;
		} else {
			float sin = (float)Math.sin(degrees * Math.PI / 180.0);
			float cos = (float)Math.cos(degrees * Math.PI / 180.0);
			float save_a = a;
			float save_b = b;
			a = cos * save_a + sin * c;
			b = cos * save_b + sin * d;
			c = -sin * save_a + cos * c;
			d = -sin * save_b + cos * d;
		}
		return this;
	}

	public String toString() {
		return "[" + a + " " + b + " " + c + " " + d + " " + e + " " + f + "]";
	}

	public static Matrix Identity() {
		return new Matrix(1, 0, 0, 1, 0, 0);
	}

	public static Matrix Scale(float x) {
		return new Matrix(x, 0, 0, x, 0, 0);
	}

	public static Matrix Scale(float x, float y) {
		return new Matrix(x, 0, 0, y, 0, 0);
	}

	public static Matrix Translate(float x, float y) {
		return new Matrix(1, 0, 0, 1, x, y);
	}

	public static Matrix Rotate(float degrees) {
		float sin, cos;

		while (degrees < 0)
			degrees += 360;
		while (degrees >= 360)
			degrees -= 360;

		if (Math.abs(0 - degrees) < 0.0001) {
			sin = 0;
			cos = 1;
		} else if (Math.abs(90 - degrees) < 0.0001) {
			sin = 1;
			cos = 0;
		} else if (Math.abs(180 - degrees) < 0.0001) {
			sin = 0;
			cos = -1;
		} else if (Math.abs(270 - degrees) < 0.0001) {
			sin = -1;
			cos = 0;
		} else {
			sin = (float)Math.sin(degrees * Math.PI / 180.0);
			cos = (float)Math.cos(degrees * Math.PI / 180.0);
		}

		return new Matrix(cos, sin, -sin, cos, 0, 0);
	}
}