diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index 9717451..12ccd0c 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -119,12 +119,22 @@ void MapperGLCanvas::mousePressEvent(QMouseEvent* event) _mousepressed = true; } } + if (event->buttons() & Qt::RightButton) + { + Shape* shape = getCurrentShape(); + if (shape->includesPoint(xmouse, ymouse)) + { + _shapegrabbed = true; + _shapefirstgrab = true; + } + } } void MapperGLCanvas::mouseReleaseEvent(QMouseEvent* event) { // std::cout << "Mouse Release event " << std::endl; _mousepressed = false; + _shapegrabbed = false; } void MapperGLCanvas::mouseMoveEvent(QMouseEvent* event) @@ -145,6 +155,26 @@ void MapperGLCanvas::mouseMoveEvent(QMouseEvent* event) emit quadChanged(); } } + else if (_shapegrabbed) + { + // std::cout << "Move event " << std::endl; + Shape* shape = getCurrentShape(); + static Point p(0,0); + if (shape) + { + if (_shapefirstgrab == false) + { + shape->translate(event->x() - p.x, event->y() - p.y); + update(); + emit quadChanged(); + } + else + _shapefirstgrab = false; + } + p.x = event->x(); + p.y = event->y(); + + } } void MapperGLCanvas::keyPressEvent(QKeyEvent* event) diff --git a/MapperGLCanvas.h b/MapperGLCanvas.h index f30492a..a94bf57 100644 --- a/MapperGLCanvas.h +++ b/MapperGLCanvas.h @@ -66,6 +66,8 @@ private: void exitDraw(); bool _mousepressed; int _active_vertex; + bool _shapegrabbed; + bool _shapefirstgrab; signals: void quadChanged(); diff --git a/Shape.h b/Shape.h index e0d9770..7d68a25 100644 --- a/Shape.h +++ b/Shape.h @@ -22,6 +22,7 @@ #include #include +#include /** * Point (or vertex) on the 2-D canvas. @@ -64,6 +65,58 @@ public: vertices[i].x = x; vertices[i].y = y; } + + /** Return true if Shape includes point (x,y), false otherwise + * Algorithm should work for all polygons, including non-convex + * Found at http://www.cs.tufts.edu/comp/163/notes05/point_inclusion_handout.pdf + */ + bool includesPoint(int x, int y) + { + Point *prev = NULL, *cur; + int left = 0, right = 0, maxy, miny; + for (std::vector::iterator it = vertices.begin() ; it != + vertices.end(); it++) + { + if (!prev) { + prev = &vertices.back(); + } + cur = &(*it); + miny = std::min(cur->y, prev->y); + maxy = std::max(cur->y, prev->y); + + if (y > miny && y < maxy) { + if (prev->x == cur->x) + { + if (x < cur->x) + right++; + else left++; + } + else + { + double slope = (cur->y - prev->y) / (cur->x - prev->x); + double offset = cur->y - slope * cur->x; + int xintersect = int((y - offset ) / slope); + if (x < xintersect) + right++; + else left++; + } + } + prev = &(*it); + } + if (right % 2 && left % 2) + return true; + return false; + } + /* Translate all vertices of shape by the vector (x,y) */ + void translate(int x, int y) + { + for (std::vector::iterator it = vertices.begin() ; it != + vertices.end(); ++it) + { + it->x += x; + it->y += y; + } + } }; /**