Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
rect.cpp
Go to the documentation of this file.
1/* Axis-aligned rectangle
2 *
3 * Authors:
4 * Michael Sloan <mgsloan@gmail.com>
5 * Krzysztof KosiƄski <tweenk.pl@gmail.com>
6 * Copyright 2007-2011 Authors
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it either under the terms of the GNU Lesser General Public
10 * License version 2.1 as published by the Free Software Foundation
11 * (the "LGPL") or, at your option, under the terms of the Mozilla
12 * Public License Version 1.1 (the "MPL"). If you do not alter this
13 * notice, a recipient may use your version of this file under either
14 * the MPL or the LGPL.
15 *
16 * You should have received a copy of the LGPL along with this library
17 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * You should have received a copy of the MPL along with this library
20 * in the file COPYING-MPL-1.1
21 *
22 * The contents of this file are subject to the Mozilla Public License
23 * Version 1.1 (the "License"); you may not use this file except in
24 * compliance with the License. You may obtain a copy of the License at
25 * http://www.mozilla.org/MPL/
26 *
27 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
28 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
29 * the specific language governing rights and limitations.
30 */
31
32#include <2geom/rect.h>
33#include <2geom/transforms.h>
34
35namespace Geom {
36
38 Point p;
39 switch (g) {
40 case ALIGN_XMIN_YMIN:
41 p[X] = 0.0;
42 p[Y] = 0.0;
43 break;
44 case ALIGN_XMID_YMIN:
45 p[X] = 0.5;
46 p[Y] = 0.0;
47 break;
48 case ALIGN_XMAX_YMIN:
49 p[X] = 1.0;
50 p[Y] = 0.0;
51 break;
52 case ALIGN_XMIN_YMID:
53 p[X] = 0.0;
54 p[Y] = 0.5;
55 break;
56 case ALIGN_XMID_YMID:
57 p[X] = 0.5;
58 p[Y] = 0.5;
59 break;
60 case ALIGN_XMAX_YMID:
61 p[X] = 1.0;
62 p[Y] = 0.5;
63 break;
64 case ALIGN_XMIN_YMAX:
65 p[X] = 0.0;
66 p[Y] = 1.0;
67 break;
68 case ALIGN_XMID_YMAX:
69 p[X] = 0.5;
70 p[Y] = 1.0;
71 break;
72 case ALIGN_XMAX_YMAX:
73 p[X] = 1.0;
74 p[Y] = 1.0;
75 break;
76 default:
77 break;
78 }
79 return p;
80}
81
82
88 Point pts[4];
89 for (unsigned i=0; i<4; ++i) pts[i] = corner(i) * m;
90 Coord minx = std::min(std::min(pts[0][X], pts[1][X]), std::min(pts[2][X], pts[3][X]));
91 Coord miny = std::min(std::min(pts[0][Y], pts[1][Y]), std::min(pts[2][Y], pts[3][Y]));
92 Coord maxx = std::max(std::max(pts[0][X], pts[1][X]), std::max(pts[2][X], pts[3][X]));
93 Coord maxy = std::max(std::max(pts[0][Y], pts[1][Y]), std::max(pts[2][Y], pts[3][Y]));
94 f[X].setMin(minx); f[X].setMax(maxx);
95 f[Y].setMin(miny); f[Y].setMax(maxy);
96 return *this;
97}
98
99Affine Rect::transformTo(Rect const &viewport, Aspect const &aspect) const
100{
101 // 1. translate viewbox to origin
102 Geom::Affine total = Translate(-min());
103
104 // 2. compute scale
105 Geom::Point vdims = viewport.dimensions();
106 Geom::Point dims = dimensions();
107 Geom::Scale scale(vdims[X] / dims[X], vdims[Y] / dims[Y]);
108
109 if (aspect.align == ALIGN_NONE) {
110 // apply non-uniform scale
111 total *= scale * Translate(viewport.min());
112 } else {
113 double uscale = 0;
114 if (aspect.expansion == EXPANSION_MEET) {
115 uscale = std::min(scale[X], scale[Y]);
116 } else {
117 uscale = std::max(scale[X], scale[Y]);
118 }
119 scale = Scale(uscale);
120
121 // compute offset for align
122 Geom::Point offset = vdims - dims * scale;
123 offset *= Scale(align_factors(aspect.align));
124 total *= scale * Translate(viewport.min() + offset);
125 }
126
127 return total;
128}
129
131{
132 auto copy{*this};
133 copy.expandBy(amount);
134 return copy;
135}
136
138{
139 auto copy{*this};
140 copy.expandBy(x, y);
141 return copy;
142}
143
145{
146 auto copy{*this};
147 copy.shrinkBy(amount);
148 return copy;
149}
150
152{
153 auto copy{*this};
154 copy.shrinkBy(x, y);
155 return copy;
156}
157
158Coord distanceSq(Point const &p, Rect const &rect)
159{
160 double dx = 0, dy = 0;
161 if ( p[X] < rect.left() ) {
162 dx = p[X] - rect.left();
163 } else if ( p[X] > rect.right() ) {
164 dx = rect.right() - p[X];
165 }
166 if (p[Y] < rect.top() ) {
167 dy = rect.top() - p[Y];
168 } else if ( p[Y] > rect.bottom() ) {
169 dy = p[Y] - rect.bottom();
170 }
171 return dx*dx+dy*dy;
172}
173
176Coord distance(Point const &p, Rect const &rect)
177{
178 // copy of distanceSq, because we need to use hypot()
179 double dx = 0, dy = 0;
180 if ( p[X] < rect.left() ) {
181 dx = p[X] - rect.left();
182 } else if ( p[X] > rect.right() ) {
183 dx = rect.right() - p[X];
184 }
185 if (p[Y] < rect.top() ) {
186 dy = rect.top() - p[Y];
187 } else if ( p[Y] > rect.bottom() ) {
188 dy = p[Y] - rect.bottom();
189 }
190 return hypot(dx, dy);
191}
192
193Coord distanceSq(Point const &p, OptRect const &rect)
194{
195 if (!rect) return std::numeric_limits<Coord>::max();
196 return distanceSq(p, *rect);
197}
198Coord distance(Point const &p, OptRect const &rect)
199{
200 if (!rect) return std::numeric_limits<Coord>::max();
201 return distance(p, *rect);
202}
203
204} // namespace Geom
205
206/*
207 Local Variables:
208 mode:c++
209 c-file-style:"stroustrup"
210 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
211 indent-tabs-mode:nil
212 fill-column:99
213 End:
214*/
215// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
double scale
Definition aa.cpp:228
3x3 matrix representing an affine transformation.
Definition affine.h:70
C right() const
Return rightmost coordinate of the rectangle (+X is to the right).
C top() const
Return top coordinate of the rectangle (+Y is downwards).
C left() const
Return leftmost coordinate of the rectangle (+X is to the right).
CPoint min() const
Get the corner of the rectangle with smallest coordinate values.
C bottom() const
Return bottom coordinate of the rectangle (+Y is downwards).
CPoint dimensions() const
Get rectangle's width and height as a point.
CPoint corner(unsigned i) const
Return the n-th corner of the rectangle.
Axis-aligned rectangle that can be empty.
Definition rect.h:203
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
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
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
Coord distance(Point const &p, Rect const &rect)
Returns the smallest distance between p and rect.
Definition rect.cpp:176
Scaling from the origin.
Definition transforms.h:150
Translation by a vector.
Definition transforms.h:115
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
double offset
Various utility functions.
Definition affine.h:22
@ 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
Point align_factors(Align align)
Convert an align specification to coordinate fractions.
Definition rect.cpp:37
Axis-aligned rectangle.
Structure that specifies placement of within a viewport.
Definition rect.h:76
Align align
Definition rect.h:77
Expansion expansion
Definition rect.h:78
Affine transformation classes.