X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Flibnr%2Fnr-rect.cpp;h=77af27417517d6b9dadfce6a35b7d600f74a2a10;hb=8b9a820756fdf348239872236be2257f854e094a;hp=3ffe06dc857ed03279abeed5631d9bd81464e63f;hpb=0ad5be4b16a3a627388ca5c7cc5906c42f73c069;p=inkscape.git diff --git a/src/libnr/nr-rect.cpp b/src/libnr/nr-rect.cpp index 3ffe06dc8..77af27417 100644 --- a/src/libnr/nr-rect.cpp +++ b/src/libnr/nr-rect.cpp @@ -10,13 +10,14 @@ */ #include "nr-rect-l.h" +#include NRRect::NRRect(NR::Rect const &rect) : x0(rect.min()[NR::X]), y0(rect.min()[NR::Y]), x1(rect.max()[NR::X]), y1(rect.max()[NR::Y]) {} -NRRect::NRRect(NR::Maybe const &rect) { +NRRect::NRRect(boost::optional const &rect) { if (rect) { x0 = rect->min()[NR::X]; y0 = rect->min()[NR::Y]; @@ -27,9 +28,9 @@ NRRect::NRRect(NR::Maybe const &rect) { } } -NR::Maybe NRRect::upgrade() const { - if (nr_rect_d_test_empty(this)) { - return NR::Nothing(); +boost::optional NRRect::upgrade() const { + if (nr_rect_d_test_empty_ptr(this)) { + return boost::optional(); } else { return NR::Rect(NR::Point(x0, y0), NR::Point(x1, y1)); } @@ -203,7 +204,7 @@ nr_rect_d_matrix_transform(NRRect *d, NRRect const *const s, NR::Matrix const &m using NR::X; using NR::Y; - if (nr_rect_d_test_empty(s)) { + if (nr_rect_d_test_empty_ptr(s)) { nr_rect_d_set_empty(d); } else { NR::Point const c00(NR::Point(s->x0, s->y0) * m); @@ -223,7 +224,7 @@ nr_rect_d_matrix_transform(NRRect *d, NRRect const *const s, NR::Matrix const &m } NRRect * -nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NRMatrix const *m) +nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NR::Matrix const *m) { return nr_rect_d_matrix_transform(d, s, *m); } @@ -242,8 +243,9 @@ nr_rect_l_enlarge(NRRectL *d, int amount) namespace NR { Rect::Rect(const Point &p0, const Point &p1) -: _min(MIN(p0[X], p1[X]), MIN(p0[Y], p1[Y])), - _max(MAX(p0[X], p1[X]), MAX(p0[Y], p1[Y])) {} +: _min(std::min(p0[X], p1[X]), std::min(p0[Y], p1[Y])), + _max(std::max(p0[X], p1[X]), std::max(p0[Y], p1[Y])) +{} /** returns the four corners of the rectangle in the correct winding order */ Point Rect::corner(unsigned i) const { @@ -264,6 +266,18 @@ Point Rect::midpoint() const { return ( _min + _max ) / 2; } +Point Rect::cornerFarthestFrom(Point const &p) const { + Point m = midpoint(); + unsigned i = 0; + if (p[X] < m[X]) { + i = 1; + } + if (p[Y] < m[Y]) { + i = 3 - i; + } + return corner(i); +} + /** returns a vector from topleft to bottom right. */ Point Rect::dimensions() const { return _max - _min; @@ -278,23 +292,33 @@ void Rect::offset(Point p) { /** Makes this rectangle large enough to include the point p. */ void Rect::expandTo(Point p) { for ( int i=0 ; i < 2 ; i++ ) { - _min[i] = MIN(_min[i], p[i]); - _max[i] = MAX(_max[i], p[i]); + _min[i] = std::min(_min[i], p[i]); + _max[i] = std::max(_max[i], p[i]); } } +void Rect::growBy(double size) { + for ( unsigned d = 0 ; d < 2 ; d++ ) { + _min[d] -= size; + _max[d] += size; + if ( _min[d] > _max[d] ) { + _min[d] = _max[d] = ( _min[d] + _max[d] ) / 2; + } + } +} + /** Returns the set of points shared by both rectangles. */ -Maybe intersection(Maybe a, Maybe b) { +boost::optional intersection(boost::optional const & a, boost::optional const & b) { if ( !a || !b ) { - return Nothing(); + return boost::optional(); } else { Rect r; for ( int i=0 ; i < 2 ; i++ ) { - r._min[i] = MAX(a->min()[i], b->min()[i]); - r._max[i] = MIN(a->max()[i], b->max()[i]); + r._min[i] = std::max(a->_min[i], b->_min[i]); + r._max[i] = std::min(a->_max[i], b->_max[i]); if ( r._min[i] > r._max[i] ) { - return Nothing(); - } + return boost::optional(); + } } return r; } @@ -304,8 +328,8 @@ Maybe intersection(Maybe a, Maybe b) { Rect union_bounds(Rect const &a, Rect const &b) { Rect r; for ( int i=0 ; i < 2 ; i++ ) { - r._min[i] = MIN(a._min[i], b._min[i]); - r._max[i] = MAX(a._max[i], b._max[i]); + r._min[i] = std::min(a._min[i], b._min[i]); + r._max[i] = std::max(a._max[i], b._max[i]); } return r; }