Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
rect.h
Go to the documentation of this file.
1/*
5 * Authors:
6 * Michael Sloan <mgsloan@gmail.com>
7 * Krzysztof KosiƄski <tweenk.pl@gmail.com>
8 * Copyright 2007-2011 Authors
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it either under the terms of the GNU Lesser General Public
12 * License version 2.1 as published by the Free Software Foundation
13 * (the "LGPL") or, at your option, under the terms of the Mozilla
14 * Public License Version 1.1 (the "MPL"). If you do not alter this
15 * notice, a recipient may use your version of this file under either
16 * the MPL or the LGPL.
17 *
18 * You should have received a copy of the LGPL along with this library
19 * in the file COPYING-LGPL-2.1; if not, output to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * You should have received a copy of the MPL along with this library
22 * in the file COPYING-MPL-1.1
23 *
24 * The contents of this file are subject to the Mozilla Public License
25 * Version 1.1 (the "License"); you may not use this file except in
26 * compliance with the License. You may obtain a copy of the License at
27 * http://www.mozilla.org/MPL/
28 *
29 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
30 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
31 * the specific language governing rights and limitations.
32 *
33 * Authors of original rect class:
34 * Lauris Kaplinski <lauris@kaplinski.com>
35 * Nathan Hurst <njh@mail.csse.monash.edu.au>
36 * bulia byak <buliabyak@users.sf.net>
37 * MenTaLguY <mental@rydia.net>
38 */
39
40#ifndef LIB2GEOM_SEEN_RECT_H
41#define LIB2GEOM_SEEN_RECT_H
42
43#include <2geom/affine.h>
44#include <2geom/interval.h>
45#include <2geom/int-rect.h>
46
47namespace Geom {
48
63
70
73
85
90class Rect
91 : public GenericRect<Coord>
92{
94public:
97
98 Rect() {}
100 Rect(Interval const &a, Interval const &b) : Base(a,b) {}
102 Rect(Point const &a, Point const &b) : Base(a,b) {}
103 Rect(Coord x0, Coord y0, Coord x1, Coord y1) : Base(x0, y0, x1, y1) {}
104 Rect(Base const &b) : Base(b) {}
105 Rect(IntRect const &ir) : Base(ir.min(), ir.max()) {}
107
110
113 bool hasZeroArea(Coord eps = EPSILON) const { return (area() <= eps); }
115 bool isFinite() const { return (*this)[X].isFinite() && (*this)[Y].isFinite(); }
117 Coord diameter() const { return distance(corner(0), corner(2)); }
119
122
123 bool interiorIntersects(Rect const &r) const {
124 return f[X].interiorIntersects(r[X]) && f[Y].interiorIntersects(r[Y]);
125 }
127 bool interiorContains(Point const &p) const {
128 return f[X].interiorContains(p[X]) && f[Y].interiorContains(p[Y]);
129 }
132 bool interiorContains(Rect const &r) const {
133 return f[X].interiorContains(r[X]) && f[Y].interiorContains(r[Y]);
134 }
135 inline bool interiorContains(OptRect const &r) const;
137
140
143 return ir;
144 }
148 return oir;
149 }
151
154
157 Rect expandedBy(Coord amount) const;
158
162 Rect expandedBy(Coord x, Coord y) const;
163
167 Rect shrunkBy(Coord amount) const;
168
172 Rect shrunkBy(Coord x, Coord y) const;
174
177
182 Affine transformTo(Rect const &viewport, Aspect const &aspect = Aspect()) const;
184
187 Rect &operator*=(Affine const &m);
188 bool operator==(IntRect const &ir) const {
189 return f[X] == ir[X] && f[Y] == ir[Y];
190 }
191 bool operator==(Rect const &other) const {
192 return Base::operator==(other);
193 }
195};
196
202 : public GenericOptRect<Coord>
203{
205public:
206 OptRect() : Base() {}
207 OptRect(Rect const &a) : Base(a) {}
208 OptRect(Point const &a, Point const &b) : Base(a, b) {}
209 OptRect(Coord x0, Coord y0, Coord x1, Coord y1) : Base(x0, y0, x1, y1) {}
210 OptRect(OptInterval const &x_int, OptInterval const &y_int) : Base(x_int, y_int) {}
211 OptRect(Base const &b) : Base(b) {}
212
213 OptRect(IntRect const &r) : Base(Rect(r)) {}
214 OptRect(OptIntRect const &r) : Base() {
215 if (r) *this = Rect(*r);
216 }
217
218 Affine transformTo(Rect const &viewport, Aspect const &aspect = Aspect()) {
219 Affine ret = Affine::identity();
220 if (empty()) return ret;
221 ret = (*this)->transformTo(viewport, aspect);
222 return ret;
223 }
224
225 bool operator==(OptRect const &other) const {
226 return Base::operator==(other);
227 }
228 bool operator==(Rect const &other) const {
229 return Base::operator==(other);
230 }
231};
232
233Coord distanceSq(Point const &p, Rect const &rect);
234Coord distance(Point const &p, Rect const &rect);
236Coord distanceSq(Point const &p, OptRect const &rect);
238Coord distance(Point const &p, OptRect const &rect);
239
240inline bool Rect::interiorContains(OptRect const &r) const {
241 return !r || interiorContains(static_cast<Rect const &>(*r));
242}
243
244// the functions below do not work when defined generically
245inline OptRect operator&(Rect const &a, Rect const &b) {
246 OptRect ret(a);
247 ret.intersectWith(b);
248 return ret;
249}
250inline OptRect intersect(Rect const &a, Rect const &b) {
251 return a & b;
252}
253inline OptRect intersect(OptRect const &a, OptRect const &b) {
254 return a & b;
255}
256inline Rect unify(Rect const &a, Rect const &b) {
257 return a | b;
258}
259inline OptRect unify(OptRect const &a, OptRect const &b) {
260 return a | b;
261}
262
265inline Rect union_list(std::vector<Rect> const &r) {
266 if(r.empty()) return Rect(Interval(0,0), Interval(0,0));
267 Rect ret = r[0];
268 for(unsigned i = 1; i < r.size(); i++)
269 ret.unionWith(r[i]);
270 return ret;
271}
272
273} // end namespace Geom
274
275#endif // LIB2GEOM_SEEN_RECT_H
276
277/*
278 Local Variables:
279 mode:c++
280 c-file-style:"stroustrup"
281 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
282 indent-tabs-mode:nil
283 fill-column:99
284 End:
285*/
286// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
pair< double, double > Point
Definition parser.cpp:7
3x3 affine transformation matrix.
3x3 matrix representing an affine transformation.
Definition affine.h:70
static Affine identity()
Axis-aligned generic rectangle that can be empty.
bool operator==(OptCRect const &other) const
Test for equality.
bool empty() const
Check for emptiness.
void intersectWith(CRect const &b)
Leave only the area overlapping with the argument.
Axis aligned, non-empty, generic rectangle.
Coord area() const
Compute the rectangle's area.
void unionWith(CRect const &b)
Enlarge the rectangle to contain the argument.
CPoint min() const
Get the corner of the rectangle with smallest coordinate values.
CPoint corner(unsigned i) const
Return the n-th corner of the rectangle.
CPoint max() const
Get the corner of the rectangle with largest coordinate values.
bool operator==(CRect const &o) const
Test for equality of rectangles.
Range of real numbers that is never empty.
Definition interval.h:59
Range of real numbers that can be empty.
Definition interval.h:199
Axis-aligned rectangle that can be empty.
Definition rect.h:203
OptRect(OptIntRect const &r)
Definition rect.h:214
GenericOptRect< Coord > Base
Definition rect.h:204
OptRect(Point const &a, Point const &b)
Definition rect.h:208
Affine transformTo(Rect const &viewport, Aspect const &aspect=Aspect())
Definition rect.h:218
OptRect(Coord x0, Coord y0, Coord x1, Coord y1)
Definition rect.h:209
OptRect(Rect const &a)
Definition rect.h:207
bool operator==(Rect const &other) const
Definition rect.h:228
OptRect(Base const &b)
Definition rect.h:211
OptRect(IntRect const &r)
Definition rect.h:213
OptRect(OptInterval const &x_int, OptInterval const &y_int)
Definition rect.h:210
bool operator==(OptRect const &other) const
Definition rect.h:225
Two-dimensional point that doubles as a vector.
Definition point.h:66
Axis aligned, non-empty rectangle.
Definition rect.h:92
Rect shrunkBy(Coord amount) const
Return a new rectangle which results from shrinking this one by the same amount along both axes.
Definition rect.cpp:144
bool isFinite() const
Check whether the rectangle has finite area.
Definition rect.h:115
OptIntRect roundInwards() const
Return the largest integer rectangle which is contained in this one.
Definition rect.h:146
Rect expandedBy(Coord amount) const
Return a new rectangle which results from expanding this one by the same amount along both axes.
Definition rect.cpp:130
bool interiorContains(Rect const &r) const
Check whether the interior includes all points in the given rectangle.
Definition rect.h:132
Rect(Base const &b)
Definition rect.h:104
IntRect roundOutwards() const
Return the smallest integer rectangle which contains this one.
Definition rect.h:141
bool operator==(IntRect const &ir) const
Definition rect.h:188
Rect(Point const &a, Point const &b)
Create a rectangle from two points.
Definition rect.h:102
bool operator==(Rect const &other) const
Definition rect.h:191
Rect & operator*=(Affine const &m)
Transform the rectangle by an affine.
Definition rect.cpp:87
Affine transformTo(Rect const &viewport, Aspect const &aspect=Aspect()) const
Transform contents to viewport.
Definition rect.cpp:99
GenericRect< Coord > Base
Definition rect.h:93
Coord diameter() const
Calculate the diameter of the smallest circle that would contain the rectangle.
Definition rect.h:117
bool interiorContains(Point const &p) const
Check whether the interior includes the given point.
Definition rect.h:127
bool hasZeroArea(Coord eps=EPSILON) const
Check whether the rectangle has zero area up to specified tolerance.
Definition rect.h:113
Rect(IntRect const &ir)
Definition rect.h:105
Coord distance(Point const &p, Rect const &rect)
Returns the smallest distance between p and rect.
Definition rect.cpp:176
Rect(Coord x0, Coord y0, Coord x1, Coord y1)
Definition rect.h:103
Rect()
Create a rectangle that contains only the point at (0,0).
Definition rect.h:98
bool interiorIntersects(Rect const &r) const
Check whether the interiors of the rectangles have any common points.
Definition rect.h:123
Rect(Interval const &a, Interval const &b)
Create a rectangle from X and Y intervals.
Definition rect.h:100
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
constexpr Coord EPSILON
Default "acceptably small" value.
Definition coord.h:84
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
Axis-aligned rectangle with integer coordinates.
Simple closed interval class.
Various utility functions.
Definition affine.h:22
Expansion
Values for the <meetOrSlice> parameter of preserveAspectRatio.
Definition rect.h:66
@ EXPANSION_SLICE
Definition rect.h:68
@ EXPANSION_MEET
Definition rect.h:67
Coord distanceSq(Point const &p, Rect const &rect)
Definition rect.cpp:158
Angle distance(Angle const &a, Angle const &b)
Definition angle.h:163
Align
Values for the <align> parameter of preserveAspectRatio.
Definition rect.h:51
@ ALIGN_XMID_YMAX
Definition rect.h:60
@ ALIGN_XMAX_YMAX
Definition rect.h:61
@ ALIGN_XMIN_YMID
Definition rect.h:56
@ ALIGN_XMID_YMID
Definition rect.h:57
@ ALIGN_XMID_YMIN
Definition rect.h:54
@ ALIGN_XMAX_YMID
Definition rect.h:58
@ ALIGN_XMIN_YMIN
Definition rect.h:53
@ ALIGN_XMIN_YMAX
Definition rect.h:59
@ ALIGN_NONE
Definition rect.h:52
@ ALIGN_XMAX_YMIN
Definition rect.h:55
IntRect unify(IntRect const &a, IntRect const &b)
Definition int-rect.h:55
OptIntRect operator&(IntRect const &a, IntRect const &b)
Definition int-rect.h:44
Rect union_list(std::vector< Rect > const &r)
Union a list of rectangles.
Definition rect.h:265
std::vector< Point > intersect(const xAx &C1, const xAx &C2)
Definition conicsec.cpp:361
Point align_factors(Align align)
Convert an align specification to coordinate fractions.
Definition rect.cpp:37
Structure that specifies placement of within a viewport.
Definition rect.h:76
Align align
Definition rect.h:77
Aspect(Align a=ALIGN_NONE, Expansion ex=EXPANSION_MEET)
Definition rect.h:81
bool deferred
for SVG compatibility
Definition rect.h:79
Expansion expansion
Definition rect.h:78