PicoVGA  1.2-cmake
VGA/TV display on Raspberry Pico
mat2d.h
Go to the documentation of this file.
1 
8 #ifndef _MAT2D_H
9 #define _MAT2D_H
10 
11 #include "pico/double.h"
12 #include "define.h"
13 
15 template <typename m2type> class cMat2D
16 {
17 public:
18 
19  // transformation matrix
20  m2type m11, m12, m13;
21  m2type m21, m22, m23;
22 
24  inline m2type GetX(m2type x, m2type y) const
25  {
26  return x*m11 + y*m12 + m13;
27  }
28 
30  inline m2type GetY(m2type x, m2type y) const
31  {
32  return x*m21 + y*m22 + m23;
33  }
34 
36  inline void Unit()
37  {
38  m11 = 1; m12 = 0; m13 = 0;
39  m21 = 0; m22 = 1; m23 = 0;
40  }
41 
43  inline void Copy(const cMat2D* m)
44  {
45  m11 = m->m11; m12 = m->m12; m13 = m->m13;
46  m21 = m->m21; m22 = m->m22; m23 = m->m23;
47  }
48 
58  inline void TransX(m2type tx)
59  {
60  m13 += tx;
61  }
62 
72  inline void TransY(m2type ty)
73  {
74  m23 += ty;
75  }
76 
86  inline void ScaleX(m2type sx)
87  {
88  m11 *= sx;
89  m12 *= sx;
90  m13 *= sx;
91  }
92 
102  inline void ScaleY(m2type sy)
103  {
104  m21 *= sy;
105  m22 *= sy;
106  m23 *= sy;
107  }
108 
118  inline void RotSC(m2type sina, m2type cosa)
119  {
120  m2type t1 = m11;
121  m2type t2 = m21;
122  m11 = t1*cosa - t2*sina;
123  m21 = t1*sina + t2*cosa;
124 
125  t1 = m12;
126  t2 = m22;
127  m12 = t1*cosa - t2*sina;
128  m22 = t1*sina + t2*cosa;
129 
130  t1 = m13;
131  t2 = m23;
132  m13 = t1*cosa - t2*sina;
133  m23 = t1*sina + t2*cosa;
134  }
135 
137  inline void Rot(m2type a)
138  {
139  this->RotSC(sin(a), cos(a));
140  }
141 
143  inline void Rot90()
144  {
145  m2type t = m11;
146  m11 = -m21;
147  m21 = t;
148 
149  t = m12;
150  m12 = -m22;
151  m22 = t;
152 
153  t = m13;
154  m13 = -m23;
155  m23 = t;
156  }
157 
159  inline void Rot180()
160  {
161  m11 = -m11;
162  m21 = -m21;
163  m12 = -m12;
164  m22 = -m22;
165  m13 = -m13;
166  m23 = -m23;
167  }
168 
170  inline void Rot270()
171  {
172  m2type t = m11;
173  m11 = m21;
174  m21 = -t;
175 
176  t = m12;
177  m12 = m22;
178  m22 = -t;
179 
180  t = m13;
181  m13 = m23;
182  m23 = -t;
183  }
184 
194  inline void ShearX(m2type dx)
195  {
196  m11 += m21*dx;
197  m12 += m22*dx;
198  m13 += m23*dx;
199  }
200 
210  inline void ShearY(m2type dy)
211  {
212  m21 += m11*dy;
213  m22 += m12*dy;
214  m23 += m13*dy;
215  }
216 
226  inline void FlipY()
227  {
228  m21 = -m21;
229  m22 = -m22;
230  m23 = -m23;
231  }
232 
242  inline void FlipX()
243  {
244  m11 = -m11;
245  m12 = -m12;
246  m13 = -m13;
247  }
248 };
249 
250 #define TOFRACT(f) ((int)((f)*FRACTMUL + (((f) < 0) ? -0.5f : 0.5f)))
251 
254 
262 class cMat2Df : public cMat2D<float>
263 {
264 public:
281  void PrepDrawImg(int ws, int hs, int x0, int y0, int wd, int hd,
282  float shearx, float sheary, float r, float tx, float ty);
283 
289  void ExportInt(int* mat) const;
290 };
291 
293 
294 #endif // _MAT2D_H
Transformation matrix.
Definition: mat2d.h:16
void TransY(m2type ty)
Translate in Y direction.
Definition: mat2d.h:72
void RotSC(m2type sina, m2type cosa)
Rotate, using sin and cos.
Definition: mat2d.h:118
void Unit()
Set unit matrix.
Definition: mat2d.h:36
void ScaleX(m2type sx)
Scale in X direction.
Definition: mat2d.h:86
void Copy(const cMat2D *m)
Copy matrix.
Definition: mat2d.h:43
void Rot180()
Rotate by 180 deg (=flipX and flipY) (sina=0, cosa=-1)
Definition: mat2d.h:159
void Rot(m2type a)
Rotate, using angle.
Definition: mat2d.h:137
void TransX(m2type tx)
Translate in X direction.
Definition: mat2d.h:58
void Rot270()
Rotate by 270 deg (sina=-1, cosa=0)
Definition: mat2d.h:170
void FlipX()
Flip in X direction.
Definition: mat2d.h:242
m2type GetX(m2type x, m2type y) const
Transform X.
Definition: mat2d.h:24
void ScaleY(m2type sy)
Scale in Y direction.
Definition: mat2d.h:102
void Rot90()
Rotate by 90 deg (sina=1, cosa=0)
Definition: mat2d.h:143
void ShearY(m2type dy)
Shear in Y direction.
Definition: mat2d.h:210
void FlipY()
Flip in Y direction.
Definition: mat2d.h:226
void ShearX(m2type dx)
Shear in X direction.
Definition: mat2d.h:194
m2type GetY(m2type x, m2type y) const
Transform Y.
Definition: mat2d.h:30
2D Transformation Matrix
Definition: mat2d.h:263
void ExportInt(int *mat) const
Export matrix to int array[6].
Definition: mat2d.cpp:53
void PrepDrawImg(int ws, int hs, int x0, int y0, int wd, int hd, float shearx, float sheary, float r, float tx, float ty)
Prepare transformation matrix (for DrawImgMat() function)
Definition: mat2d.cpp:22
VGA common definitions of C and ASM.