~ubuntu-branches/debian/experimental/inkscape/experimental

« back to all changes in this revision

Viewing changes to src/libnr/nr-rect.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-09-09 23:29:02 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20080909232902-c50iujhk1w79u8e7
Tags: 0.46-2.1
* Non-maintainer upload.
* Add upstream patch fixing a crash in the open dialog
  in the zh_CN.utf8 locale. Closes: #487623.
  Thanks to Luca Bruno for the patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 */
11
11
 
12
12
#include "nr-rect-l.h"
 
13
#include <algorithm>
 
14
 
 
15
NRRect::NRRect(NR::Rect const &rect)
 
16
: x0(rect.min()[NR::X]), y0(rect.min()[NR::Y]),
 
17
  x1(rect.max()[NR::X]), y1(rect.max()[NR::Y])
 
18
{}
 
19
 
 
20
NRRect::NRRect(NR::Maybe<NR::Rect> const &rect) {
 
21
    if (rect) {
 
22
        x0 = rect->min()[NR::X];
 
23
        y0 = rect->min()[NR::Y];
 
24
        x1 = rect->max()[NR::X];
 
25
        y1 = rect->max()[NR::Y];
 
26
    } else {
 
27
        nr_rect_d_set_empty(this);
 
28
    }
 
29
}
 
30
 
 
31
NR::Maybe<NR::Rect> NRRect::upgrade() const {
 
32
    if (nr_rect_d_test_empty(this)) {
 
33
        return NR::Nothing();
 
34
    } else {
 
35
        return NR::Rect(NR::Point(x0, y0), NR::Point(x1, y1));
 
36
    }
 
37
}
13
38
 
14
39
/**
15
40
 *    \param r0 Rectangle.
45
70
        return d;
46
71
}
47
72
 
 
73
// returns minimal rect which covers all of r0 not covered by r1
 
74
NRRectL *
 
75
nr_rect_l_subtract(NRRectL *d, NRRectL const *r0, NRRectL const *r1)
 
76
{
 
77
    bool inside1 = nr_rect_l_test_inside(r1, r0->x0, r0->y0);
 
78
    bool inside2 = nr_rect_l_test_inside(r1, r0->x1, r0->y0);
 
79
    bool inside3 = nr_rect_l_test_inside(r1, r0->x1, r0->y1);
 
80
    bool inside4 = nr_rect_l_test_inside(r1, r0->x0, r0->y1);
 
81
 
 
82
    if (inside1 && inside2 && inside3) {
 
83
        nr_rect_l_set_empty (d);
 
84
 
 
85
    } else if (inside1 && inside2) {
 
86
        d->x0 = r0->x0;
 
87
        d->y0 = r1->y1;
 
88
 
 
89
        d->x1 = r0->x1;
 
90
        d->y1 = r0->y1;
 
91
    } else if (inside2 && inside3) {
 
92
        d->x0 = r0->x0;
 
93
        d->y0 = r0->y0;
 
94
 
 
95
        d->x1 = r1->x0;
 
96
        d->y1 = r0->y1;
 
97
    } else if (inside3 && inside4) {
 
98
        d->x0 = r0->x0;
 
99
        d->y0 = r0->y0;
 
100
 
 
101
        d->x1 = r0->x1;
 
102
        d->y1 = r1->y0;
 
103
    } else if (inside4 && inside1) {
 
104
        d->x0 = r1->x1;
 
105
        d->y0 = r0->y0;
 
106
 
 
107
        d->x1 = r0->x1;
 
108
        d->y1 = r0->y1;
 
109
    } else {
 
110
        d->x0 = r0->x0;
 
111
        d->y0 = r0->y0;
 
112
 
 
113
        d->x1 = r0->x1;
 
114
        d->y1 = r0->y1;
 
115
    }
 
116
    return d;
 
117
}
 
118
 
 
119
NR::ICoord nr_rect_l_area(NRRectL *r)
 
120
{
 
121
  if (!r || NR_RECT_DFLS_TEST_EMPTY (r)) {
 
122
      return 0;
 
123
  }
 
124
  return ((r->x1 - r->x0) * (r->y1 - r->y0));
 
125
}
 
126
 
48
127
NRRect *
49
128
nr_rect_d_union (NRRect *d, const NRRect *r0, const NRRect *r1)
50
129
{
150
229
    return nr_rect_d_matrix_transform(d, s, *m);
151
230
}
152
231
 
 
232
/** Enlarges the rectangle given amount of pixels to all directions */
 
233
NRRectL *
 
