Skip to content

Commit 175f31d

Browse files
committed
Flesh out the Javadoc for PMatrix.
1 parent a272fa8 commit 175f31d

3 files changed

Lines changed: 217 additions & 15 deletions

File tree

core/src/processing/core/PMatrix.java

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,21 @@
2424
package processing.core;
2525

2626

27+
/**
28+
* A matrix is used to define graphical transformations. PMatrix is the common
29+
* interface for both the 2D and 3D matrix classes in Processing. A matrix is a
30+
* grid of numbers, which can be multiplied by a vector to give another vector.
31+
* Multiplying a point by a particular matrix might translate it, rotate it,
32+
* or carry out a combination of transformations.
33+
*
34+
* Multiplying matrices by each other combines their effects; use the
35+
* {@code apply} and {@code preApply} methods for this.
36+
*/
2737
public interface PMatrix {
2838

39+
/**
40+
* Make this an identity matrix. Multiplying by it will have no effect.
41+
*/
2942
public void reset();
3043

3144
/**
@@ -40,13 +53,26 @@ public interface PMatrix {
4053
public float[] get(float[] target);
4154

4255

56+
/**
57+
* Make this matrix become a copy of src.
58+
*/
4359
public void set(PMatrix src);
4460

61+
/**
62+
* Set the contents of this matrix to the contents of source. Fills the
63+
* matrix left-to-right, starting in the top row.
64+
*/
4565
public void set(float[] source);
4666

67+
/**
68+
* Set the matrix content to this 2D matrix or its 3D equivalent.
69+
*/
4770
public void set(float m00, float m01, float m02,
4871
float m10, float m11, float m12);
4972

73+
/**
74+
* Set the matrix content to the 3D matrix supplied, if this matrix is 3D.
75+
*/
5076
public void set(float m00, float m01, float m02, float m03,
5177
float m10, float m11, float m12, float m13,
5278
float m20, float m21, float m22, float m23,
@@ -77,18 +103,30 @@ public void set(float m00, float m01, float m02, float m03,
77103

78104
public void shearY(float angle);
79105

80-
/**
106+
/**
81107
* Multiply this matrix by another.
82108
*/
83109
public void apply(PMatrix source);
84110

111+
/**
112+
* Multiply this matrix by another.
113+
*/
85114
public void apply(PMatrix2D source);
86115

116+
/**
117+
* Multiply this matrix by another.
118+
*/
87119
public void apply(PMatrix3D source);
88120

121+
/**
122+
* Multiply this matrix by another.
123+
*/
89124
public void apply(float n00, float n01, float n02,
90125
float n10, float n11, float n12);
91126

127+
/**
128+
* Multiply this matrix by another.
129+
*/
92130
public void apply(float n00, float n01, float n02, float n03,
93131
float n10, float n11, float n12, float n13,
94132
float n20, float n21, float n22, float n23,
@@ -99,27 +137,44 @@ public void apply(float n00, float n01, float n02, float n03,
99137
*/
100138
public void preApply(PMatrix left);
101139

140+
/**
141+
* Apply another matrix to the left of this one.
142+
*/
102143
public void preApply(PMatrix2D left);
103144

145+
/**
146+
* Apply another matrix to the left of this one. 3D only.
147+
*/
104148
public void preApply(PMatrix3D left);
105149

150+
/**
151+
* Apply another matrix to the left of this one.
152+
*/
106153
public void preApply(float n00, float n01, float n02,
107154
float n10, float n11, float n12);
108155

156+
/**
157+
* Apply another matrix to the left of this one. 3D only.
158+
*/
109159
public void preApply(float n00, float n01, float n02, float n03,
110160
float n10, float n11, float n12, float n13,
111161
float n20, float n21, float n22, float n23,
112162
float n30, float n31, float n32, float n33);
113163

114164

115-
/**
116-
* Multiply a PVector by this matrix.
165+
/**
166+
* Multiply source by this matrix, and return the result.
167+
* The result will be stored in target if target is non-null, and target
168+
* will then be the matrix returned. This improves performance if you reuse
169+
* target, so it's recommended if you call this many times in draw().
117170
*/
118171
public PVector mult(PVector source, PVector target);
119172

120173

121-
/**
122-
* Multiply a multi-element vector against this matrix.
174+
/**
175+
* Multiply a multi-element vector against this matrix.
176+
* Supplying and recycling a target array improves performance, so it's
177+
* recommended if you call this many times in draw().
123178
*/
124179
public float[] mult(float[] source, float[] target);
125180

@@ -133,13 +188,14 @@ public void preApply(float n00, float n01, float n02, float n03,
133188

134189

135190
/**
136-
* Transpose this matrix.
191+
* Transpose this matrix; rows become columns and columns rows.
137192
*/
138193
public void transpose();
139194

140195

141196
/**
142-
* Invert this matrix.
197+
* Invert this matrix. Will not necessarily succeed, because some matrices
198+
* map more than one point to the same image point, and so are irreversible.
143199
* @return true if successful
144200
*/
145201
public boolean invert();
@@ -149,4 +205,4 @@ public void preApply(float n00, float n01, float n02, float n03,
149205
* @return the determinant of the matrix
150206
*/
151207
public float determinant();
152-
}
208+
}

core/src/processing/core/PMatrix2D.java

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,25 @@
2626

2727
/**
2828
* 3x2 affine matrix implementation.
29+
* Matrices are used to describe a transformation; see {@link PMatrix} for a
30+
* general description. This matrix looks like the following when multiplying
31+
* a vector (x, y) in {@code mult()}.
32+
* <pre>
33+
* [m00 m01 m02][x] [m00*x + m01*y + m02*1] [x']
34+
* [m10 m11 m12][y] = [m10*x + m11*y + m12*1] = [y']
35+
* [ 0 0 1 ][1] [ 0*x + 0*y + 1*1 ] [ 1]</pre>
36+
* (x', y') is returned. The values in the matrix determine the transformation.
37+
* They are modified by the various transformation functions.
2938
*/
3039
public class PMatrix2D implements PMatrix {
3140

3241
public float m00, m01, m02;
3342
public float m10, m11, m12;
3443

3544

45+
/**
46+
* Create a new matrix, set to the identity matrix.
47+
*/
3648
public PMatrix2D() {
3749
reset();
3850
}
@@ -69,6 +81,7 @@ public PMatrix2D get() {
6981
/**
7082
* Copies the matrix contents into a 6 entry float array.
7183
* If target is null (or not the correct size), a new array will be created.
84+
* Returned in the order {@code {m00, m01, m02, m10, m11, m12}}.
7285
*/
7386
public float[] get(float[] target) {
7487
if ((target == null) || (target.length != 6)) {
@@ -86,6 +99,10 @@ public float[] get(float[] target) {
8699
}
87100

88101

102+
/**
103+
* If matrix is a PMatrix2D, sets this matrix to be a copy of it.
104+
* @throws IllegalArgumentException If <tt>matrix</tt> is not 2D.
105+
*/
89106
public void set(PMatrix matrix) {
90107
if (matrix instanceof PMatrix2D) {
91108
PMatrix2D src = (PMatrix2D) matrix;
@@ -97,6 +114,9 @@ public void set(PMatrix matrix) {
97114
}
98115

99116

117+
/**
118+
* Unavailable in 2D. Does nothing.
119+
*/
100120
public void set(PMatrix3D src) {
101121
}
102122

@@ -112,13 +132,19 @@ public void set(float[] source) {
112132
}
113133

114134

135+
/**
136+
* Sets the matrix content.
137+
*/
115138
public void set(float m00, float m01, float m02,
116139
float m10, float m11, float m12) {
117140
this.m00 = m00; this.m01 = m01; this.m02 = m02;
118141
this.m10 = m10; this.m11 = m11; this.m12 = m12;
119142
}
120143

121144

145+
/**
146+
* Unavailable in 2D. Does nothing.
147+
*/
122148
public void set(float m00, float m01, float m02, float m03,
123149
float m10, float m11, float m12, float m13,
124150
float m20, float m21, float m22, float m23,
@@ -133,6 +159,10 @@ public void translate(float tx, float ty) {
133159
}
134160

135161

162+
/**
163+
* Unavailable in 2D.
164+
* @throws IllegalArgumentException
165+
*/
136166
public void translate(float x, float y, float z) {
137167
throw new IllegalArgumentException("Cannot use translate(x, y, z) on a PMatrix2D.");
138168
}
@@ -154,11 +184,19 @@ public void rotate(float angle) {
154184
}
155185

156186

187+
/**
188+
* Unavailable in 2D.
189+
* @throws IllegalArgumentException
190+
*/
157191
public void rotateX(float angle) {
158192
throw new IllegalArgumentException("Cannot use rotateX() on a PMatrix2D.");
159193
}
160194

161195

196+
/**
197+
* Unavailable in 2D.
198+
* @throws IllegalArgumentException
199+
*/
162200
public void rotateY(float angle) {
163201
throw new IllegalArgumentException("Cannot use rotateY() on a PMatrix2D.");
164202
}
@@ -169,6 +207,10 @@ public void rotateZ(float angle) {
169207
}
170208

171209

210+
/**
211+
* Unavailable in 2D.
212+
* @throws IllegalArgumentException
213+
*/
172214
public void rotate(float angle, float v0, float v1, float v2) {
173215
throw new IllegalArgumentException("Cannot use this version of rotate() on a PMatrix2D.");
174216
}
@@ -185,6 +227,10 @@ public void scale(float sx, float sy) {
185227
}
186228

187229

230+
/**
231+
* Unavailable in 2D.
232+
* @throws IllegalArgumentException
233+
*/
188234
public void scale(float x, float y, float z) {
189235
throw new IllegalArgumentException("Cannot use this version of scale() on a PMatrix2D.");
190236
}
@@ -215,6 +261,10 @@ public void apply(PMatrix2D source) {
215261
}
216262

217263

264+
/**
265+
* Unavailable in 2D.
266+
* @throws IllegalArgumentException
267+
*/
218268
public void apply(PMatrix3D source) {
219269
throw new IllegalArgumentException("Cannot use apply(PMatrix3D) on a PMatrix2D.");
220270
}
@@ -236,6 +286,10 @@ public void apply(float n00, float n01, float n02,
236286
}
237287

238288

289+
/**
290+
* Unavailable in 2D.
291+
* @throws IllegalArgumentException
292+
*/
239293
public void apply(float n00, float n01, float n02, float n03,
240294
float n10, float n11, float n12, float n13,
241295
float n20, float n21, float n22, float n23,
@@ -262,6 +316,10 @@ public void preApply(PMatrix2D left) {
262316
}
263317

264318

319+
/**
320+
* Unavailable in 2D.
321+
* @throws IllegalArgumentException
322+
*/
265323
public void preApply(PMatrix3D left) {
266324
throw new IllegalArgumentException("Cannot use preApply(PMatrix3D) on a PMatrix2D.");
267325
}
@@ -289,6 +347,10 @@ public void preApply(float n00, float n01, float n02,
289347
}
290348

291349

350+
/**
351+
* Unavailable in 2D.
352+
* @throws IllegalArgumentException
353+
*/
292354
public void preApply(float n00, float n01, float n02, float n03,
293355
float n10, float n11, float n12, float n13,
294356
float n20, float n21, float n22, float n23,
@@ -301,7 +363,8 @@ public void preApply(float n00, float n01, float n02, float n03,
301363

302364

303365
/**
304-
* Multiply the x and y coordinates of a PVector against this matrix.
366+
* {@inheritDoc}
367+
* Ignores any z component.
305368
*/
306369
public PVector mult(PVector source, PVector target) {
307370
if (target == null) {
@@ -339,26 +402,34 @@ public float[] mult(float vec[], float out[]) {
339402
}
340403

341404

405+
/**
406+
* Returns the x-coordinate of the result of multiplying the point (x, y)
407+
* by this matrix.
408+
*/
342409
public float multX(float x, float y) {
343410
return m00*x + m01*y + m02;
344411
}
345412

346413

414+
/**
415+
* Returns the y-coordinate of the result of multiplying the point (x, y)
416+
* by this matrix.
417+
*/
347418
public float multY(float x, float y) {
348419
return m10*x + m11*y + m12;
349420
}
350421

351422

423+
352424
/**
353-
* Transpose this matrix.
425+
* Unavailable in 2D. Does nothing.
354426
*/
355427
public void transpose() {
356428
}
357429

358430

359-
/**
360-
* Invert this matrix. Implementation stolen from OpenJDK.
361-
* @return true if successful
431+
/*
432+
* Implementation stolen from OpenJDK.
362433
*/
363434
public boolean invert() {
364435
float determinant = determinant();

0 commit comments

Comments
 (0)