PDA

View Full Version : find the lines cross while draw free path using QPainterPath



wagmare
19th February 2015, 14:33
Hi friends,
im implementing the freeform style drawing for drawing a free path using the mouse press and move with Qpainter QPainterpath

now i have to detect when exactly the drawn path crossed or intersects with the other. how i can identify when the lines crossed each other at some point and give warning to the user.

is there a Qt API to tell that if a path composed of different elements intersect in a point or not.?
because QPainterPath::​intersects function can be used for two different painterpath . how to check for the current painterpath

10956

Santosh Reddy
23rd February 2015, 06:26
A workaround: Can you capture the current mouse path to a temp path, check if it intersects the master path it yes inform user, else just append the current mouse path to master path.

wagmare
23rd February 2015, 09:39
hi Santosh,

This is what i tried earlier

inside mouseMoveEvent


for(int i =1; i < theResvPath.elementCount(); i++)
{
QPointF repPath = theResvPath.elementAt(i);
QRectF sampRect = QRectF(e->pos().x() , e->pos().y() ,1, 1 );


bool intersects = theResvPath.intersects(sampRect);
qDebug()<<"The painter path:"<<repPath<<"and the rect:"<<sampRect<<"and the intersects:"<<intersects;


if(intersects){
}

theResvPath.lineTo(e->pos());

}

but the intersects return true even one x,y is inside the path

The painter path: QPointF(982.268, 337.732) and the rect: QRectF(917.938,609.897 1x1) and the intersects: true .

Santosh Reddy
23rd February 2015, 12:15
Try playing with code


#include <QtWidgets>

class MousePaintWidget : public QWidget
{
public:
explicit MousePaintWidget(QWidget * parent = 0)
: QWidget(parent)
, mousePressed(false)
{
;
}

protected:
void paintEvent(QPaintEvent *)
{
QPainter p(this);
p.drawPath(masterPath);

if(mousePressed)
{
p.setPen(Qt::red);
p.drawLine(firstPoint, lastPoint);
}
}

void mouseMoveEvent(QMouseEvent * mouseEvent)
{
if(mousePressed)
{
QPainterPath newPath;

newPath.moveTo(lastPoint);
newPath.lineTo(mouseEvent->pos());

if(masterPath.elementCount() == 0)
{
masterPath.moveTo(lastPoint);
}
else
{
if(newPath.intersects(masterPath))
{
mousePressed = false;
}
else
{
masterPath.lineTo(lastPoint);
}
}

lastPoint = mouseEvent->pos();

update();
}
}

void mousePressEvent(QMouseEvent * mouseEvent)
{
firstPoint = lastPoint = mouseEvent->pos();
masterPath = QPainterPath();
mousePressed = true;
}

void mouseReleaseEvent(QMouseEvent *)
{
mousePressed = false;
}

private:
bool mousePressed;
QPainterPath masterPath;
QPoint lastPoint;
QPoint firstPoint;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MousePaintWidget w;
w.show();

return app.exec();
}

wagmare
24th February 2015, 07:20
Hi ,
I tried your logic with the QpainterPath intersect and it is coming fine in the required but
QPainterPath::intersects() return true even if the endpoint is inside the unclosed area we have drawn .

i know im not clear with my answer so i added the image with this reply .

you can see the blue path is been drawn when the painterpath intersects .


void paintEvent(QPaintEvent *)
{
QPainter p(this);
p.drawPath(masterPath);

if(mousePressed)
{
p.setPen(Qt::red);
p.drawLine(firstPoint, lastPoint);
}

p.setPen(QPen(Qt::blue,4));
p.drawPath(newerPath);
}

void mouseMoveEvent(QMouseEvent * mouseEvent)
{
if(mousePressed)
{
QPainterPath newPath;

newPath.moveTo(lastPoint);
newPath.lineTo(mouseEvent->pos());

QList<QPolygonF> polyList = masterPath.toSubpathPolygons();


if(masterPath.elementCount() == 0)
{
masterPath.moveTo(lastPoint);
}
else
{


if(newPath.intersects(masterPath))
{

newerPath.moveTo(lastPoint);
newerPath.lineTo(mouseEvent->pos());




// mousePressed = false;

masterPath.lineTo(lastPoint);
}
else
{
masterPath.lineTo(lastPoint);
}
}

lastPoint = mouseEvent->pos();

update();
}
}

i have attached both the normal and the time when i move across the start point of the path

10960
10961


event the doc says
Set operations on paths will treat the paths as areas. Non-closed paths will be treated as implicitly closed.
please help me

thanks in advance

Santosh Reddy
3rd March 2015, 05:31
I think that is the way QPainterPath works....

Well you could try writing an intersection detection function for yourself, it is not trivial yet but not complex either, it involves a little geometry.