CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Math/geom2d.h
Views: 1401
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
bool Contains(float px, float py) const {
35
return (px >= x && py >= y && px < x + w && py < y + h);
36
}
37
38
bool Intersects(const Bounds &other) const {
39
return !(x > other.x2() || x2() < other.x || y > other.y2() || y2() < other.y);
40
}
41
42
void Clip(const Bounds &clipTo) {
43
if (x < clipTo.x) {
44
w -= clipTo.x - x;
45
x = clipTo.x;
46
}
47
if (y < clipTo.y) {
48
h -= clipTo.y - y;
49
y = clipTo.y;
50
}
51
if (x2() > clipTo.x2()) {
52
w = clipTo.x2() - x;
53
}
54
if (y2() > clipTo.y2()) {
55
h = clipTo.y2() - y;
56
}
57
}
58
59
float x2() const { return x + w; }
60
float y2() const { return y + h; }
61
float centerX() const { return x + w * 0.5f; }
62
float centerY() const { return y + h * 0.5f; }
63
Point2D Center() const {
64
return Point2D(centerX(), centerY());
65
}
66
Bounds Expand(float amount) const {
67
return Bounds(x - amount, y - amount, w + amount * 2, h + amount * 2);
68
}
69
Bounds Expand(float xAmount, float yAmount) const {
70
return Bounds(x - xAmount, y - yAmount, w + xAmount * 2, h + yAmount * 2);
71
}
72
Bounds Expand(float left, float top, float right, float bottom) const {
73
return Bounds(x - left, y - top, w + left + right, h + top + bottom);
74
}
75
Bounds Offset(float xAmount, float yAmount) const {
76
return Bounds(x + xAmount, y + yAmount, w, h);
77
}
78
Bounds Inset(float left, float top, float right, float bottom) {
79
return Bounds(x + left, y + top, w - left - right, h - bottom - top);
80
}
81
82
Bounds Inset(float xAmount, float yAmount) const {
83
return Bounds(x + xAmount, y + yAmount, w - xAmount * 2, h - yAmount * 2);
84
}
85
Bounds Inset(float left, float top, float right, float bottom) const {
86
return Bounds(x + left, y + top, w - left - right, h - top - bottom);
87
}
88
89
float x;
90
float y;
91
float w;
92
float h;
93
};
94
95
96
97