PDA

View Full Version : Find point of intersection/overlaping?



Morea
19th April 2007, 13:41
If I have two QGraphicsItems of some kind, and they are moved to intersect/overlap eachother, is there an easy way to find the point (or region) of intersection?

marcel
19th April 2007, 14:00
No really...
You could use the shapes of the two items( QPainterPaths ) and implement an algorithm for contour intersection (to determine the shape of intersection). But for complex shapes, these algorithms are inaccurate and slow.

Regards

marcel
19th April 2007, 14:01
Maybe you could approximate the intersection area by intersecting only the bounding rectangles of the two items. I don't know if that is enough, however.

high_flyer
19th April 2007, 14:03
All though I haven't done it my self yet, I know that the Graphics View frame work supports it from the box:
http://doc.trolltech.com/4.2/graphicsview.html#the-graphics-view-coordinate-system


QGraphicsItem supports collision detection through the QGraphicsItem::shape() function, and QGraphicsItem::collidesWith(), which are both virtual functions. By returning your item's shape as a local coordinate QPainterPath from QGraphicsItem::shape(), QGraphicsItem will handle all collision detection for you. If you want to provide your own collision detection, however, you can reimplement QGraphicsItem::collidesWith().

marcel
19th April 2007, 14:10
ll though I haven't done it my self yet, I know that the Graphics View frame work supports it from the box:
http://doc.trolltech.com/4.2/graphic...rdinate-system (http://doc.trolltech.com/4.2/graphicsview.html#the-graphics-view-coordinate-system)
Quote:
QGraphicsItem supports collision detection through the QGraphicsItem::shape() function, and QGraphicsItem::collidesWith(), which are both virtual functions. By returning your item's shape as a local coordinate QPainterPath from QGraphicsItem::shape(), QGraphicsItem will handle all collision detection for you. If you want to provide your own collision detection, however, you can reimplement QGraphicsItem::collidesWith().


Yes, but in that reimplementation he must compute the intersection shape, because Qt won't provide it.

high_flyer
19th April 2007, 14:14
Yes, but in that reimplementation he must compute the intersection shape, because Qt won't provide it.
But isn't that only in case he needs something special?
Normal collision detection is provided by the frame work - at leaste that is they I dunderstand the docs.

marcel
19th April 2007, 14:16
Yes, yes, but in the end it comes down to actually computing the intersection of two shapes. Qt will only tell you if the collide.

Morea
19th April 2007, 14:21
The items will probably not intersect very much so I thought of this:

1) Find smaller and smaller rects that contains the intersected parts
2) choose the mid point of the rectangle as the point of intersection (yeah, not rocket science, but that migh suffice)

Then I would start by usign bounding rects for the items, find their intersction, but what should I do in the next step to find smaller rects?

marcel
19th April 2007, 14:33
The items might not necessarily intersect in a point - might be an area, etc.
But to find the intersection point from the intersection rectangle test each point in the intersection rectangle and see which one belongs to both items. This is your intersection point.

wysota
19th April 2007, 14:42
The first question should be - "what do you need it for?" - maybe the intersection (which is computation intensive to get) is not needed after all.