File: Rectangle.h

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (111 lines) | stat: -rw-r--r-- 2,005 bytes parent folder | download | duplicates (3)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef RECTANGLE_H
#define RECTANGLE_H

#include "type2.h"
#include "SpringMath.h"
#include "System/creg/creg_cond.h"

struct SRectangle {
	CR_DECLARE_STRUCT(SRectangle)

	SRectangle()
		: x1(0)
		, z1(0)
		, x2(0)
		, z2(0)
	{}
	SRectangle(int x1_, int z1_, int x2_, int z2_)
		: x1(x1_)
		, z1(z1_)
		, x2(x2_)
		, z2(z2_)
	{}

	int GetWidth() const { return x2 - x1; }
	int GetHeight() const { return z2 - z1; }
	int GetArea() const { return (GetWidth() * GetHeight()); }

	bool Inside(const int2 pos) const {
		// note: *min inclusive, *max exclusive
		const bool xb = (pos.x >= x1 && pos.x < x2);
		const bool yb = (pos.y >= y1 && pos.y < y2);
		return (xb && yb);
	}

	void ClampPos(int2* pos) const {
		pos->x = Clamp(pos->x, x1, x2);
		pos->y = Clamp(pos->y, y1, y2);
	}

	void ClampIn(const SRectangle& rect) {
		x1 = Clamp(x1, rect.x1, rect.x2);
		x2 = Clamp(x2, rect.x1, rect.x2);
		y1 = Clamp(y1, rect.y1, rect.y2);
		y2 = Clamp(y2, rect.y1, rect.y2);
	}

	bool CheckOverlap(const SRectangle& rect) const {
		return
			x1 < rect.x2 && x2 > rect.x1 &&
			y1 < rect.y2 && y2 > rect.y1;
	}

	bool operator< (const SRectangle& other) const {
		if (x1 == other.x1)
			return (z1 < other.z1);

		return (x1 < other.x1);
	}

	template<typename T>
	SRectangle operator* (const T v) const {
		return SRectangle(
			x1 * v, z1 * v,
			x2 * v, z2 * v
		);
	}

	union {
		int x1;
		int left;
	};
	union {
		int z1;
		int y1;
		int top;
	};
	union {
		int x2;
		int right;
	};
	union {
		int z2;
		int y2;
		int bottom;
	};
};


template<typename T> struct TRectangle {
	TRectangle() = default;
	TRectangle(T _x1, T _y1, T _x2, T _y2): x1(_x1), y1(_y1), x2(_x2), y2(_y2) {}

	TRectangle operator + (const TRectangle& o) const {
		TRectangle r;
		r.x1 = x1 + o.x1;
		r.x2 = x1 + o.x2;
		r.y1 = y1 + o.y1;
		r.y2 = y1 + o.y2;
		return r;
	}

	T x1 = T(0);
	T y1 = T(0);
	T x2 = T(0);
	T y2 = T(0);
};

#endif // RECTANGLE_H