-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeometry.h
82 lines (69 loc) · 1.57 KB
/
Geometry.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// Geometry.h
// SCMS
//
// Created by Zach Zahos on 2016-12-11.
//
//
#pragma once
#include "Types.h"
#include <algorithm>
template <typename S>
struct Size;
template <typename S>
struct Point {
S x, y;
inline Point<S> operator+(const Point<S> &other) {
return {x + other.x, y + other.y};
}
inline Point<S> operator-(const Point<S> &other) {
return {x - other.x, y - other.y};
}
};
template <typename S>
struct Size {
S width, height;
u32 area()
{ return width * height; }
};
template <typename S>
struct Extents;
template <typename P,typename S>
struct Rect {
Point<P> origin;
Size<S> size;
inline Extents<P> extents() {
return {origin, bottomRight()};
}
inline Point<P> bottomRight() {
return {origin.x + (P)size.width, origin.y + (P)size.height};
}
inline bool isEmpty() {
return (size.width == 0 || size.height == 0);
}
Rect<P,S> overlap(Rect<P,S> other) {
Extents<P> extents = {
{ std::max(origin.x, other.origin.x), std::max(origin.y, other.origin.y) },
{ (P)std::min(origin.x + size.width, other.origin.x + other.size.width), (P)std::min(origin.y + size.height, other.origin.y + other.size.height) }
};
if (!extents.isEmpty()) {
return {extents.start, {(S)(extents.end.x - extents.start.x), (S)(extents.end.y - extents.start.y)}};
} else {
return {{0,0},{0,0}};
}
}
};
template <typename S>
struct Extents {
Point<S> start;
Point<S> end;
inline Rect<S, S> rect() {
return {start, size()};
}
inline Size<S> size() {
return end - start;
}
inline bool isEmpty() {
return start.x >= end.x || start.y >= end.y;
}
};