11constexpr float kScalarRoot2Over2 = 0.707106781f;
18 [[nodiscard]]
static constexpr Point zero() {
return Point{0.0f, 0.0f}; }
19 [[nodiscard]]
static constexpr Point fromXY(
float px,
float py) {
return Point{px, py}; }
21 [[nodiscard]]
bool isZero()
const {
return x == 0.0f && y == 0.0f; }
22 [[nodiscard]]
bool isFinite()
const {
return std::isfinite(x) && std::isfinite(y); }
26 float mag2 = x * x + y * y;
27 if (std::isfinite(mag2)) {
28 return std::sqrt(mag2);
30 double xx = x, yy = y;
31 return static_cast<float>(std::sqrt(xx * xx + yy * yy));
34 [[nodiscard]]
float lengthSquared()
const {
return x * x + y * y; }
38 return Point{x - other.x, y - other.y}.length();
41 [[nodiscard]]
float distanceToSquared(
const Point& other)
const {
42 float dx = x - other.x;
43 float dy = y - other.y;
44 return dx * dx + dy * dy;
47 [[nodiscard]]
constexpr float dot(
const Point& other)
const {
return x * other.x + y * other.y; }
49 [[nodiscard]]
constexpr float cross(
const Point& other)
const {
50 return x * other.y - y * other.x;
53 [[nodiscard]]
bool canNormalize()
const {
54 return std::isfinite(x) && std::isfinite(y) && (x != 0.0f || y != 0.0f);
60 bool setNormalize(
float px,
float py) {
68 double xx = x, yy = y;
69 double dmag = std::sqrt(xx * xx + yy * yy);
70 double dscale =
static_cast<double>(len) / dmag;
71 x *=
static_cast<float>(dscale);
72 y *=
static_cast<float>(dscale);
73 if (!std::isfinite(x) || !std::isfinite(y) || (x == 0.0f && y == 0.0f)) {
81 void scale(
float factor) {
86 [[nodiscard]]
constexpr Point scaled(
float factor)
const {
return Point{x * factor, y * factor}; }
102 [[nodiscard]]
bool equalsWithinTolerance(
const Point& other,
float tolerance)
const {
103 return std::abs(x - other.x) <= tolerance && std::abs(y - other.y) <= tolerance;
106 constexpr bool operator==(
const Point&)
const =
default;
109[[nodiscard]]
inline constexpr Point operator-(
const Point& a,
const Point& b) {
110 return Point{a.x - b.x, a.y - b.y};
113[[nodiscard]]
inline constexpr Point operator+(
const Point& a,
const Point& b) {
114 return Point{a.x + b.x, a.y + b.y};
117[[nodiscard]]
inline constexpr Point operator-(
const Point& a) {
return Point{-a.x, -a.y}; }
2D point / vector with float components.
Definition Point.h:14
void rotateCounterClockwise()
Rotates 90 degrees counter-clockwise.
Definition Point.h:96
bool normalize()
Normalizes in-place. Returns false if zero-length.
Definition Point.h:58
float length() const
Euclidean length.
Definition Point.h:25
bool setLength(float len)
Sets the length, preserving direction. Returns false on failure.
Definition Point.h:67
float distance(const Point &other) const
Distance to another point.
Definition Point.h:37
void rotateClockwise()
Rotates 90 degrees clockwise.
Definition Point.h:89