diff options
author | neoraider <devnull@localhost> | 2007-09-16 22:59:04 +0200 |
---|---|---|
committer | neoraider <devnull@localhost> | 2007-09-16 22:59:04 +0200 |
commit | 96e9d04527dc17a2a4f8c2f7034c1c356ed5186e (patch) | |
tree | 20bc1fe6fd680005c47c49396aca5b6e237e1c7d /Rectangle.cpp | |
parent | 5ea7f0464eb13581322215f0614eaae52393e02a (diff) | |
download | zoomedit-96e9d04527dc17a2a4f8c2f7034c1c356ed5186e.tar zoomedit-96e9d04527dc17a2a4f8c2f7034c1c356ed5186e.zip |
zoomedit: C++ized Line & Rectangle
Diffstat (limited to 'Rectangle.cpp')
-rw-r--r-- | Rectangle.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
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; +} |