Line data Source code
1 : #pragma once
2 : #include "math3d/core.hpp"
3 :
4 : namespace m3d
5 : {
6 :
7 : struct vec2i
8 : {
9 : int x, y;
10 :
11 : // Constructors
12 1 : vec2i() : x(0), y(0) {}
13 36 : vec2i(int _x, int _y) : x(_x), y(_y) {}
14 1 : vec2i(int _d) : x(_d), y(_d) {}
15 :
16 : // Operator Overloading for clean syntax
17 1 : vec2i operator+(const vec2i &rhs) const { return {x + rhs.x, y + rhs.y}; }
18 1 : vec2i operator-(const vec2i &rhs) const { return {x - rhs.x, y - rhs.y}; }
19 1 : vec2i operator*(int s) const { return {x * s, y * s}; }
20 2 : vec2i operator/(int s) const
21 : {
22 2 : return {x / s, y / s};
23 : }
24 :
25 : // Unary negation
26 1 : vec2i operator-() const { return {-x, -y}; }
27 : // In-place operators (important for performance)
28 1 : vec2i &operator+=(const vec2i &rhs)
29 : {
30 1 : x += rhs.x;
31 1 : y += rhs.y;
32 1 : return *this;
33 : }
34 :
35 1 : vec2i &operator-=(const vec2i &rhs)
36 : {
37 1 : x -= rhs.x;
38 1 : y -= rhs.y;
39 1 : return *this;
40 : }
41 :
42 1 : vec2i &operator*=(int s)
43 : {
44 1 : x *= s;
45 1 : y *= s;
46 1 : return *this;
47 : }
48 :
49 14 : bool operator==(const vec2i &other) const
50 : {
51 14 : return x == other.x && y == other.y;
52 : }
53 :
54 2 : bool operator!=(const vec2i &other) const
55 : {
56 2 : return !(*this == other);
57 : }
58 :
59 : // Meber acces via indices
60 2 : int &operator[](int i)
61 : {
62 2 : return (i == 0) ? x : y;
63 : }
64 :
65 2 : const int &operator[](int i) const
66 : {
67 2 : return (i == 0) ? x : y;
68 : }
69 :
70 1 : friend std::ostream &operator<<(std::ostream &os, const vec2i &v)
71 : {
72 1 : return os << "vec2i(" << v.x << ", " << v.y << ")";
73 : }
74 : };
75 :
76 : // Left-side scalar multiplication (s * v)
77 1 : inline vec2i operator*(int s, const vec2i &v) { return {v.x * s, v.y * s}; }
78 :
79 : // --- Vector Functions ---
80 :
81 3 : inline int dot(const vec2i &a, const vec2i &b)
82 : {
83 3 : return a.x * b.x + a.y * b.y;
84 : }
85 :
86 3 : inline int cross(const vec2i &a, const vec2i &b)
87 : {
88 3 : return a.x * b.y - a.y * b.x;
89 : }
90 :
91 :
92 : } // namespace m3d
|