forked from mypaint/libmypaint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mypaint-matrix.c
92 lines (83 loc) · 2.79 KB
/
mypaint-matrix.c
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
/* libmypaint - The MyPaint Brush Library
* Copyright (C) 2019 The MyPaint Team
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "mypaint-matrix.h"
#include <math.h>
MyPaintTransform
mypaint_matrix_multiply(const MyPaintTransform m1, const MyPaintTransform m2)
{
MyPaintTransform result;
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
result.rows[row][col] =
m1.rows[0][col] * m2.rows[row][0] +
m1.rows[1][col] * m2.rows[row][1] +
m1.rows[2][col] * m2.rows[row][2];
}
}
return result;
}
MyPaintTransform
mypaint_transform_unit()
{
MyPaintTransform m = {{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}};
return m;
}
MyPaintTransform
mypaint_transform_rotate_cw(const MyPaintTransform transform, const float angle_radians)
{
const float a = angle_radians;
MyPaintTransform factor = {{{cos(a), sin(a), 0}, {-sin(a), cos(a), 0}, {0, 0, 1}}};
return mypaint_matrix_multiply(transform, factor);
}
MyPaintTransform
mypaint_transform_rotate_ccw(const MyPaintTransform transform, const float angle_radians)
{
const float a = angle_radians;
MyPaintTransform factor = {{
{cos(a), -sin(a), 0},
{sin(a), cos(a), 0},
{0, 0, 1},
}};
return mypaint_matrix_multiply(transform, factor);
}
MyPaintTransform
mypaint_transform_reflect(const MyPaintTransform transform, const float angle_radians)
{
float x = cos(angle_radians);
float y = sin(angle_radians);
MyPaintTransform factor = {{
{x * x - y * y, 2.0 * x * y, 0},
{2.0 * x * y, y * y - x * x, 0},
{0, 0, 1},
}};
return mypaint_matrix_multiply(transform, factor);
}
MyPaintTransform
mypaint_transform_translate(const MyPaintTransform transform, const float x, const float y)
{
MyPaintTransform factor = {{
{1, 0, x},
{0, 1, y},
{0, 0, 1},
}};
return mypaint_matrix_multiply(transform, factor);
}
void
mypaint_transform_point(const MyPaintTransform* const t, float x, float y, float* xout, float* yout)
{
*xout = t->rows[0][0] * x + t->rows[0][1] * y + t->rows[0][2];
*yout = t->rows[1][0] * x + t->rows[1][1] * y + t->rows[1][2];
}