This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
neofx-zoomedit/Rectangle.cpp

51 lines
1.5 KiB
C++
Raw Normal View History

2007-09-16 20:59:04 +00:00
#include "Rectangle.h"
bool Rectangle::contains(const Vertex &v) const {
return (edges(v) == EDGE_NONE);
}
int Rectangle::edges(const Vertex &v) const {
int ret = EDGE_NONE;
if(v.getX() < v1.getX()) ret |= EDGE_LEFT;
else if(v.getX() >= v2.getX()) ret |= EDGE_RIGHT;
if(v.getY() < v1.getY()) ret |= EDGE_TOP;
else if(v.getY() >= v2.getY()) ret |= EDGE_BOTTOM;
return ret;
}
int Rectangle::intersects(const Line &l, Vertex *v, int edge) const {
const Line top(v1.getX(), v1.getY(), v2.getX(), v1.getY());
const Line bottom(v1.getX(), v2.getY(), v2.getX(), v2.getY());
const Line left(v1.getX(), v1.getY(), v1.getX(), v2.getY());
const Line right(v2.getX(), v1.getY(), v2.getX(), v2.getY());
if(edge == EDGE_NONE) edge = edges(l.getVertex1());
if((edge & EDGE_TOP) && (top.intersects(l, v) == INTERSECTION_SEGMENT_SEGMENT))
return EDGE_TOP;
if((edge & EDGE_BOTTOM) && (bottom.intersects(l, v) == INTERSECTION_SEGMENT_SEGMENT))
return EDGE_BOTTOM;
if((edge & EDGE_LEFT) && (left.intersects(l, v) == INTERSECTION_SEGMENT_SEGMENT))
return EDGE_LEFT;
if((edge & EDGE_RIGHT) && (right.intersects(l, v) == INTERSECTION_SEGMENT_SEGMENT))
return EDGE_RIGHT;
v->setLocation(0, 0);
return EDGE_NONE;
}
int Rectangle::intersects(const Line &l, Vertex *v1, Vertex *v2, int edge) const {
int ret = EDGE_NONE;
if(edge == EDGE_NONE) edge = edges(l.getVertex1());
ret |= intersects(l, v1, edge);
ret |= intersects(l, v2, EDGE_ALL^edge);
return ret;
}