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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
#include "boundary.h"
boundary::boundary()
{
filename="";
}
boundary::boundary(string file,float vol)
{
openFile(file);
sound.setVolume(vol);
}
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(filename,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 y=0;
float maxx=-1;
float minx=100000;
for (int i=0;i<points.size();i++) {
y+=points[i].y;
minx=min(minx,points[i].x);
maxx=max(maxx,points[i].x);
}
centroid=ofPoint(minx+((maxx-minx)*0.1),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;
}
int boundary::findPoint(ofPoint pos){
int threshold=6;
int selected=-1;
for (int i=0;i<points.size();i++) {
if (abs(points[i].x-pos.x)<=threshold&&abs(points[i].x-pos.x)<=threshold) {
selected=i;
}
}
return selected;
}
void boundary::openFile(string file) {
filename=file;
if (filename.length()>0) {
if (sound.loadSound(filename)) sound.setLoop(false);
else printf("failed to load %s\n",filename.c_str());
}
}
bool boundary::checkFile(ofPoint pos,string file) {
if (contains(pos)) {
sound.stop();
openFile(file);
return true;
}
else return false;
}
bool boundary::checkClick(ofPoint pos){
if (contains(pos)&&sound.isLoaded()&&!sound.getIsPlaying()) {
sound.play();
return true;
}
else return false;
}
void boundary::setVolume(float vol){
sound.setVolume(vol);
}
|