Merge branch 'shape-drag' of github.com:vliaskov/libremapping

Conflicts:
	MapperGLCanvas.h
	Shape.h
This commit is contained in:
Tats
2013-12-04 23:02:47 -05:00
3 changed files with 85 additions and 0 deletions
+30
View File
@@ -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)
+2
View File
@@ -66,6 +66,8 @@ private:
void exitDraw();
bool _mousepressed;
int _active_vertex;
bool _shapegrabbed;
bool _shapefirstgrab;
signals:
void quadChanged();
+53
View File
@@ -22,6 +22,7 @@
#include <vector>
#include <tr1/memory>
#include <iostream>
/**
* 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<Point>::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<Point>::iterator it = vertices.begin() ; it !=
vertices.end(); ++it)
{
it->x += x;
it->y += y;
}
}
};
/**