234
nr_rect_l_enlarge(NRRectL *d, int amount)
 
235
{
 
236
    d->x0 -= amount;
 
237
    d->y0 -= amount;
 
238
    d->x1 += amount;
 
239
    d->y1 += amount;
 
240
    return d;
 
241
}
 
242
 
153
243
namespace NR {
154
244
 
155
245
Rect::Rect(const Point &p0, const Point &p1)
156
 
: _min(MIN(p0[X], p1[X]), MIN(p0[Y], p1[Y])),
157
 
  _max(MAX(p0[X], p1[X]), MAX(p0[Y], p1[Y])) {}
 
246
: _min(std::min(p0[X], p1[X]), std::min(p0[Y], p1[Y])),
 
247
  _max(std::max(p0[X], p1[X]), std::max(p0[Y], p1[Y]))
 
248
{}
158
249
 
159
250
/** returns the four corners of the rectangle in the correct winding order */
160
251
Point Rect::corner(unsigned i) const {
175
266
        return ( _min + _max ) / 2;
176
267
}
177
268
 
 
269
Point Rect::cornerFarthestFrom(Point const &p) const {
 
270
    Point m = midpoint();
 
271
    unsigned i = 0;
 
272
    if (p[X] < m[X]) {
 
273
        i = 1;
 
274
    }
 
275
    if (p[Y] < m[Y]) {
 
276
        i = 3 - i;
 
277
    }
 
278
    return corner(i);
 
279
}
 
280
 
178
281
/** returns a vector from topleft to bottom right. */
179
282
Point Rect::dimensions() const {
180
283
        return _max - _min;
189
292
/** Makes this rectangle large enough to include the point p. */
190
293
void Rect::expandTo(Point p) {
191
294
        for ( int i=0 ; i < 2 ; i++ ) {
192
 
                _min[i] = MIN(_min[i], p[i]);
193
 
                _max[i] = MAX(_max[i], p[i]);
 
295
                _min[i] = std::min(_min[i], p[i]);
 
296
                _max[i] = std::max(_max[i], p[i]);
194
297
        }
195
298
}
196
299
 
 
300
void Rect::growBy(double size) {
 
301
  for ( unsigned d = 0 ; d < 2 ; d++ ) {
 
302
    _min[d] -= size;
 
303
    _max[d] += size;
 
304
    if ( _min[d] > _max[d] ) {
 
305
      _min[d] = _max[d] = ( _min[d] + _max[d] ) / 2;
 
306
    }
 
307
  }
 
308
 
309
 
197
310
/** Returns the set of points shared by both rectangles. */
198
 
Maybe<Rect> Rect::intersection(const Rect &a, const Rect &b) {
199
 
        Rect r;
200
 
        for ( int i=0 ; i < 2 ; i++ ) {
201
 
                r._min[i] = MAX(a._min[i], b._min[i]);
202
 
                r._max[i] = MIN(a._max[i], b._max[i]);
203
 
 
204
 
                if ( r._min[i] > r._max[i] ) {
205
 
                        return Nothing();
206
 
                }
 
311
Maybe<Rect> intersection(Maybe<Rect> const & a, Maybe<Rect> const & b) {
 
312
    if ( !a || !b ) {
 
313
        return Nothing();
 
314
    } else {
 
315
        Rect r;
 
316
        for ( int i=0 ; i < 2 ; i++ ) {
 
317
            r._min[i] = std::max(a->_min[i], b->_min[i]);
 
318
            r._max[i] = std::min(a->_max[i], b->_max[i]);
 
319
            if ( r._min[i] > r._max[i] ) {
 
320
                return Nothing();
 
321
            }
207
322
        }
208
323
        return r;
 
324
    }
209
325
}
210
326
 
211
327
/** returns the smallest rectangle containing both rectangles */
212
 
Rect Rect::union_bounds(const Rect &a, const Rect &b) {
213
 
        Rect r;
214
 
        for ( int i=0; i < 2 ; i++ ) {
215
 
                r._min[i] = MIN(a._min[i], b._min[i]);
216
 
                r._max[i] = MAX(a._max[i], b._max[i]);
217
 
        }
218
 
        return r;
 
328
Rect union_bounds(Rect const &a, Rect const &b) {
 
329
    Rect r;
 
330
    for ( int i=0 ; i < 2 ; i++ ) {
 
331
        r._min[i] = std::min(a._min[i], b._min[i]);
 
332
        r._max[i] = std::max(a._max[i], b._max[i]);
 
333
    }
 
334
    return r;
219
335
}
220
336
 
221
337
}  // namespace NR