Repository navigation
Expand file tree
/
Copy pathvector2D.h
More file actions
84 lines (73 loc) · 2.08 KB
/
Copy pathvector2D.h
File metadata and controls
84 lines (73 loc) · 2.08 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
#pragma once
#include <math.h>
/**
* @brief Represents a 2D vector with basic math operations.
*
* Stores x and y components as floats. Supports addition,
* subtraction, scalar multiplication, magnitude, normalization,
* and dot product.
*/
class vec2D {
public:
float x; ///< x component
float y; ///< y component
/**
* @brief Default constructor. Initializes x and y to 0.
*/
vec2D();
/**
* @brief Creates a vec2D with the given x and y components.
* @param x - x component
* @param y - y component
*/
vec2D(float x, float y);
/**
* @brief Adds two vectors component-wise.
* @param a - the vector to add
* @return a new vec2D equal to this + a
*/
vec2D operator+(const vec2D& a) const;
/**
* @brief Adds another vector to this one in place
* @param a - the vector to add
* @return reference to this vec2D after addition
*/
vec2D& operator+=(const vec2D& a);
/**
* @brief Subtracts two vectors component-wise.
* @param a - the vector to subtract
* @return a new vec2D equal to this - a
*/
vec2D operator-(const vec2D& a) const;
/**
* @brief Subtracts another vector to this one in place
* @param a - the vector to subtract
* @return reference to this vec2D after subtraction
*/
vec2D& operator-=(const vec2D& a);
/**
* @brief Scales the vector by a scalar.
* @param scalar - the value to multiply by
* @return a new vec2D equal to this * scalar
*/
vec2D operator*(float scalar) const;
/**
* @brief Computes the length of the vector.
* @return sqrt(x^2 + y^2)
*/
float magnitude() const;
/**
* @brief Returns a unit vector pointing in the same direction.
*
* Returns (0, 0) if the vector has zero length.
*
* @return normalized vec2D with magnitude 1
*/
vec2D normalize() const;
/**
* @brief Computes the dot product with another vector.
* @param a - the other vector
* @return x*a.x + y*a.y
*/
float dot(const vec2D& a) const;
};