FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/core/src/processing/core/PMatrix2D.java at master · 2php/processing · GitHub
2php
processing
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
core
/
src
/
processing
/
core
/
PMatrix2D.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
534 lines (403 loc) · 12.5 KB
Breadcrumbs
processing
/
core
/
src
/
processing
/
core
/
PMatrix2D.java
Copy path
File metadata and controls
534 lines (403 loc) · 12.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2005-08 Ben Fry and Casey Reas
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package
processing
.
core
;
/**
* 3x2 affine matrix implementation.
* Matrices are used to describe a transformation; see {@link PMatrix} for a
* general description. This matrix looks like the following when multiplying
* a vector (x, y) in {@code mult()}.
* <pre>
* [m00 m01 m02][x] [m00*x + m01*y + m02*1] [x']
* [m10 m11 m12][y] = [m10*x + m11*y + m12*1] = [y']
* [ 0 0 1 ][1] [ 0*x + 0*y + 1*1 ] [ 1]</pre>
* (x', y') is returned. The values in the matrix determine the transformation.
* They are modified by the various transformation functions.
*/
public
class
PMatrix2D
implements
PMatrix
{
public
float
m00
,
m01
,
m02
;
public
float
m10
,
m11
,
m12
;
/**
* Create a new matrix, set to the identity matrix.
*/
public
PMatrix2D
() {
reset
();
}
public
PMatrix2D
(
float
m00
,
float
m01
,
float
m02
,
float
m10
,
float
m11
,
float
m12
) {
set
(
m00
,
m01
,
m02
,
m10
,
m11
,
m12
);
}
public
PMatrix2D
(
PMatrix
matrix
) {
set
(
matrix
);
}
public
void
reset
() {
set
(
1
,
0
,
0
,
0
,
1
,
0
);
}
/**
* Returns a copy of this PMatrix.
*/
public
PMatrix2D
get
() {
PMatrix2D
outgoing
=
new
PMatrix2D
();
outgoing
.
set
(
this
);
return
outgoing
;
}
/**
* Copies the matrix contents into a 6 entry float array.
* If target is null (or not the correct size), a new array will be created.
* Returned in the order {@code {m00, m01, m02, m10, m11, m12}}.
*/
public
float
[]
get
(
float
[]
target
) {
if
((
target
==
null
) || (
target
.
length
!=
6
)) {
target
=
new
float
[
6
];
}
target
[
0
] =
m00
;
target
[
1
] =
m01
;
target
[
2
] =
m02
;
target
[
3
] =
m10
;
target
[
4
] =
m11
;
target
[
5
] =
m12
;
return
target
;
}
/**
* If matrix is a PMatrix2D, sets this matrix to be a copy of it.
* @throws IllegalArgumentException If <tt>matrix</tt> is not 2D.
*/
public
void
set
(
PMatrix
matrix
) {
if
(
matrix
instanceof
PMatrix2D
) {
PMatrix2D
src
= (
PMatrix2D
)
matrix
;
set
(
src
.
m00
,
src
.
m01
,
src
.
m02
,
src
.
m10
,
src
.
m11
,
src
.
m12
);
}
else
{
throw
new
IllegalArgumentException
(
"PMatrix2D.set() only accepts PMatrix2D objects."
);
}
}
/**
* Unavailable in 2D. Does nothing.
*/
public
void
set
(
PMatrix3D
src
) {
}
public
void
set
(
float
[]
source
) {
m00
=
source
[
0
];
m01
=
source
[
1
];
m02
=
source
[
2
];
m10
=
source
[
3
];
m11
=
source
[
4
];
m12
=
source
[
5
];
}
/**
* Sets the matrix content.
*/
public
void
set
(
float
m00
,
float
m01
,
float
m02
,
float
m10
,
float
m11
,
float
m12
) {
this
.
m00
=
m00
;
this
.
m01
=
m01
;
this
.
m02
=
m02
;
this
.
m10
=
m10
;
this
.
m11
=
m11
;
this
.
m12
=
m12
;
}
/**
* Unavailable in 2D. Does nothing.
*/
public
void
set
(
float
m00
,
float
m01
,
float
m02
,
float
m03
,
float
m10
,
float
m11
,
float
m12
,
float
m13
,
float
m20
,
float
m21
,
float
m22
,
float
m23
,
float
m30
,
float
m31
,
float
m32
,
float
m33
) {
}
public
void
translate
(
float
tx
,
float
ty
) {
m02
=
tx
*
m00
+
ty
*
m01
+
m02
;
m12
=
tx
*
m10
+
ty
*
m11
+
m12
;
}
/**
* Unavailable in 2D.
* @throws IllegalArgumentException
*/
public
void
translate
(
float
x
,
float
y
,
float
z
) {
throw
new
IllegalArgumentException
(
"Cannot use translate(x, y, z) on a PMatrix2D."
);
}
// Implementation roughly based on AffineTransform.
public
void
rotate
(
float
angle
) {
float
s
=
sin
(
angle
);
float
c
=
cos
(
angle
);
float
temp1
=
m00
;
float
temp2
=
m01
;
m00
=
c
*
temp1
+
s
*
temp2
;
m01
= -
s
*
temp1
+
c
*
temp2
;
temp1
=
m10
;
temp2
=
m11
;
m10
=
c
*
temp1
+
s
*
temp2
;
m11
= -
s
*
temp1
+
c
*
temp2
;
}
/**
* Unavailable in 2D.
* @throws IllegalArgumentException
*/
public
void
rotateX
(
float
angle
) {
throw
new
IllegalArgumentException
(
"Cannot use rotateX() on a PMatrix2D."
);
}
/**
* Unavailable in 2D.
* @throws IllegalArgumentException
*/
public
void
rotateY
(
float
angle
) {
throw
new
IllegalArgumentException
(
"Cannot use rotateY() on a PMatrix2D."
);
}
public
void
rotateZ
(
float
angle
) {
rotate
(
angle
);
}
/**
* Unavailable in 2D.
* @throws IllegalArgumentException
*/
public
void
rotate
(
float
angle
,
float
v0
,
float
v1
,
float
v2
) {
throw
new
IllegalArgumentException
(
"Cannot use this version of rotate() on a PMatrix2D."
);
}
public
void
scale
(
float
s
) {
scale
(
s
,
s
);
}
public
void
scale
(
float
sx
,
float
sy
) {
m00
*=
sx
;
m01
*=
sy
;
m10
*=
sx
;
m11
*=
sy
;
}
/**
* Unavailable in 2D.
* @throws IllegalArgumentException
*/
public
void
scale
(
float
x
,
float
y
,
float
z
) {
throw
new
IllegalArgumentException
(
"Cannot use this version of scale() on a PMatrix2D."
);
}
public
void
shearX
(
float
angle
) {
apply
(
1
,
0
,
1
,
tan
(
angle
),
0
,
0
);
}
public
void
shearY
(
float
angle
) {
apply
(
1
,
0
,
1
,
0
,
tan
(
angle
),
0
);
}
public
void
apply
(
PMatrix
source
) {
if
(
source
instanceof
PMatrix2D
) {
apply
((
PMatrix2D
)
source
);
}
else
if
(
source
instanceof
PMatrix3D
) {
apply
((
PMatrix3D
)
source
);
}
}
public
void
apply
(
PMatrix2D
source
) {
apply
(
source
.
m00
,
source
.
m01
,
source
.
m02
,
source
.
m10
,
source
.
m11
,
source
.
m12
);
}
/**
* Unavailable in 2D.
* @throws IllegalArgumentException
*/
public
void
apply
(
PMatrix3D
source
) {
throw
new
IllegalArgumentException
(
"Cannot use apply(PMatrix3D) on a PMatrix2D."
);
}
public
void
apply
(
float
n00
,
float
n01
,
float
n02
,
float
n10
,
float
n11
,
float
n12
) {
float
t0
=
m00
;
float
t1
=
m01
;
m00
=
n00
*
t0
+
n10
*
t1
;
m01
=
n01
*
t0
+
n11
*
t1
;
m02
+=
n02
*
t0
+
n12
*
t1
;
t0
=
m10
;
t1
=
m11
;
m10
=
n00
*
t0
+
n10
*
t1
;
m11
=
n01
*
t0
+
n11
*
t1
;
m12
+=
n02
*
t0
+
n12
*
t1
;
}
/**
* Unavailable in 2D.
* @throws IllegalArgumentException
*/
public
void
apply
(
float
n00
,
float
n01
,
float
n02
,
float
n03
,
float
n10
,
float
n11
,
float
n12
,
float
n13
,
float
n20
,
float
n21
,
float
n22
,
float
n23
,
float
n30
,
float
n31
,
float
n32
,
float
n33
) {
throw
new
IllegalArgumentException
(
"Cannot use this version of apply() on a PMatrix2D."
);
}
/**
* Apply another matrix to the left of this one.
*/
public
void
preApply
(
PMatrix
source
) {
if
(
source
instanceof
PMatrix2D
) {
preApply
((
PMatrix2D
)
source
);
}
else
if
(
source
instanceof
PMatrix3D
) {
preApply
((
PMatrix3D
)
source
);
}
}
public
void
preApply
(
PMatrix2D
left
) {
preApply
(
left
.
m00
,
left
.
m01
,
left
.
m02
,
left
.
m10
,
left
.
m11
,
left
.
m12
);
}
/**
* Unavailable in 2D.
* @throws IllegalArgumentException
*/
public
void
preApply
(
PMatrix3D
left
) {
throw
new
IllegalArgumentException
(
"Cannot use preApply(PMatrix3D) on a PMatrix2D."
);
}
public
void
preApply
(
float
n00
,
float
n01
,
float
n02
,
float
n10
,
float
n11
,
float
n12
) {
float
t0
=
m02
;
float
t1
=
m12
;
n02
+=
t0
*
n00
+
t1
*
n01
;
n12
+=
t0
*
n10
+
t1
*
n11
;
m02
=
n02
;
m12
=
n12
;
t0
=
m00
;
t1
=
m10
;
m00
=
t0
*
n00
+
t1
*
n01
;
m10
=
t0
*
n10
+
t1
*
n11
;
t0
=
m01
;
t1
=
m11
;
m01
=
t0
*
n00
+
t1
*
n01
;
m11
=
t0
*
n10
+
t1
*
n11
;
}
/**
* Unavailable in 2D.
* @throws IllegalArgumentException
*/
public
void
preApply
(
float
n00
,
float
n01
,
float
n02
,
float
n03
,
float
n10
,
float
n11
,
float
n12
,
float
n13
,
float
n20
,
float
n21
,
float
n22
,
float
n23
,
float
n30
,
float
n31
,
float
n32
,
float
n33
) {
throw
new
IllegalArgumentException
(
"Cannot use this version of preApply() on a PMatrix2D."
);
}
//////////////////////////////////////////////////////////////
/**
* {@inheritDoc}
* Ignores any z component.
*/
public
PVector
mult
(
PVector
source
,
PVector
target
) {
if
(
target
==
null
) {
target
=
new
PVector
();
}
target
.
x
=
m00
*
source
.
x
+
m01
*
source
.
y
+
m02
;
target
.
y
=
m10
*
source
.
x
+
m11
*
source
.
y
+
m12
;
return
target
;
}
/**
* Multiply a two element vector against this matrix.
* If out is null or not length four, a new float array will be returned.
* The values for vec and out can be the same (though that's less efficient).
*/
public
float
[]
mult
(
float
vec
[],
float
out
[]) {
if
(
out
==
null
||
out
.
length
!=
2
) {
out
=
new
float
[
2
];
}
if
(
vec
==
out
) {
float
tx
=
m00
*
vec
[
0
] +
m01
*
vec
[
1
] +
m02
;
float
ty
=
m10
*
vec
[
0
] +
m11
*
vec
[
1
] +
m12
;
out
[
0
] =
tx
;
out
[
1
] =
ty
;
}
else
{
out
[
0
] =
m00
*
vec
[
0
] +
m01
*
vec
[
1
] +
m02
;
out
[
1
] =
m10
*
vec
[
0
] +
m11
*
vec
[
1
] +
m12
;
}
return
out
;
}
/**
* Returns the x-coordinate of the result of multiplying the point (x, y)
* by this matrix.
*/
public
float
multX
(
float
x
,
float
y
) {
return
m00
*
x
+
m01
*
y
+
m02
;
}
/**
* Returns the y-coordinate of the result of multiplying the point (x, y)
* by this matrix.
*/
public
float
multY
(
float
x
,
float
y
) {
return
m10
*
x
+
m11
*
y
+
m12
;
}
/**
* Unavailable in 2D. Does nothing.
*/
public
void
transpose
() {
}
/*
* Implementation stolen from OpenJDK.
*/
public
boolean
invert
() {
float
determinant
=
determinant
();
if
(
Math
.
abs
(
determinant
) <=
Float
.
MIN_VALUE
) {
return
false
;
}
float
t00
=
m00
;
float
t01
=
m01
;
float
t02
=
m02
;
float
t10
=
m10
;
float
t11
=
m11
;
float
t12
=
m12
;
m00
=
t11
/
determinant
;
m10
= -
t10
/
determinant
;
m01
= -
t01
/
determinant
;
m11
=
t00
/
determinant
;
m02
= (
t01
*
t12
-
t11
*
t02
) /
determinant
;
m12
= (
t10
*
t02
-
t00
*
t12
) /
determinant
;
return
true
;
}
/**
* @return the determinant of the matrix
*/
public
float
determinant
() {
return
m00
*
m11
-
m01
*
m10
;
}
//////////////////////////////////////////////////////////////
public
void
print
() {
int
big
= (
int
)
abs
(
max
(
PApplet
.
max
(
abs
(
m00
),
abs
(
m01
),
abs
(
m02
)),
PApplet
.
max
(
abs
(
m10
),
abs
(
m11
),
abs
(
m12
))));
int
digits
=
1
;
if
(
Float
.
isNaN
(
big
) ||
Float
.
isInfinite
(
big
)) {
// avoid infinite loop
digits
=
5
;
}
else
{
while
((
big
/=
10
) !=
0
)
digits
++;
// cheap log()
}
System
.
out
.
println
(
PApplet
.
nfs
(
m00
,
digits
,
4
) +
" "
+
PApplet
.
nfs
(
m01
,
digits
,
4
) +
" "
+
PApplet
.
nfs
(
m02
,
digits
,
4
));
System
.
out
.
println
(
PApplet
.
nfs
(
m10
,
digits
,
4
) +
" "
+
PApplet
.
nfs
(
m11
,
digits
,
4
) +
" "
+
PApplet
.
nfs
(
m12
,
digits
,
4
));
System
.
out
.
println
();
}
//////////////////////////////////////////////////////////////
// TODO these need to be added as regular API, but the naming and
// implementation needs to be improved first. (e.g. actually keeping track
// of whether the matrix is in fact identity internally.)
protected
boolean
isIdentity
() {
return
((
m00
==
1
) && (
m01
==
0
) && (
m02
==
0
) &&
(
m10
==
0
) && (
m11
==
1
) && (
m12
==
0
));
}
// TODO make this more efficient, or move into PMatrix2D
protected
boolean
isWarped
() {
return
((
m00
!=
1
) || (
m01
!=
0
) &&
(
m10
!=
0
) || (
m11
!=
1
));
}
//////////////////////////////////////////////////////////////
static
private
final
float
max
(
float
a
,
float
b
) {
return
(
a
>
b
) ?
a
:
b
;
}
static
private
final
float
abs
(
float
a
) {
return
(
a
<
0
) ? -
a
:
a
;
}
static
private
final
float
sin
(
float
angle
) {
return
(
float
)
Math
.
sin
(
angle
);
}
static
private
final
float
cos
(
float
angle
) {
return
(
float
)
Math
.
cos
(
angle
);
}
static
private
final
float
tan
(
float
angle
) {
return
(
float
)
Math
.
tan
(
angle
);
}
}
Back
|
FazBrowse Home
|
New Git URL