40#ifndef LIB2GEOM_SEEN_GENERIC_RECT_H
41#define LIB2GEOM_SEEN_GENERIC_RECT_H
81 GenericRect(C x0, C y0, C x1, C y1) : f{{x0, x1}, {y0, y1}} {}
89 template <
typename InputIterator>
109 return CRect(xy, xy + wh);
113 auto min = std::numeric_limits<C>::min();
114 auto max = std::numeric_limits<C>::max();
147 C
top()
const {
return f[
Y].min(); }
151 C
left()
const {
return f[
X].min(); }
156 C
width()
const {
return f[
X].extent(); }
170 C
area()
const {
return f[
X].extent() * f[
Y].extent(); }
175 C
maxExtent()
const {
return std::max(f[
X].extent(), f[
Y].extent()); }
177 C
minExtent()
const {
return std::min(f[
X].extent(), f[
Y].extent()); }
181 return (p - clamp(p)).lengthSq();
186 return CPoint(f[
X].clamp(p[
X]), f[
Y].clamp(p[
Y]));
194 C cx = f[
X].nearestEnd(p[
X]);
195 C cy = f[
Y].nearestEnd(p[
Y]);
196 if (std::abs(cx - p[
X]) <= std::abs(cy - p[
Y])) {
209 return f[
X].intersects(r[
X]) && f[
Y].intersects(r[
Y]);
213 return f[
X].contains(r[
X]) && f[
Y].contains(r[
Y]);
225 return f[
X].contains(p[
X]) && f[
Y].contains(p[
Y]);
264 f[
X].unionWith(b[
X]);
265 f[
Y].unionWith(b[
Y]);
277 expandBy(amount, amount);
309 expandBy(p[
X], p[
Y]);
347 :
public std::optional<typename CoordTraits<C>::RectType>
348 , boost::equality_comparable< typename CoordTraits<C>::OptRectType
349 , boost::equality_comparable< typename CoordTraits<C>::OptRectType, typename CoordTraits<C>::RectType
350 , boost::orable< typename CoordTraits<C>::OptRectType
351 , boost::andable< typename CoordTraits<C>::OptRectType
352 , boost::andable< typename CoordTraits<C>::OptRectType, typename CoordTraits<C>::RectType
360 using Base = std::optional<CRect>;
374 if (x_int && y_int) {
375 *
this =
CRect(*x_int, *y_int);
388 template <
typename InputIterator>
401 inline bool empty()
const {
return !*
this; };
407 bool contains(
CRect const &r)
const {
return *
this && (*this)->contains(r); }
424 C
area()
const {
return *
this ? (*this)->area() : 0; }
426 bool hasZeroArea()
const {
return !*
this || (*this)->hasZeroArea(); }
437 (*this)->unionWith(b);
447 if (b) unionWith(*b);
456 *
this =
CRect(*x, *y);
476 (*this)->expandTo(p);
503 if (!*
this != !other)
return false;
504 return *
this ? **
this == *other :
true;
507 if (!*
this)
return false;
508 return **
this == other;
530 return out <<
"Rect " << r[
X] <<
" x " << r[
Y];
535 return r ? (out << *r) : (out <<
"Rect (empty)");
Adaptor that creates 2D functions from 1D ones.
A range of numbers which is never empty.
A range of numbers that can be empty.
Axis-aligned generic rectangle that can be empty.
GenericOptRect< C > & operator|=(OptCRect const &b)
Union with b.
static OptCRect from_range(InputIterator start, InputIterator end)
Create a rectangle from a range of points.
void unionWith(OptCRect const &b)
Enlarge the rectangle to contain the argument.
GenericOptRect(GenericRect< C > const &a)
GenericOptRect(OptCInterval const &x_int, OptCInterval const &y_int)
Creates an empty OptRect when one of the argument intervals is empty.
bool contains(CRect const &r) const
Check whether the rectangle includes all points in the given rectangle.
GenericOptRect(C x0, C y0, C x1, C y1)
bool intersects(OptCRect const &r) const
Check whether the rectangles have any common points.
typename CoordTraits< C >::OptIntervalType OptCInterval
typename CoordTraits< C >::PointType CPoint
bool contains(OptCRect const &r) const
Check whether the rectangle includes all points in the given rectangle.
OptCRect regularized() const
Returns an empty optional (testing false) if the rectangle has zero area.
typename CoordTraits< C >::OptRectType OptCRect
bool operator==(OptCRect const &other) const
Test for equality.
GenericOptRect(CPoint const &a, CPoint const &b)
bool contains(CPoint const &p) const
Check whether the given point is within the rectangle.
C area() const
Compute the rectangle's area.
GenericOptRect< C > & operator&=(OptCRect const &b)
Intersect with b.
void unionWith(CRect const &b)
Enlarge the rectangle to contain the argument.
bool empty() const
Check for emptiness.
CInterval const & D1ConstReference
std::optional< CRect > Base
void intersectWith(OptCRect const &b)
Leave only the area overlapping with the argument.
typename CoordTraits< C >::RectType CRect
GenericOptRect< C > & operator&=(CRect const &b)
Intersect with b.
bool hasZeroArea() const
Check whether the rectangle has zero area.
bool intersects(CRect const &r) const
Check whether the rectangles have any common points.
bool operator==(CRect const &other) const
typename CoordTraits< C >::IntervalType CInterval
void expandTo(CPoint const &p)
Create or enlarge the rectangle to contain the given point.
void intersectWith(CRect const &b)
Leave only the area overlapping with the argument.
Axis aligned, non-empty, generic rectangle.
static CRect from_xywh(C x, C y, C w, C h)
Create rectangle from origin and dimensions.
C distanceSq(CPoint const &p) const
Get rectangle's distance SQUARED away from the given point.
C right() const
Return rightmost coordinate of the rectangle (+X is to the right).
GenericRect()=default
Create a rectangle that contains only the point at (0, 0).
typename CoordTraits< C >::RectType CRect
CPoint clamp(CPoint const &p) const
Clamp point to the rectangle.
C area() const
Compute the rectangle's area.
void setMin(CPoint const &p)
Set the upper left point of the rectangle.
bool contains(GenericRect< C > const &r) const
Check whether the rectangle includes all points in the given rectangle.
GenericRect< C > & operator|=(OptCRect const &o)
bool intersects(GenericRect< C > const &r) const
Check whether the rectangles have any common points.
GenericRect(C x0, C y0, C x1, C y1)
Create rectangle from coordinates of two points.
static CRect from_range(InputIterator start, InputIterator end)
Create a rectangle from a range of points.
bool contains(OptCRect const &r) const
Check whether the rectangle includes all points in the given rectangle.
void unionWith(OptCRect const &b)
Enlarge the rectangle to contain the argument.
void setLeft(C val)
Set the minimum X coordinate of the rectangle.
bool contains(CPoint const &p) const
Check whether the given point is within the rectangle.
C top() const
Return top coordinate of the rectangle (+Y is downwards).
static CRect from_xywh(CPoint const &xy, CPoint const &wh)
Create rectangle from origin and dimensions.
Coord aspectRatio() const
Get the ratio of width to height of the rectangle.
CInterval const & D1ConstReference
void shrinkBy(C amount)
Shrink the rectangle in both directions by the specified amount.
bool intersects(OptCRect const &r) const
Check whether the rectangles have any common points.
void setTop(C val)
Set the minimum Y coordinate of the rectangle.
void setMax(CPoint const &p)
Set the lower right point of the rectangle.
CPoint nearestEdgePoint(CPoint const &p) const
Get the nearest point on the edge of the rectangle.
void setRight(C val)
Set the maximum X coordinate of the rectangle.
CPoint midpoint() const
Get the point in the geometric center of the rectangle.
void setBottom(C val)
Set the maximum Y coordinate of the rectangle.
void expandBy(C amount)
Expand the rectangle in both directions by the specified amount.
C left() const
Return leftmost coordinate of the rectangle (+X is to the right).
typename CoordTraits< C >::IntervalType CInterval
GenericRect< C > & operator|=(CRect const &o)
Union two rectangles.
static CRect infinite()
Create infinite rectangle.
CInterval const & operator[](Dim2 d) const
void expandTo(CPoint const &p)
Enlarge the rectangle to contain the given point.
GenericRect(CInterval const &a, CInterval const &b)
Create a rectangle from X and Y intervals.
C height() const
Get the vertical extent of the rectangle.
C minExtent() const
Get the smaller extent (width or height) of the rectangle.
GenericRect< C > & operator+=(CPoint const &p)
Offset the rectangle by a vector.
void unionWith(CRect const &b)
Enlarge the rectangle to contain the argument.
C width() const
Get the horizontal extent of the rectangle.
void expandBy(C x, C y)
Expand the rectangle in both directions.
void expandBy(CPoint const &p)
Expand the rectangle by the coordinates of the given point.
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).
typename CoordTraits< C >::OptRectType OptCRect
CInterval const & operator[](unsigned i) const
C maxExtent() const
Get the larger extent (width or height) of the rectangle.
CPoint dimensions() const
Get rectangle's width and height as a point.
CInterval & operator[](unsigned i)
GenericRect(CPoint const &a, CPoint const &b)
Create a rectangle from two points.
typename CoordTraits< C >::PointType CPoint
CPoint corner(unsigned i) const
Return the n-th corner of the rectangle.
GenericRect< C > & operator-=(CPoint const &p)
Offset the rectangle by the negation of a vector.
static CRect from_array(CPoint const *c, unsigned n)
Create a rectangle from a C-style array of points it should contain.
CPoint max() const
Get the corner of the rectangle with largest coordinate values.
bool operator==(CRect const &o) const
Test for equality of rectangles.
void shrinkBy(C x, C y)
Shrink the rectangle in both directions by possibly different amounts.
CInterval & operator[](Dim2 d)
bool hasZeroArea() const
Check whether the rectangle has zero area.
Integral and real coordinate types and some basic utilities.
Dim2
2D axis enumeration (X or Y).
double Coord
Floating point type used to store coordinates.
Various utility functions.
std::ostream & operator<<(std::ostream &os, const Bezier &b)
MultiDegree< n > max(MultiDegree< n > const &p, MultiDegree< n > const &q)
Returns the maximal degree appearing in the two arguments for each variables.
bool contains(Path const &p, Point const &i, bool evenodd=true)
Piecewise< SBasis > min(SBasis const &f, SBasis const &g)
Return the more negative of the two functions pointwise.
Traits class used with coordinate types.