summaryrefslogtreecommitdiff
path: root/src/boundary.cpp
diff options
context:
space:
mode:
authorTim Redfern <tim@eclectronics.org>2012-10-11 19:43:47 +0100
committerTim Redfern <tim@eclectronics.org>2012-10-11 19:43:47 +0100
commite2e45966d7a3ca7673bdbaadef5b5e8a38b0ff78 (patch)
tree0a93d0d044b51f808658124761ac85afdcb23fae /src/boundary.cpp
initial commit
Diffstat (limited to 'src/boundary.cpp')
-rwxr-xr-xsrc/boundary.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/boundary.cpp b/src/boundary.cpp
new file mode 100755
index 0000000..c30f676
--- /dev/null
+++ b/src/boundary.cpp
@@ -0,0 +1,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;
+}