-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD2d_matrixS.c
More file actions
213 lines (156 loc) · 3.81 KB
/
Copy pathD2d_matrixS.c
File metadata and controls
213 lines (156 loc) · 3.81 KB
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
#include <D2d_matrix.h>
#include <math.h>
#include <FPT.h>
/*
( x') (x)
( y') = M * (y)
( 1 ) (1)
instead of (x',y',1) = (x,y,1) * M
*/
int D2d_print_mat (double a[3][3])
{
int r,c ;
for (r = 0 ; r < 3 ; r++ ) {
for (c = 0 ; c < 3 ; c++ ) {
printf(" %12.4lf ",a[r][c]) ;
}
printf("\n") ;
}
return 1 ;
}
int D2d_copy_mat (double a[3][3], double b[3][3])
// a = b
{
int r,c ;
for (r = 0 ; r < 3 ; r++ ) {
for (c = 0 ; c < 3 ; c++ ) {
a[r][c] = b[r][c] ;
}
}
return 1 ;
}
int D2d_mat_mult (double res[3][3], double a[3][3], double b[3][3])
{
// res = a * b
// this is SAFE, i.e. the user can make a call such as
// D2d_mat_mult(p, p,q) or D2d_mat_mult(p, q,p) or D2d_mat_mult(p, p,p)
int i, j;
double c[3][3] ;
for (i = 0; i<3; i++) {
for (j = 0; j < 3; j++) {
c[i][j] = a[i][0]*b[0][j]+a[i][1]*b[1][j]+a[i][2]*b[2][j] ; }
}
D2d_copy_mat(res, c) ;
return 1 ;
}
int D2d_make_identity (double a[3][3])
// a = I
{
int r,c ;
for (r = 0 ; r < 3 ; r++ ) {
for (c = 0 ; c < 3 ; c++ ) {
if (r == c) a[r][c] = 1.0 ;
else a[r][c] = 0.0 ;
}
}
return 1 ;
}
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
int D2d_translate (double a[3][3], double b[3][3], double dx, double dy)
// a = translation*a
// b = b*translation_inverse
{
double t[3][3] ;
D2d_make_identity(t) ;
t[0][2] = dx ; t[1][2] = dy ;
D2d_mat_mult(a, t,a) ;
t[0][2] = -dx ; t[1][2] = -dy ;
D2d_mat_mult(b, b,t) ;
return 1 ;
}
int D2d_scale (double a[3][3], double b[3][3], double sx, double sy)
// a = scale*a
// b = b*scale_inverse
{
double t[3][3] ;
D2d_make_identity(t) ;
t[0][0] = sx ; t[1][1] = sy ;
D2d_mat_mult(a, t, a) ;
if ((sx == 0) || (sy == 0)) {return 0 ; }
t[0][0] = 1/sx ; t[1][1] = 1/sy ;
D2d_mat_mult(b, b, t) ;
return 1;
}
int D2d_rotate (double a[3][3], double b[3][3], double radians)
// a = rotate*a
// b = b*rotate_inverse
{
double t[3][3] ;
double cs = cos( radians ) ;
double sn = sin( radians ) ;
D2d_make_identity(t) ;
t[0][0] = cs ; t[0][1] = -sn ;
t[1][0] = sn ; t[1][1] = cs ;
D2d_mat_mult(a, t, a) ;
t[0][0] = cs ; t[0][1] = sn ;
t[1][0] = -sn ; t[1][1] = cs ;
D2d_mat_mult(b, b, t) ;
return 1;
}
/*
int D2d_rotate_sincos(double a[3][3], double b[3][3], double sn, double cs){
double t[3][3] ;
D2d_make_identity(t) ;
t[0][0] = cs ; t[0][1] = -sn ;
t[1][0] = sn ; t[1][1] = cs ;
D2d_mat_mult(a, t, a) ;
t[0][0] = cs ; t[0][1] = sn ;
t[1][0] = -sn ; t[1][1] = cs ;
D2d_mat_mult(b, b, t) ;
return 1;
}
*/
int D2d_negate_x (double a[3][3], double b[3][3])
// negate the x, meaning there's a reflection in the y-axis
// a = reflect*a
// b = b*reflect_inverse
{
double t[3][3] ;
D2d_make_identity(t) ;
t[0][0] = -1 ;
D2d_mat_mult(a, t,a) ;
// the transformation, t, is its own inverse
D2d_mat_mult(b, b,t) ;
return 1 ;
}
int D2d_negate_y (double a[3][3], double b[3][3])
// negate the y, meaning there's a reflection in the x-axis
// a = reflect*a
// b = b*reflect_inverse
{
double t[3][3] ;
D2d_make_identity(t) ;
t[1][1] = -1 ;
D2d_mat_mult(a, t,a) ;
// the transformation, t, is its own inverse
D2d_mat_mult(b, b,t) ;
return 1 ;
}
int D2d_mat_mult_points (double *X, double *Y, double m[3][3], double *x, double *y, int numpoints)
// |X0 X1 X2 ...| |x0 x1 x2 ...|
// |Y0 Y1 Y2 ...| = m * |y0 y1 y2 ...|
// | 1 1 1 ...| | 1 1 1 ...|
// SAFE, user may make a call like D2d_mat_mult_points (x,y, m, x,y, n) ;
{
double u,v ;
int i ;
for (i = 0 ; i < numpoints ; i++) {
u = m[0][0]*x[i] + m[0][1]*y[i] + m[0][2] ;
v = m[1][0]*x[i] + m[1][1]*y[i] + m[1][2] ;
X[i] = u ;
Y[i] = v ;
}
return 1 ;
}