ASCII-GFX  2.0
math2d.h
Go to the documentation of this file.
1 // NCurses 2D vector math
2 // (c) 2022 by Stefan Roettger
3 
4 #pragma once
5 
7 struct Vec2
8 {
9  float x;
10  float y;
11 };
12 
14 struct Vec3
15 {
16  float x;
17  float y;
18  float z;
19 };
20 
22 struct Mat3
23 {
24  float r1x, r1y, r1z;
25  float r2x, r2y, r2z;
26  float r3x, r3y, r3z;
27 };
28 
30 inline Vec2 vec2(float x = 0, float y = 0)
31 {
32  Vec2 v = {x, y};
33  return(v);
34 }
35 
38 inline Vec3 vec3(float x = 0, float y = 0, float z = 1)
39 {
40  Vec3 v = {x, y, z};
41  return(v);
42 }
43 
46 inline Vec3 vec3(Vec2 v)
47 {
48  return(vec3(v.x, v.y));
49 }
50 
52 inline Mat3 mat3(float r1x = 1, float r1y = 0, float r1z = 0,
53  float r2x = 0, float r2y = 1, float r2z = 0,
54  float r3x = 0, float r3y = 0, float r3z = 1)
55 {
56  Mat3 M = {r1x, r1y, r1z, r2x, r2y, r2z, r3x, r3y, r3z};
57  return(M);
58 }
59 
61 inline Vec2 add2(Vec2 a, Vec2 b)
62 {
63  return(vec2(a.x+b.x, a.y+b.y));
64 }
65 
67 inline Vec2 sub2(Vec2 a, Vec2 b)
68 {
69  return(vec2(a.x-b.x, a.y-b.y));
70 }
71 
73 inline Vec2 mul2(Vec2 a, Vec2 b)
74 {
75  return(vec2(a.x*b.x, a.y*b.y));
76 }
77 
79 inline Vec2 mul2s(Vec2 a, float s)
80 {
81  return(vec2(a.x*s, a.y*s));
82 }
83 
85 inline float dot2(Vec2 a, Vec2 b)
86 {
87  return(a.x*b.x + a.y*b.y);
88 }
89 
91 inline float dot3(Vec3 a, Vec3 b)
92 {
93  return(a.x*b.x + a.y*b.y + a.z*b.z);
94 }
95 
97 inline Vec3 row1(const Mat3 &m) {return(vec3(m.r1x, m.r1y, m.r1z));}
98 inline Vec3 row2(const Mat3 &m) {return(vec3(m.r2x, m.r2y, m.r2z));}
99 inline Vec3 row3(const Mat3 &m) {return(vec3(m.r3x, m.r3y, m.r3z));}
100 
102 inline Vec3 col1(const Mat3 &m) {return(vec3(m.r1x, m.r2x, m.r3x));}
103 inline Vec3 col2(const Mat3 &m) {return(vec3(m.r1y, m.r2y, m.r3y));}
104 inline Vec3 col3(const Mat3 &m) {return(vec3(m.r1z, m.r2z, m.r3z));}
105 
107 inline Mat3 mul3(const Mat3 &m1, const Mat3 &m2)
108 {
109  return(mat3(dot3(row1(m1), col1(m2)), dot3(row1(m1), col2(m2)), dot3(row1(m1), col3(m2)),
110  dot3(row2(m1), col1(m2)), dot3(row2(m1), col2(m2)), dot3(row2(m1), col3(m2)),
111  dot3(row3(m1), col1(m2)), dot3(row3(m1), col2(m2)), dot3(row3(m1), col3(m2))));
112 }
113 
115 inline Vec2 mul3v(const Mat3 &m, Vec3 v)
116 {
117  Vec3 v3 = vec3(dot3(row1(m), v),
118  dot3(row2(m), v),
119  dot3(row3(m), v));
120 
121  float h = v3.z;
122  if (h != 0)
123  if (h != 1)
124  h = 1/h;
125 
126  return(vec2(v3.x*h, v3.y*h));
127 }
128 
130 inline int vec2_x(Vec2 v)
131 {
132  return((int)(v.x+0.5f));
133 }
134 
136 inline int vec2_y(Vec2 v)
137 {
138  return((int)(v.y+0.5f));
139 }
140 
142 inline int vec3_x(Vec3 v)
143 {
144  return((int)(v.x+0.5f));
145 }
146 
148 inline int vec3_y(Vec3 v)
149 {
150  return((int)(v.y+0.5f));
151 }
152 
154 inline int vec3_z(Vec3 v)
155 {
156  return((int)(v.z+0.5f));
157 }
158 
161 Mat3 top();
162 
165 void push();
166 
168 void replace(const Mat3 &m);
169 
172 Mat3 pop();
173 
177 void translate(Vec2 v);
178 
182 void translate(float x, float y);
183 
188 void rotate(float a, float aspect = 2);
189 
193 void scale(float f);
194 
198 void scale(float s, float t);
199 
201 Vec2 translation();
202 
206 float rotation(float aspect = 2);
207 
211 float scaling(float aspect = 2);
vec2_y
int vec2_y(Vec2 v)
convert y-component of 2D vector to rounded int
Definition: math2d.h:136
rotate
void rotate(float a, float aspect=2)
apply clockwise rotation to current transformation
Definition: math2d.cpp:74
add2
Vec2 add2(Vec2 a, Vec2 b)
2D vector addition
Definition: math2d.h:61
rotation
float rotation(float aspect=2)
get current rotation angle
Definition: math2d.cpp:119
scale
void scale(float f)
apply uniform scaling factor to current transformation
Definition: math2d.cpp:88
vec3
Vec3 vec3(float x=0, float y=0, float z=1)
3D vector construction from components x, y and z
Definition: math2d.h:38
Mat3::r3z
float r3z
Definition: math2d.h:26
sub2
Vec2 sub2(Vec2 a, Vec2 b)
2D vector subtraction
Definition: math2d.h:67
push
void push()
push current transformation this duplicates the current matrix on top of the matrix stack
Definition: math2d.cpp:22
Vec3::z
float z
Definition: math2d.h:18
pop
Mat3 pop()
pop current transformation this removes the current matrix on top of the matrix stack
Definition: math2d.cpp:49
Vec3
3D vector type
Definition: math2d.h:14
dot2
float dot2(Vec2 a, Vec2 b)
2D dot product
Definition: math2d.h:85
Vec2
2D vector type
Definition: math2d.h:7
Mat3::r3y
float r3y
Definition: math2d.h:26
vec2
Vec2 vec2(float x=0, float y=0)
2D vector construction from components x and y
Definition: math2d.h:30
translation
Vec2 translation()
get current translation vector
Definition: math2d.cpp:104
Mat3::r1z
float r1z
Definition: math2d.h:24
scaling
float scaling(float aspect=2)
get current scaling factor
Definition: math2d.cpp:135
Vec3::x
float x
Definition: math2d.h:16
col3
Vec3 col3(const Mat3 &m)
Definition: math2d.h:104
Mat3::r2x
float r2x
Definition: math2d.h:25
mul3
Mat3 mul3(const Mat3 &m1, const Mat3 &m2)
3D matrix multiplication
Definition: math2d.h:107
translate
void translate(Vec2 v)
apply translation vector to current transformation
Definition: math2d.cpp:68
Mat3
3D matrix type
Definition: math2d.h:22
row1
Vec3 row1(const Mat3 &m)
get 3D matrix row
Definition: math2d.h:97
vec3_y
int vec3_y(Vec3 v)
convert y-component of 3D vector to rounded int
Definition: math2d.h:148
dot3
float dot3(Vec3 a, Vec3 b)
3D dot product
Definition: math2d.h:91
vec2_x
int vec2_x(Vec2 v)
convert x-component of 2D vector to rounded int
Definition: math2d.h:130
Mat3::r2z
float r2z
Definition: math2d.h:25
vec3_x
int vec3_x(Vec3 v)
convert x-component of 3D vector to rounded int
Definition: math2d.h:142
vec3_z
int vec3_z(Vec3 v)
convert z-component of 3D vector to rounded int
Definition: math2d.h:154
mul3v
Vec2 mul3v(const Mat3 &m, Vec3 v)
3D matrix multiplication with right-hand side vector
Definition: math2d.h:115
top
Mat3 top()
get current transformation represented by a 3x3 homogeneous matrix the current transformation is the ...
Definition: math2d.cpp:13
mul2
Vec2 mul2(Vec2 a, Vec2 b)
2D vector multiplication
Definition: math2d.h:73
Vec2::y
float y
Definition: math2d.h:10
Mat3::r3x
float r3x
Definition: math2d.h:26
mul2s
Vec2 mul2s(Vec2 a, float s)
2D vector multiplication with scalar value
Definition: math2d.h:79
col1
Vec3 col1(const Mat3 &m)
get 3D matrix column
Definition: math2d.h:102
Mat3::r1x
float r1x
Definition: math2d.h:24
mat3
Mat3 mat3(float r1x=1, float r1y=0, float r1z=0, float r2x=0, float r2y=1, float r2z=0, float r3x=0, float r3y=0, float r3z=1)
3D matrix construction
Definition: math2d.h:52
replace
void replace(const Mat3 &m)
replace current transformation with a given transformation matrix
Definition: math2d.cpp:35
Mat3::r1y
float r1y
Definition: math2d.h:24
Mat3::r2y
float r2y
Definition: math2d.h:25
row2
Vec3 row2(const Mat3 &m)
Definition: math2d.h:98
Vec3::y
float y
Definition: math2d.h:17
col2
Vec3 col2(const Mat3 &m)
Definition: math2d.h:103
Vec2::x
float x
Definition: math2d.h:9
row3
Vec3 row3(const Mat3 &m)
Definition: math2d.h:99