summaryrefslogtreecommitdiff
path: root/gaunt01/src/outsidePolygon.cpp
blob: deda88f8dbf3c82e309def03cf10d15f11fdd5b7 (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
#include "outsidePolygon.h"


bool OutsidePolygon(vector<ofPoint>& polygon,ofPoint p)
//winding rule algorithm for 2D polygon containment test
//thanks to Paul Bourke
//http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/
{
  int counter = 0;
  int i;
  double xinters;
  ofPoint p1,p2;

  p1 = polygon[0];
  for (i=1;i<=polygon.size();i++) {
    p2 = polygon[i % polygon.size()];
    if (p.y > min(p1.y,p2.y)) {
      if (p.y <= max(p1.y,p2.y)) {
        if (p.x <= max(p1.x,p2.x)) {
          if (p1.y != p2.y) {
            xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
            if (p1.x == p2.x || p.x <= xinters)
              counter++;
          }
        }
      }
    }
    p1 = p2;
  }

  if (counter % 2 == 0)
    return true;
  else
    return false;
}