-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.h
58 lines (47 loc) · 1.5 KB
/
math.h
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
#ifndef MATH_H
#define MATH_H
// VECTOR-RELATED FUNCTIONS
/*
* vect2 provides a lightweight 2D vector class for operations such as
* addition, scaling, normalization, and dot products. Useful for
* managing positions, velocities, and directions in 2D space.
*/
/* Performance Tips and Notes:
* Avoid Repeated Normalization: Normalization can be computationally
* intensive. Cache the normalized vector if it will be reused frequently.
* Use Temporary Variables for Large Calculations: When chaining operations,
* intermediate temporary variables can help keep the code clear and performant.
*/
/* Memory Layout:
* Vect2 is small and can be passed by value, but for larger structures,
* prefer passing by reference.
*/
/* Future Improvements (if relevant):
* Consider adding additional vector operations, such as
* cross-product for 3D, or conversion functions if interfacing with
* libraries that have their own vector types.
*/
class Vect2 {
private:
float x;
float y;
public:
Vect2() : x(0), y(0) {} // Default constructor
Vect2(float xVal, float yVal) : x(xVal), y(yVal) {} // Parameterized constructor
// Getter methods
float getX() const { return x; }
float getY() const { return y; }
// Setter methods
void setX(float xVal) { x = xVal; }
void setY(float yVal) { x = yVal; }
// Other member functions
float magnitude() const { return sqrt(x * x + y * y); }
void normalize(){
float mag = magnitude();
if (mag > 0) {
x /= mag;
y /= mag;
}
}
};
#endif // MATH_H