Recti 1.2.4
Loading...
Searching...
No Matches
point.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <tuple> // for std::tie()
8#include <utility> // for std::move
9
10#include "generic.hpp"
11#include "vector2.hpp"
12
13namespace recti {
14
15 template <typename T1, typename T2> class Point;
16
30 template <typename T1 = int, typename T2 = T1> class Point {
31 using Self = Point<T1, T2>;
32
37 template <typename, typename> friend class Point;
39
40 T1 _xcoord;
41 T2 _ycoord;
42
43 public:
44 using value_type = T1;
45
50 constexpr Point() : _xcoord{}, _ycoord{} {}
51
65 constexpr Point(T1 xcoord, T2 ycoord) noexcept
66 : _xcoord{std::move(xcoord)}, _ycoord{std::move(ycoord)} {}
67
72 constexpr auto xcoord() const -> const T1& { return this->_xcoord; }
73
78 constexpr auto ycoord() const -> const T2& { return this->_ycoord; }
79
84
97 template <typename U1, typename U2>
98 constexpr auto operator==(const Point<U1, U2>& rhs) const -> bool {
99 return std::tie(this->xcoord(), this->ycoord()) == std::tie(rhs.xcoord(), rhs.ycoord());
100 }
101
113 template <typename U1, typename U2> //
114 constexpr auto operator<(const Point<U1, U2>& rhs) const -> bool {
115 return std::tie(this->xcoord(), this->ycoord()) < std::tie(rhs.xcoord(), rhs.ycoord());
116 }
117
130 template <typename U1, typename U2>
131 constexpr auto operator!=(const Point<U1, U2>& rhs) const -> bool {
132 return !(*this == rhs);
133 }
134
136
141
152 template <typename U1, typename U2>
153 CONSTEXPR14 auto operator+=(const Vector2<U1, U2>& right_point) -> Self& {
154 this->_xcoord += right_point.x();
155 this->_ycoord += right_point.y();
156 return *this;
157 }
158
169 template <typename U1, typename U2> //
170 friend constexpr auto operator+(Point lhs, const Vector2<U1, U2>& vector) {
171 auto xcoord = lhs.xcoord() + vector.x();
172 auto ycoord = lhs.ycoord() + vector.y();
173 return Point<decltype(xcoord), decltype(ycoord)>{std::move(xcoord), std::move(ycoord)};
174 }
175
186 template <typename U1, typename U2>
187 CONSTEXPR14 auto operator-=(const Vector2<U1, U2>& right_point) -> Self& {
188 this->_xcoord -= right_point.x();
189 this->_ycoord -= right_point.y();
190 return *this;
191 }
192
203 template <typename U1, typename U2> //
204 friend constexpr auto operator-(Point lhs, const Vector2<U1, U2>& vector) {
205 auto xcoord = lhs.xcoord() - vector.x();
206 auto ycoord = lhs.ycoord() - vector.y();
207 return Point<decltype(xcoord), decltype(ycoord)>{std::move(xcoord), std::move(ycoord)};
208 }
209
216 constexpr auto operator-(const Self& other_point) const {
217 auto xcoord = this->xcoord() - other_point.xcoord();
218 auto ycoord = this->ycoord() - other_point.ycoord();
219 return Vector2<decltype(xcoord), decltype(ycoord)>{std::move(xcoord),
220 std::move(ycoord)};
221 }
222
228 constexpr auto measure() const {
229 return measure_of(this->xcoord()) * measure_of(this->ycoord());
230 }
231
237 constexpr auto flip_xy() const -> Point<T2, T1> { return {this->ycoord(), this->xcoord()}; }
238
244 constexpr auto flip_y() const -> Point<T1, T2> { return {-this->xcoord(), this->ycoord()}; }
245
254 template <typename U1, typename U2> //
255 constexpr auto overlaps(const Point<U1, U2>& other_point) const -> bool {
256 return overlap(this->xcoord(), other_point.xcoord())
257 && overlap(this->ycoord(), other_point.ycoord());
258 }
259
268 template <typename U1, typename U2> //
269 constexpr auto intersect_with(const Point<U1, U2>& other_point) const {
270 auto xcoord = intersection(this->xcoord(), other_point.xcoord());
271 auto ycoord = intersection(this->ycoord(), other_point.ycoord());
272 return Point<decltype(xcoord), decltype(ycoord)>{std::move(xcoord), std::move(ycoord)};
273 }
274
283 template <typename U1, typename U2> //
284 constexpr auto hull_with(const Point<U1, U2>& other_point) const {
285 auto xcoord = hull(this->xcoord(), other_point.xcoord());
286 auto ycoord = hull(this->ycoord(), other_point.ycoord());
287 return Point<decltype(xcoord), decltype(ycoord)>{std::move(xcoord), std::move(ycoord)};
288 }
289
298 template <typename U1, typename U2> //
299 constexpr auto contains(const Point<U1, U2>& other_point) const -> bool {
300 return contain(this->xcoord(), other_point.xcoord())
301 && contain(this->ycoord(), other_point.ycoord());
302 }
303
312 template <typename U1, typename U2> //
313 constexpr auto blocks(const Point<U1, U2>& other_point) const -> bool {
314 return (contain(this->xcoord(), other_point.xcoord())
315 && contain(other_point.ycoord(), this->ycoord()))
316 || (contain(this->ycoord(), other_point.ycoord())
317 && contain(other_point.xcoord(), this->xcoord()));
318 }
319
328 template <typename U1, typename U2> //
329 constexpr auto min_dist_with(const Point<U1, U2>& other_point) const {
330 return min_dist(this->xcoord(), other_point.xcoord())
331 + min_dist(this->ycoord(), other_point.ycoord());
332 }
333
342 template <typename U1, typename U2> //
343 constexpr auto min_dist_change_with(Point<U1, U2>& other_point) {
344 return min_dist_change(this->_xcoord, other_point._xcoord)
345 + min_dist_change(this->_ycoord, other_point._ycoord);
346 }
347
354 template <typename T> constexpr auto enlarge_with(const T& value) const {
355 auto xb = enlarge(this->xcoord(), value);
356 auto yb = enlarge(this->ycoord(), value);
357 return Point<decltype(xb), decltype(yb)>{std::move(xb), std::move(yb)};
358 }
359
366 template <typename U1, typename U2> //
367 constexpr auto nearest_to(const Point<U1, U2>& other_point) const {
368 auto xcoord = nearest(this->xcoord(), other_point.xcoord());
369 auto ycoord = nearest(this->ycoord(), other_point.ycoord());
370 return Point<U1, U2>{std::move(xcoord), std::move(ycoord)};
371 }
372
378 constexpr auto rotates() const -> Point<T1, T2> {
379 if constexpr (std::is_same_v<T1, Point>) {
380 auto pt = _xcoord.rotates();
381 auto pt2 = Point(pt.ycoord(), _ycoord).rotates();
382 auto pt3 = Point(pt.xcoord(), pt2.ycoord()).rotates();
383 return Point(Point(pt3.xcoord(), pt2.xcoord()), pt3.ycoord());
384 } else {
385 auto xcoord = _xcoord - _ycoord;
386 auto ycoord = _xcoord + _ycoord;
387 return Point<T1, T2>(xcoord, ycoord);
388 }
389 }
390
396 constexpr auto inv_rotates() const -> Point<T1, T2> {
397 if constexpr (std::is_same_v<T1, Point>) {
398 auto pt = Point(_xcoord.xcoord(), _ycoord).inv_rotates();
399 auto pt2 = Point(_xcoord.ycoord(), pt.ycoord()).inv_rotates();
400 auto pt3 = Point(pt.xcoord(), pt2.xcoord()).inv_rotates();
401 return Point(Point(pt3.xcoord(), pt3.ycoord()), pt2.ycoord());
402 } else {
403 auto xcoord = (_xcoord + _ycoord) / 2;
404 auto ycoord = (-_xcoord + _ycoord) / 2;
405 return Point<T1, T2>(xcoord, ycoord);
406 }
407 }
408
414 constexpr auto get_center() const {
415 auto xcoord = center(_xcoord);
416 auto ycoord = center(_ycoord);
417 return Point<decltype(xcoord), decltype(ycoord)>{std::move(xcoord), std::move(ycoord)};
418 }
419
425 constexpr auto lower_corner() const {
426 auto xcoord = lower(_xcoord);
427 auto ycoord = lower(_ycoord);
428 return Point<decltype(xcoord), decltype(ycoord)>{std::move(xcoord), std::move(ycoord)};
429 }
430
436 constexpr auto upper_corner() const {
437 auto xcoord = upper(_xcoord);
438 auto ycoord = upper(_ycoord);
439 return Point<decltype(xcoord), decltype(ycoord)>{std::move(xcoord), std::move(ycoord)};
440 }
441
449 template <class Stream> friend auto operator<<(Stream& output_stream, const Point& obj)
450 -> Stream& {
451 output_stream << "(" << obj.xcoord() << ", " << obj.ycoord() << ")";
452 return output_stream;
453 }
454
455 protected:
461 CONSTEXPR14 auto get_xcoord() -> T1& { return this->_xcoord; }
462
468 constexpr auto get_xcoord() const -> const T1& { return this->_xcoord; }
469
475 CONSTEXPR14 auto get_ycoord() -> T2& { return this->_ycoord; }
476
482 constexpr auto get_ycoord() const -> const T2& { return this->_ycoord; }
483 };
484
485} // namespace recti
Point.
Definition point.hpp:30
CONSTEXPR14 auto operator-=(const Vector2< U1, U2 > &right_point) -> Self &
Subtracts a vector (translation) from this Point.
Definition point.hpp:187
friend auto operator<<(Stream &output_stream, const Point &obj) -> Stream &
Definition point.hpp:449
constexpr auto intersect_with(const Point< U1, U2 > &other_point) const
Definition point.hpp:269
constexpr auto rotates() const -> Point< T1, T2 >
Rotate the point by 45 degrees (used for ManhattanArc).
Definition point.hpp:378
constexpr Point()
Default constructor.
Definition point.hpp:50
friend constexpr auto operator+(Point lhs, const Vector2< U1, U2 > &vector)
Adds a vector (translation) to this Point.
Definition point.hpp:170
constexpr auto inv_rotates() const -> Point< T1, T2 >
Inverse rotate the point (used for ManhattanArc).
Definition point.hpp:396
constexpr Point(T1 xcoord, T2 ycoord) noexcept
Construct a new Point object.
Definition point.hpp:65
T1 value_type
Definition point.hpp:44
constexpr auto operator==(const Point< U1, U2 > &rhs) const -> bool
Compares this Point object with another Point object for equality.
Definition point.hpp:98
constexpr auto xcoord() const -> const T1 &
Gets the x coordinate of this Point.
Definition point.hpp:72
constexpr auto blocks(const Point< U1, U2 > &other_point) const -> bool
Definition point.hpp:313
constexpr auto get_center() const
Calculate the center of the point.
Definition point.hpp:414
constexpr auto min_dist_with(const Point< U1, U2 > &other_point) const
minimum distance between this point and another point
Definition point.hpp:329
constexpr auto operator-(const Self &other_point) const
Calculates the displacement vector between this point and another point.
Definition point.hpp:216
friend constexpr auto operator-(Point lhs, const Vector2< U1, U2 > &vector)
Definition point.hpp:204
CONSTEXPR14 auto operator+=(const Vector2< U1, U2 > &right_point) -> Self &
Adds a vector (translation) to this Point.
Definition point.hpp:153
constexpr auto hull_with(const Point< U1, U2 > &other_point) const
Definition point.hpp:284
CONSTEXPR14 auto get_xcoord() -> T1 &
Returns a mutable reference to the x-coordinate of the point.
Definition point.hpp:461
constexpr auto nearest_to(const Point< U1, U2 > &other_point) const
Definition point.hpp:367
constexpr auto operator<(const Point< U1, U2 > &rhs) const -> bool
Compares this Point object with another Point object to check if it is less than.
Definition point.hpp:114
constexpr auto get_xcoord() const -> const T1 &
Returns a const reference to the x-coordinate of the point.
Definition point.hpp:468
constexpr auto measure() const
measure (area, volume etc.)
Definition point.hpp:228
constexpr auto lower_corner() const
Calculate the lower corner of the point.
Definition point.hpp:425
constexpr auto min_dist_change_with(Point< U1, U2 > &other_point)
minimum distance between this point and another point
Definition point.hpp:343
constexpr auto contains(const Point< U1, U2 > &other_point) const -> bool
Definition point.hpp:299
constexpr auto flip_xy() const -> Point< T2, T1 >
flip_xy according to xcoord-ycoord diagonal line
Definition point.hpp:237
constexpr auto get_ycoord() const -> const T2 &
Returns a const reference to the y-coordinate of the point.
Definition point.hpp:482
constexpr auto operator!=(const Point< U1, U2 > &rhs) const -> bool
Compares this Point object with another Point object for inequality.
Definition point.hpp:131
constexpr auto upper_corner() const
Calculate the upper corner of the point.
Definition point.hpp:436
constexpr auto ycoord() const -> const T2 &
Gets the y coordinate of this Point.
Definition point.hpp:78
constexpr auto overlaps(const Point< U1, U2 > &other_point) const -> bool
Definition point.hpp:255
constexpr auto enlarge_with(const T &value) const
Enlarge the point by value
Definition point.hpp:354
constexpr auto flip_y() const -> Point< T1, T2 >
flip according to ycoord-axis
Definition point.hpp:244
CONSTEXPR14 auto get_ycoord() -> T2 &
Returns a mutable reference to the y-coordinate of the point.
Definition point.hpp:475
2D vector template class.
Definition vector2.hpp:28
constexpr auto x() const noexcept -> const T1 &
Returns a const reference to the x-coordinate of the vector.
Definition vector2.hpp:72
constexpr auto y() const noexcept -> const T2 &
Returns a const reference to the y-coordinate of the vector.
Definition vector2.hpp:81
Generic free functions for geometric operations (overlap, intersection, hull, distance).
Definition svg_utils.hpp:12
constexpr auto contain(const U1 &lhs, const U2 &rhs) -> bool
Check if one object contains another object.
Definition generic.hpp:90
constexpr auto nearest(const U1 &lhs, const U2 &rhs)
Returns the nearest point on lhs to rhs.
Definition generic.hpp:227
constexpr auto hull(const U1 &left, const U2 &right)
Computes the hull of two objects.
Definition interval.hpp:554
constexpr auto enlarge(const U1 &left, const U2 &right)
Enlarges an interval or scalar value by adding and subtracting a given value.
Definition interval.hpp:579
constexpr auto intersection(const U1 &lhs, const U2 &rhs)
Computes the intersection of two objects.
Definition generic.hpp:128
constexpr auto min_dist_change(U1 &lhs, U2 &rhs)
Calculates the minimum distance between two objects lhs and rhs, with the ability to handle a change ...
Definition generic.hpp:195
constexpr auto min_dist(const U1 &lhs, const U2 &rhs)
Calculates the minimum distance between two objects lhs and rhs.
Definition generic.hpp:166
constexpr auto lower(const U &obj)
Calculates the lower corner of an object.
Definition generic.hpp:281
constexpr auto overlap(const U1 &lhs, const U2 &rhs) -> bool
Checks if two objects overlap.
Definition generic.hpp:54
constexpr auto measure_of(const U &obj)
Calculates the measure (length, area, etc.) of an object.
Definition generic.hpp:245
constexpr auto upper(const U &obj)
Calculates the upper corner of an object.
Definition generic.hpp:299
constexpr auto center(const U &obj)
Calculates the center of an object.
Definition generic.hpp:263
2D vector template class with arithmetic and comparison operations.
#define CONSTEXPR14
Definition vector2.hpp:13