File: Rect.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (114 lines) | stat: -rw-r--r-- 2,443 bytes parent folder | download | duplicates (2)
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
112
113
114
#include "stdafx.h"
#include "Rect.h"
#include "Core/Str.h"
#include "Core/StrBuf.h"

namespace storm {
	namespace geometry {

		Rect::Rect() : p0(), p1() {}

		Rect::Rect(Size s) : p0(), p1(s) {}

		Rect::Rect(Point p, Size s) : p0(p), p1(p + s) {}

		Rect::Rect(Point topLeft, Point bottomRight) : p0(topLeft), p1(bottomRight) {}

		Rect::Rect(Float left, Float top, Float right, Float bottom) : p0(left, top), p1(right, bottom) {}

		Rect Rect::normalized() const {
			return Rect(
				Point(min(p0.x, p1.x), min(p0.y, p1.y)),
				Point(max(p0.x, p1.x), max(p0.y, p1.y))
				);
		}

		Bool Rect::contains(Point p) const {
			return (p0.x <= p.x) && (p.x < p1.x)
				&& (p0.y <= p.y) && (p.y < p1.y);
		}

		Bool Rect::contains(Rect r) const {
			return (p0.x <= r.p0.x) && (r.p1.x < p1.x)
				&& (p0.y <= r.p0.y) && (r.p1.y < p1.y);
		}

		Bool inside(Point pt, Rect r) {
			return r.contains(pt);
		}

		Bool Rect::intersects(Rect other) const {
			return (p0.x < other.p1.x) && (other.p0.x < p1.x)
				&& (p0.y < other.p1.y) && (other.p0.y < p1.y);
		}

		Rect Rect::operator +(Point pt) const {
			return Rect(p0 + pt, p1 + pt);
		}

		Rect Rect::operator -(Point pt) const {
			return Rect(p0 - pt, p1 - pt);
		}

		Rect &Rect::operator +=(Point pt) {
			p0 += pt;
			p1 += pt;
			return *this;
		}

		Rect &Rect::operator -=(Point pt) {
			p0 -= pt;
			p1 -= pt;
			return *this;
		}

		Rect Rect::include(Point to) const {
			Float l = min(p0.x, to.x);
			Float t = min(p0.y, to.y);
			Float r = max(p1.x, to.x);
			Float b = max(p1.y, to.y);
			return Rect(l, t, r, b);
		}

		Rect Rect::include(Rect other) const {
			return include(other.p0).include(other.p1);
		}

		Rect Rect::scaled(Float scale) const {
			Size s = size() / 2;
			Float dx = s.w * scale - s.w;
			Float dy = s.h * scale - s.h;

			Float l = p0.x - dx;
			Float t = p0.y - dy;
			Float r = p1.x + dx;
			Float b = p1.y + dy;
			return Rect(l, t, r, b);
		}

		Rect Rect::shrink(Size s) const {
			Float l = p0.x + s.w;
			Float t = p0.y + s.h;
			Float r = p1.x - s.w;
			Float b = p1.y - s.h;
			return Rect(l, t, r, b);
		}

		Rect Rect::grow(Size s) const {
			Float l = p0.x - s.w;
			Float t = p0.y - s.h;
			Float r = p1.x + s.w;
			Float b = p1.y + s.h;
			return Rect(l, t, r, b);
		}

		wostream &operator <<(wostream &to, Rect r) {
			return to << r.p0 << L"-" << r.p1;
		}

		void Rect::toS(StrBuf *to) const {
			*to << p0 << S("-") << p1;
		}

	}
}