Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Math/geom2d.h
5672 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
// Moved here from UI, it has general uses too.
29
enum Orientation {
30
ORIENT_HORIZONTAL,
31
ORIENT_VERTICAL,
32
};
33
34
// Possibly, we'll add a mode for the book-style dual screen phones later.
35
// TODO: Find a better home for this!
36
enum class DeviceOrientation {
37
Landscape = 0,
38
Portrait = 1,
39
};
40
41
// Workaround for X header, ugh.
42
#undef Opposite
43
44
inline constexpr Orientation Opposite(Orientation o) {
45
return (o == ORIENT_HORIZONTAL) ? ORIENT_VERTICAL : ORIENT_HORIZONTAL;
46
}
47
48
// Resolved bounds on screen after layout.
49
struct Bounds {
50
Bounds() : x(0.0f), y(0.0f), w(0.0f), h(0.0f) {}
51
Bounds(float x_, float y_, float w_, float h_) : x(x_), y(y_), w(w_), h(h_) {}
52
53
static Bounds FromCenter(float x_, float y_, float radius) {
54
return Bounds(x_ - radius, y_ - radius, radius * 2.0f, radius * 2.0f);
55
}
56
57
static Bounds FromCenterWH(float x_, float y_, float w, float h) {
58
return Bounds(x_ - w * 0.5f, y_ - h * 0.5f, w, h);
59
}
60
61
float GetSize(Orientation o) const {
62
return (o == ORIENT_HORIZONTAL) ? w : h;
63
}
64
65
bool Contains(float px, float py) const {
66
return (px >= x && py >= y && px < x + w && py < y + h);
67
}
68
69
bool Intersects(const Bounds &other) const {
70
return !(x > other.x2() || x2() < other.x || y > other.y2() || y2() < other.y);
71
}
72
73
void Clip(const Bounds &clipTo) {
74
if (x < clipTo.x) {
75
w -= clipTo.x - x;
76
x = clipTo.x;
77
}
78
if (y < clipTo.y) {
79
h -= clipTo.y - y;
80
y = clipTo.y;
81
}
82
if (x2() > clipTo.x2()) {
83
w = clipTo.x2() - x;
84
}
85
if (y2() > clipTo.y2()) {
86
h = clipTo.y2() - y;
87
}
88
}
89
90
float x2() const { return x + w; }
91
float y2() const { return y + h; }
92
float centerX() const { return x + w * 0.5f; }
93
float centerY() const { return y + h * 0.5f; }
94
float radius() const {
95
return ((w > h) ? w : h) * 0.5f;
96
}
97
Point2D Center() const {
98
return Point2D(centerX(), centerY());
99
}
100
Bounds Expand(float amount) const {
101
return Bounds(x - amount, y - amount, w + amount * 2, h + amount * 2);
102
}
103
Bounds Expand(float xAmount, float yAmount) const {
104
return Bounds(x - xAmount, y - yAmount, w + xAmount * 2, h + yAmount * 2);
105
}
106
Bounds Expand(float left, float top, float right, float bottom) const {
107
return Bounds(x - left, y - top, w + left + right, h + top + bottom);
108
}
109
Bounds Offset(float xAmount, float yAmount) const {
110
return Bounds(x + xAmount, y + yAmount, w, h);
111
}
112
Bounds Inset(float left, float top, float right, float bottom) {
113
return Bounds(x + left, y + top, w - left - right, h - bottom - top);
114
}
115
116
Bounds Inset(float xAmount, float yAmount) const {
117
return Bounds(x + xAmount, y + yAmount, w - xAmount * 2, h - yAmount * 2);
118
}
119
Bounds Inset(float left, float top, float right, float bottom) const {
120
return Bounds(x + left, y + top, w - left - right, h - top - bottom);
121
}
122
123
float AspectRatio() const {
124
return w / h;
125
}
126
127
float x;
128
float y;
129
float w;
130
float h;
131
};
132
133
134
135