72 lines
1.4 KiB
C++
72 lines
1.4 KiB
C++
#ifndef BBOX2_H
|
|
#define BBOX2_H
|
|
|
|
#include "Point2.h"
|
|
#include "Line2.h"
|
|
|
|
class BBox2 {
|
|
|
|
protected:
|
|
|
|
static constexpr float MAX = +99999;
|
|
static constexpr float MIN = -99999;
|
|
|
|
/** minimum */
|
|
Point2 p1;
|
|
|
|
/** maximum */
|
|
Point2 p2;
|
|
|
|
public:
|
|
|
|
BBox2() : p1(MAX,MAX), p2(MIN,MIN) {;}
|
|
|
|
/** adjust the bounding-box by adding this point */
|
|
void add(const Point2& p) {
|
|
|
|
if (p.x > p2.x) {p2.x = p.x;}
|
|
if (p.y > p2.y) {p2.y = p.y;}
|
|
|
|
if (p.x < p1.x) {p1.x = p.x;}
|
|
if (p.y < p1.y) {p1.y = p.y;}
|
|
|
|
}
|
|
|
|
/** get the bbox's minimum */
|
|
const Point2& getMin() const {return p1;}
|
|
|
|
/** get the bbox's maximum */
|
|
const Point2& getMax() const {return p2;}
|
|
|
|
/** equal? */
|
|
bool operator == (const BBox2& o) const {
|
|
return (p1.x == o.p1.x) &&
|
|
(p1.y == o.p1.y) &&
|
|
(p2.x == o.p2.x) &&
|
|
(p2.y == o.p2.y);
|
|
}
|
|
|
|
/** does the BBox intersect with the given line? */
|
|
bool intersects (const Line2& l) const {
|
|
Line2 l1(p1.x, p1.y, p2.x, p1.y); // upper
|
|
Line2 l2(p1.x, p2.y, p2.x, p2.y); // lower
|
|
Line2 l3(p1.x, p1.y, p1.x, p2.y); // left
|
|
Line2 l4(p2.x, p1.y, p2.x, p2.y); // right
|
|
return l.getSegmentIntersection(l1) ||
|
|
l.getSegmentIntersection(l2) ||
|
|
l.getSegmentIntersection(l3) ||
|
|
l.getSegmentIntersection(l4);
|
|
}
|
|
|
|
bool contains(const Point2& p) const {
|
|
if (p.x < p1.x) {return false;}
|
|
if (p.x > p2.x) {return false;}
|
|
if (p.y < p1.y) {return false;}
|
|
if (p.y > p2.y) {return false;}
|
|
return true;
|
|
}
|
|
|
|
};
|
|
|
|
#endif // BBOX2_H
|