Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Math/geom2d.h
3186 views
1
#pragma once
2
3
#include <cmath>
4
5
struct Point2D {
6
Point2D() : x(0.0f), y(0.0f) {}
7
Point2D(float x_, float y_) : x(x_), y(y_) {}
8
9
float x;
10
float y;
11
12
float distanceTo(const Point2D &other) const {
13
float dx = other.x - x, dy = other.y - y;
14
return sqrtf(dx*dx + dy*dy);
15
}
16
17
bool operator ==(const Point2D &other) const {
18
return x == other.x && y == other.y;
19
}
20
21
/*
22
FocusDirection directionTo(const Point &other) const {
23
int angle = atan2f(other.y - y, other.x - x) / (2 * M_PI) - 0.125;
24
25
}*/
26
};
27
28
29
// Resolved bounds on screen after layout.
30
struct Bounds {
31
Bounds() : x(0.0f), y(0.0f), w(0.0f), h(0.0f) {}
32
Bounds(float x_, float y_, float w_, float h_) : x(x_), y(y_), w(w_), h(h_) {}
33
34
static Bounds FromCenter(float x_, float y_, float radius) {
35
return Bounds(x_ - radius, y_ - radius, radius * 2.0f, radius * 2.0f);
36
}
37
38
bool Contains(float px, float py) const {
39
return (px >= x && py >= y && px < x + w && py < y + h);
40
}
41
42
bool Intersects(const Bounds &other) const {
43
return !(x > other.x2() || x2() < other.x || y > other.y2() || y2() < other.y);
44
}
45
46
void Clip(const Bounds &clipTo) {
47
if (x < clipTo.x) {
48
w -= clipTo.x - x;
49
x = clipTo.x;
50
}
51
if (y < clipTo.y) {
52
h -= clipTo.y - y;
53
y = clipTo.y;
54
}
55
if (x2() > clipTo.x2()) {
56
w = clipTo.x2() - x;
57
}
58
if (y2() > clipTo.y2()) {
59
h = clipTo.y2() - y;
60
}
61
}
62
63
float x2() const { return x + w; }
64
float y2() const { return y + h; }
65
float centerX() const { return x + w * 0.5f; }
66
float centerY() const { return y + h * 0.5f; }
67
float radius() const {
68
return ((w > h) ? w : h) * 0.5f;
69
}
70
Point2D Center() const {
71
return Point2D(centerX(), centerY());
72
}
73
Bounds Expand(float amount) const {
74
return Bounds(x - amount, y - amount, w + amount * 2, h + amount * 2);
75
}
76
Bounds Expand(float xAmount, float yAmount) const {
77
return Bounds(x - xAmount, y - yAmount, w + xAmount * 2, h + yAmount * 2);
78
}
79
Bounds Expand(float left, float top, float right, float bottom) const {
80
return Bounds(x - left, y - top, w + left + right, h + top + bottom);
81
}
82
Bounds Offset(float xAmount, float yAmount) const {
83
return Bounds(x + xAmount, y + yAmount, w, h);
84
}
85
Bounds Inset(float left, float top, float right, float bottom) {
86
return Bounds(x + left, y + top, w - left - right, h - bottom - top);
87
}
88
89
Bounds Inset(float xAmount, float yAmount) const {
90
return Bounds(x + xAmount, y + yAmount, w - xAmount * 2, h - yAmount * 2);
91
}
92
Bounds Inset(float left, float top, float right, float bottom) const {
93
return Bounds(x + left, y + top, w - left - right, h - top - bottom);
94
}
95
96
float x;
97
float y;
98
float w;
99
float h;
100
};
101
102
103
104