From 96e9d04527dc17a2a4f8c2f7034c1c356ed5186e Mon Sep 17 00:00:00 2001 From: neoraider Date: Sun, 16 Sep 2007 20:59:04 +0000 Subject: zoomedit: C++ized Line & Rectangle --- Rectangle.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Rectangle.cpp (limited to 'Rectangle.cpp') diff --git a/Rectangle.cpp b/Rectangle.cpp new file mode 100644 index 0000000..c90ab0d --- /dev/null +++ b/Rectangle.cpp @@ -0,0 +1,50 @@ +#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; +} -- cgit v1.2.3