]> Dogcows Code - chaz/yoink/blob - src/rectangle.hh
now using cml for vectors and math stuff
[chaz/yoink] / src / rectangle.hh
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #ifndef _RECTANGLE_HH_
30 #define _RECTANGLE_HH_
31
32 #include "math.hh"
33 #include "vector.hh"
34
35
36 namespace dc {
37
38 //
39 // A rectangle is a 2d shape defined by point (origin) and size (width & height)
40 // vectors. The usual rectangle maths are available. Valid rectangles have
41 // positive width and height. Invalid rectangles will be returned for invalid
42 // operations such as trying to get the intersection of two rectangles which
43 // don't intersect. Using an invalid rectangle is undefined, but it may be used
44 // after "fixing" it using the fix method which will cause the size to be
45 // positive and adjust the origin to be the bottom-left point. Check the
46 // validity of a rectangle using the isValid method especially if the rectangle
47 // is a result of an operation which could fail.
48 //
49
50 class rectangle
51 {
52 public:
53 // Constructors.
54 rectangle() {}
55 rectangle(point o, point s) {
56 origin = o;
57 size = s;
58 }
59
60 // Rectangle validity.
61 bool isValid() const { return size.x >= 0.0 && size.y >= 0.0; }
62 void fix()
63 {
64 if (size.x <= 0.0)
65 {
66 size.x = 0.0 - size.x;
67 origin.x -= size.x;
68 }
69 if (size.y <= 0.0)
70 {
71 size.y = 0.0 - size.y;
72 origin.y -= size.y;
73 }
74 }
75
76 // Rectangle operations.
77 scalar area() const { return size.x * size.y; }
78 scalar perimeter() const {
79 return size.x + size.x + size.y + size.y;
80 }
81
82 bool containsPoint(point pt) const
83 {
84 return origin.x <= pt.x && origin.y <= pt.y &&
85 pt.x <= origin.x + size.x && pt.y <= origin.y + size.y;
86 }
87
88 void inset(scalar h, scalar v)
89 {
90 origin.x += h;
91 origin.y += v;
92 size.x -= h + h;
93 size.y -= v + v;
94 }
95 void offset(scalar h, scalar v)
96 {
97 origin.x -= h;
98 origin.y -= v;
99 size.x += h + h;
100 size.y += v + v;
101 }
102
103 bool containsRect(const rectangle& r) const;
104 bool intersectsRect(const rectangle& r) const;
105 rectangle unionWith(const rectangle& r) const;
106 rectangle intersectionWith(const rectangle& r) const;
107
108 // Checking equality.
109 bool operator == (const rectangle& r) const {
110 return origin == r.origin && size == r.size;
111 }
112 bool operator != (const rectangle& r) const {
113 return !(*this == r);
114 }
115
116 // Data.
117 point origin, size;
118 };
119
120 } // namespace dc
121
122
123 #endif // _RECTANGLE_HH_
124
This page took 0.037332 seconds and 4 git commands to generate.