Brick
Loading...
Searching...
No Matches
point2d.h
1#pragma once
2
3#include <Eigen/Dense>
4
5#include "cevalm.hpp"
6
7/// Class representing a lattice point
8class Point2d {
9 private:
10 int x_, y_;
11
12 public:
13 /// Default constructor for Point2d creating a lattice point at the origin
14 constexpr Point2d() : x_(0), y_(0) {}
15
16 /**
17 * Creates a lattice point at the coordinate (x,y)
18 * @param x The x-coordinate of the point
19 * @param y The y-coordinate of the point
20 */
21 constexpr Point2d(int x, int y) : x_(x), y_(y) {}
22
23 /**
24 * Creates a lattice point with the values from the given vector
25 * @param vector The vector whose values will be used
26 */
27 constexpr Point2d(Eigen::Vector2i vector) : x_(vector(0)), y_(vector(1)) {}
28
29 /// Gets the X coordinate of the point.
30 constexpr int x() const { return x_; }
31
32 /// Sets the x coordinate.
33 constexpr void set_x(int val) { x_ = val; }
34
35 /// Gets the Y coordinate of the point.
36 constexpr int y() const { return y_; }
37
38 /// Sets the y coordinate.
39 constexpr void set_y(int val) { y_ = val; }
40
41 /// Gets the point as an Eigen::Vector2i.
42 constexpr Eigen::Vector2i as_vector() const { return Eigen::Vector2i(x_, y_); }
43
44 /**
45 * Gets a vector in the canonical basis corresponding to the point in the basis of X and Y
46 * @param X The vector corresponding to <1, 0> in the basis of X and Y
47 * @param Y The vector corresponding to <0, 1> in the basis of X and Y
48 * @returns The point as a linear combination of the X and Y vectors
49 */
50 constexpr Eigen::Vector2d as_vector(Eigen::Vector2d X, Eigen::Vector2d Y) const {
51 return Eigen::Vector2d(x_ * X(0) + y_ * Y(0), x_ * X(1) + y_ * Y(1));
52 }
53
54 /// Calculates the manhattan distance between two points.
55 constexpr int manhattan_distance(Point2d other) const {
56 return cevalm::abs(x_ - other.x_) + cevalm::abs(y_ - other.y_);
57 }
58
59 /// Calculates the manhattan distance away from the origin.
60 constexpr int manhattan_norm() const { return cevalm::abs(x_) + cevalm::abs(y_); }
61
62 /// Calculates the distance (as a continuous number) between two points.
63 constexpr double distance(Point2d other) const {
64 return cevalm::hypot(x_ - other.x_, y_ - other.y_);
65 }
66
67 /// Calculates the distance (as a continuous number) away from the origin.
68 constexpr double norm() const { return cevalm::hypot(x_, y_); }
69
70 /**
71 * Returns the inverse of the point
72 *
73 * [x] = -[x]
74 * [y] = -[y]
75 *
76 * @return The inverse of the point
77 */
78 constexpr Point2d inverse() const {
79 return Point2d{-x_, -y_};
80 ;
81 }
82
83 /**
84 * Returns the dot product of two points
85 *
86 * [scalar] = [x][otherx] + [y][othery]
87 *
88 * @param other The other point to find the dot product with
89 * @return The scalar-valued dot product
90 */
91 constexpr int dot(Point2d other) const { return (x_ * other.x_) + (y_ * other.y_); }
92
93 /**
94 * Returns the sum of two points
95 *
96 * [x] = [x] + [otherx]
97 * [y] = [y] + [otherx]
98 *
99 * @param other The other point to be added
100 * @return The sum of the two points
101 */
102 constexpr Point2d operator+(Point2d other) const { return Point2d{x_ + other.x_, y_ + other.y_}; }
103
104 /// Adds another point to this point
105 constexpr Point2d& operator+=(Point2d other) { return *this = *this + other; }
106
107 /**
108 * Returns the difference of two points
109 *
110 * [x] = [x] - [otherx]
111 * [y] = [y] - [otherx]
112 *
113 * @param other The point being subtracted from this one
114 * @return The difference of the two points
115 */
116 constexpr Point2d operator-(Point2d other) const { return Point2d{x_ - other.x_, y_ - other.y_}; }
117
118 /// Subtracts another point from this point
119 constexpr Point2d& operator-=(Point2d other) { return *this = *this - other; }
120
121 /**
122 * Computes the inverse of the point
123 *
124 * [x] = -[x]
125 * [y] = -[y]
126 *
127 * @return The inverse of the point
128 */
129 constexpr Point2d operator-() const { return inverse(); }
130
131 /**
132 * Returns this point multiplied by a scalar
133 *
134 * [x] = [x] * [scalar]
135 * [y] = [y] * [scalar]
136 *
137 * @param scalar The scalar to multiply by
138 * @return This point multiplied by a scalar
139 */
140 constexpr Point2d operator*(int scalar) const { return Point2d{x_ * scalar, y_ * scalar}; }
141
142 /// Scales the point
143 friend constexpr Point2d operator*(int scalar, Point2d point) { return point * scalar; }
144
145 /**
146 * Computes the dot product of two points
147 *
148 * [scalar] = [x][otherx] + [y][othery]
149 *
150 * @param other The other point to find the dot product with
151 * @return The scalar-valued dot product
152 */
153 constexpr int operator*(Point2d other) const { return dot(other); }
154
155 /// Multiplies this point by a scalar
156 constexpr Point2d& operator*=(int scalar) { return *this = *this * scalar; }
157
158 /**
159 * Compares two points
160 * @param other The other Point2d to compare to
161 * @return TRUE if the components of both points are equal, and FALSE if otherwise
162 */
163 constexpr bool operator==(Point2d other) const { return x_ == other.x_ && y_ == other.y_; }
164};
constexpr void set_x(int val)
Sets the x coordinate.
Definition point2d.h:33
constexpr int manhattan_norm() const
Calculates the manhattan distance away from the origin.
Definition point2d.h:60
constexpr Point2d operator-(Point2d other) const
Definition point2d.h:116
constexpr bool operator==(Point2d other) const
Definition point2d.h:163
constexpr Point2d operator*(int scalar) const
Definition point2d.h:140
constexpr Eigen::Vector2i as_vector() const
Gets the point as an Eigen::Vector2i.
Definition point2d.h:42
constexpr Point2d inverse() const
Definition point2d.h:78
constexpr Point2d & operator+=(Point2d other)
Adds another point to this point.
Definition point2d.h:105
constexpr void set_y(int val)
Sets the y coordinate.
Definition point2d.h:39
constexpr Eigen::Vector2d as_vector(Eigen::Vector2d X, Eigen::Vector2d Y) const
Definition point2d.h:50
constexpr Point2d operator-() const
Definition point2d.h:129
constexpr double norm() const
Calculates the distance (as a continuous number) away from the origin.
Definition point2d.h:68
constexpr int operator*(Point2d other) const
Definition point2d.h:153
constexpr Point2d(Eigen::Vector2i vector)
Definition point2d.h:27
constexpr Point2d operator+(Point2d other) const
Definition point2d.h:102
constexpr int x() const
Gets the X coordinate of the point.
Definition point2d.h:30
constexpr Point2d(int x, int y)
Definition point2d.h:21
constexpr int y() const
Gets the Y coordinate of the point.
Definition point2d.h:36
constexpr Point2d()
Default constructor for Point2d creating a lattice point at the origin.
Definition point2d.h:14
constexpr Point2d & operator-=(Point2d other)
Subtracts another point from this point.
Definition point2d.h:119
constexpr Point2d & operator*=(int scalar)
Multiplies this point by a scalar.
Definition point2d.h:156
constexpr int manhattan_distance(Point2d other) const
Calculates the manhattan distance between two points.
Definition point2d.h:55
constexpr int dot(Point2d other) const
Definition point2d.h:91
friend constexpr Point2d operator*(int scalar, Point2d point)
Scales the point.
Definition point2d.h:143
constexpr double distance(Point2d other) const
Calculates the distance (as a continuous number) between two points.
Definition point2d.h:63