blob: c30f6769f102c84dd0421a6e82b0dd7f1329fca9 (
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#include "boundary.h"
boundary::boundary()
{
note=40; //middle C
}
boundary::boundary(int _note)
{
note=_note;
}
boundary::~boundary()
{
}
void boundary::draw(){
if (points.size()>1) {
for (int i=0;i<points.size();i++) {
ofLine(points[i],points[(i+1)%points.size()]);
}
ofDrawBitmapString(ofToString(note),centroid);
}
}
void boundary::add(ofPoint p){
points.push_back(p);
getCentroid();
}
void boundary::undo(){
if (points.size()>0) {
points.erase(points.end()-1);
getCentroid();
}
}
void boundary::getCentroid(){
float x=0;
float y=0;
for (int i=0;i<points.size();i++) {
x+=points[i].x;
y+=points[i].y;
}
centroid=ofPoint(x/points.size(),y/points.size(),0);
}
bool boundary::contains(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 = points[0];
for (i=1;i<=points.size();i++) {
p2 = points[i % points.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 false;
else
return true;
}
|