tiny-skia-cpp
A C++20 2D rendering library (port of tiny-skia)
Loading...
Searching...
No Matches
Point.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <cmath>
7
8namespace tiny_skia {
9
11constexpr float kScalarRoot2Over2 = 0.707106781f;
12
14struct Point {
15 float x = 0.0f;
16 float y = 0.0f;
17
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}; }
20
21 [[nodiscard]] bool isZero() const { return x == 0.0f && y == 0.0f; }
22 [[nodiscard]] bool isFinite() const { return std::isfinite(x) && std::isfinite(y); }
23
25 [[nodiscard]] float length() const {
26 float mag2 = x * x + y * y;
27 if (std::isfinite(mag2)) {
28 return std::sqrt(mag2);
29 }
30 double xx = x, yy = y;
31 return static_cast<float>(std::sqrt(xx * xx + yy * yy));
32 }
33
34 [[nodiscard]] float lengthSquared() const { return x * x + y * y; }
35
37 [[nodiscard]] float distance(const Point& other) const {
38 return Point{x - other.x, y - other.y}.length();
39 }
40
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;
45 }
46
47 [[nodiscard]] constexpr float dot(const Point& other) const { return x * other.x + y * other.y; }
48
49 [[nodiscard]] constexpr float cross(const Point& other) const {
50 return x * other.y - y * other.x;
51 }
52
53 [[nodiscard]] bool canNormalize() const {
54 return std::isfinite(x) && std::isfinite(y) && (x != 0.0f || y != 0.0f);
55 }
56
58 bool normalize() { return setLength(1.0f); }
59
60 bool setNormalize(float px, float py) {
61 x = px;
62 y = py;
63 return setLength(1.0f);
64 }
65
67 bool setLength(float len) {
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)) {
74 x = 0.0f;
75 y = 0.0f;
76 return false;
77 }
78 return true;
79 }
80
81 void scale(float factor) {
82 x *= factor;
83 y *= factor;
84 }
85
86 [[nodiscard]] constexpr Point scaled(float factor) const { return Point{x * factor, y * factor}; }
87
90 float tmp = x;
91 x = -y;
92 y = tmp;
93 }
94
97 float tmp = x;
98 x = y;
99 y = -tmp;
100 }
101
102 [[nodiscard]] bool equalsWithinTolerance(const Point& other, float tolerance) const {
103 return std::abs(x - other.x) <= tolerance && std::abs(y - other.y) <= tolerance;
104 }
105
106 constexpr bool operator==(const Point&) const = default;
107};
108
109[[nodiscard]] inline constexpr Point operator-(const Point& a, const Point& b) {
110 return Point{a.x - b.x, a.y - b.y};
111}
112
113[[nodiscard]] inline constexpr Point operator+(const Point& a, const Point& b) {
114 return Point{a.x + b.x, a.y + b.y};
115}
116
117[[nodiscard]] inline constexpr Point operator-(const Point& a) { return Point{-a.x, -a.y}; }
118
119} // namespace tiny_skia
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