23 #ifndef GNASH_RANGE2D_H
24 #define GNASH_RANGE2D_H
75 friend std::ostream& operator<< (std::ostream& os, const Range2d<U>& rect);
151 assert(_xmin <= _xmax);
152 assert(_ymin <= _ymax);
157 template <
typename U>
162 }
else if ( from.
isNull() ) {
165 _xmin = roundMin(from.
getMinX());
166 _ymin = roundMin(from.
getMinY());
167 _xmax = roundMax(from.
getMaxX());
168 _ymax = roundMax(from.
getMaxY());
175 return _xmax < _xmin;
184 _xmin = std::numeric_limits<T>::max();
185 _xmax = std::numeric_limits<T>::min();
192 return _xmax == std::numeric_limits<T>::max()
193 && _xmin == std::numeric_limits<T>::min();
216 _xmin = std::numeric_limits<T>::min();
217 _xmax = std::numeric_limits<T>::max();
228 template <
typename U>
231 if (
isNull() )
return false;
233 if (x < _xmin || x > _xmax || y < _ymin || y > _ymax)
254 if ( other.
isWorld() )
return false;
256 return _xmin <= other._xmin &&
257 _xmax >= other._xmax &&
258 _ymin <= other._ymin &&
259 _ymax >= other._ymax;
274 if ( _xmin > other._xmax )
return false;
275 if ( _xmax < other._xmin )
return false;
276 if ( _ymin > other._ymax )
return false;
277 if ( _ymax < other._ymin )
return false;
296 _xmin = std::min(_xmin, x);
297 _ymin = std::min(_ymin, y);
298 _xmax = std::max(_xmax, x);
299 _ymax = std::max(_ymax, y);
352 assert(_xmin <= _xmax);
353 assert(_ymin <= _ymax);
417 return scale(factor, 1);
423 return scale(1, factor);
461 assert(xfactor >= 0 && yfactor >= 0);
465 if ( xfactor == 0 || yfactor == 0 )
472 _xmin = scaleMin(_xmin, xfactor);
473 _xmax = scaleMax(_xmax, xfactor);
474 assert(_xmin <= _xmax);
479 _ymin = scaleMin(_ymin, yfactor);
480 _ymax = scaleMax(_ymax, yfactor);
481 assert(_ymin <= _ymax);
490 return scale(factor, factor);
513 if ( amount < 0 )
return shrinkBy(-amount);
515 T newxmin = _xmin - amount;
516 if (newxmin > _xmin )
return setWorld();
517 else _xmin = newxmin;
519 T newxmax = _xmax + amount;
520 if (newxmax < _xmax )
return setWorld();
521 else _xmax = newxmax;
523 T newymin = _ymin - amount;
524 if (newymin > _ymin )
return setWorld();
525 else _ymin = newymin;
527 T newymax = _ymax + amount;
528 if (newymax < _ymax )
return setWorld();
529 else _ymax = newymax;
565 if ( amount < 0 )
return growBy(-amount);
572 if ( _xmax - _xmin <= amount )
return setNull();
573 if ( _ymax - _ymin <= amount )
return setNull();
629 return (_xmax - _xmin) * (_ymax - _ymin);
661 _xmin = std::min(_xmin, r._xmin);
662 _xmax = std::max(_xmax, r._xmax);
663 _ymin = std::min(_ymin, r._ymin);
664 _ymax = std::max(_ymax, r._ymax);
670 T _xmin, _xmax, _ymin, _ymax;
672 T scaleMin(
T min,
float scale)
const {
673 return roundMin(static_cast<float>(min) * scale);
676 T scaleMax(
T max,
float scale)
const {
677 return roundMax(static_cast<float>(max) * scale);
680 T roundMin(
float v)
const {
681 return static_cast<T>(
v);
684 T roundMax(
float v)
const {
685 return static_cast<T>(
v);
691 template <
typename T>
inline std::ostream&
692 operator<< (std::ostream& os, const Range2d<T>& rect)
694 if ( rect.isNull() )
return os <<
"Null range";
695 if ( rect.isWorld() )
return os <<
"World range";
697 return os <<
"Finite range (" << rect._xmin <<
"," << rect._ymin
698 <<
" " << rect._xmax <<
"," << rect._ymax <<
")";
701 template <
typename T>
inline bool
713 return r1._xmin == r2._xmin && r1._ymin == r2._ymin &&
714 r1._xmax == r2._xmax && r1._ymax == r2._ymax;
717 template <
typename T>
inline bool
720 return ! ( r1 == r2 );
724 template <
typename T>
inline bool
731 template <
typename T>
inline Range2d<T>
743 template <
typename T>
inline Range2d<T>
767 std::max(r1._xmin, r2._xmin),
768 std::max(r1._ymin, r2._ymin),
769 std::min(r1._xmax, r2._xmax),
770 std::min(r1._ymax, r2._ymax)
779 template<>
inline int
780 Range2d<int>::roundMin(
float min)
const
782 return static_cast<int>(std::floor(min));
789 template<>
inline unsigned int
790 Range2d<unsigned int>::roundMin(
float min)
const
792 return static_cast<unsigned int>(std::floor(min));
799 template<>
inline int
800 Range2d<int>::roundMax(
float max)
const
802 return static_cast<int>(std::ceil(max));
809 template<>
inline unsigned int
810 Range2d<unsigned int>::roundMax(
float max)
const
812 return static_cast<unsigned int>(std::ceil(max));
819 template<>
inline int
822 assert ( !isWorld() );
823 if ( isNull() )
return 0;
824 return (_xmax - _xmin + 1) * (_ymax - _ymin + 1);
831 template<>
inline unsigned int
835 return (_xmax - _xmin + 1) * (_ymax - _ymin + 1);
842 #endif // GNASH_RANGE2D_H