PDA

View Full Version : Fill area of QPainterPath

vides2012
16th June 2011, 15:35
Hello,

I'd like to ask if there's a way to calculate the area of a closed painter path. I'd like to calculate the common area of an ellipse and a polygon, and since QPainterPath supports intersecting paths, I thought I could solve the problem easily. I've searched for ellipse-polygon intersection on the net, but with no luck.
My idea is to paint the intersected path, and then count the pixels with the painter color. However this is very slow, and I'd like a fast(er) solution. Any help would be appreciated.

Thanks!

Santosh Reddy
16th June 2011, 16:14
You can use this

QPainterPath QPainterPath::intersected ( const QPainterPath & p ) const;

Edit: get the intersected path and paint it

vides2012
16th June 2011, 18:11
Thank you, but I'm interested in calculating the area, not painting it.

Santosh Reddy
16th June 2011, 18:20
Ok you have the intersection painter path, you can use it what evet you want.

By the way, what do you mean by area ? number of pixels, sub-pixels?

vides2012
16th June 2011, 18:25
I'm sorry to be confusing. By area I mean the mathematical area. Not in pixels, since that's only an approximation (or maybe that's good enough, since I don't see any other option). I'm talking about the geometrical area of the intersection of two geometrical objects. Is that clear now?

stampede
16th June 2011, 18:50
Maybe for your purposes you can convert the QPainterPath to QPolygonF (http://doc.qt.nokia.com/latest/qpolygonf.html) with toFillPolygon() (http://doc.qt.nokia.com/latest/qpainterpath.html#toFillPolygon) method, and calculate area of polygon (which is just a set of points).
Now the question is - what precision do you need ? Conversion is always just an approximation. Next thing is precision when calculating the area. QPolygonF does not provide any methods for area calculation, so you'll need to implement one yourself.
If you want it to be very accurate and/or don't want to mess with the math yourself, maybe you can use some geometry library, like CGAL (http://www.cgal.org/).
Simple "home-made" approximation could be to divide the boundingRect() of polygon to sub-rects, and for each of them check if polygon contains it's center. Then the "hit" ratio can be used to approximate area using total area of bounding rect. The more "sub-rects" you use, the more precision (and should be very simple to implement, since you have QPolygonF::containsPoint() method). Could be faster than drawing the painter path and counting the pixels too.

vides2012
17th June 2011, 14:00
Thanks for the reply, I'll try the polygon way. Calculating the area of a polygon is a much more documented problem :)
CGAL is not an option, since that's only part LGPL.