]>
Dogcows Code - chaz/yoink/blob - src/rectangle.hh
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
29 #ifndef _RECTANGLE_HH_
30 #define _RECTANGLE_HH_
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.
55 rectangle(point o
, point s
) {
60 // Rectangle validity.
61 bool isValid() const { return size
.x
>= 0.0 && size
.y
>= 0.0; }
66 size
.x
= 0.0 - size
.x
;
71 size
.y
= 0.0 - size
.y
;
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
;
82 bool containsPoint(point pt
) const
84 return origin
.x
<= pt
.x
&& origin
.y
<= pt
.y
&&
85 pt
.x
<= origin
.x
+ size
.x
&& pt
.y
<= origin
.y
+ size
.y
;
88 void inset(scalar h
, scalar v
)
95 void offset(scalar h
, scalar v
)
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;
108 // Checking equality.
109 bool operator == (const rectangle
& r
) const {
110 return origin
== r
.origin
&& size
== r
.size
;
112 bool operator != (const rectangle
& r
) const {
113 return !(*this == r
);
123 #endif // _RECTANGLE_HH_
This page took 0.041442 seconds and 5 git commands to generate.