summaryrefslogtreecommitdiffstats
path: root/Rectangle.cpp
blob: c90ab0d8279fe451b407269f57c12fc5f62f6546 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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;
}