# Thread: Fill area of QPainterPath

1. Beginner
Join Date
Jun 2009
Posts
7
Qt products
Platforms

## Fill area of QPainterPath

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!

2. ## Re: Fill area of QPainterPath

You can use this
Qt Code:
`QPainterPath QPainterPath::intersected ( const QPainterPath & p ) const;`
To copy to clipboard, switch view to plain text mode

Edit: get the intersected path and paint it

3. Beginner
Join Date
Jun 2009
Posts
7
Qt products
Platforms

## Re: Fill area of QPainterPath

Thank you, but I'm interested in calculating the area, not painting it.

4. ## Re: Fill area of QPainterPath

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?

5. Beginner
Join Date
Jun 2009
Posts
7
Qt products
Platforms

## Re: Fill area of QPainterPath

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?

6. Expert
Join Date
Sep 2009
Location
Wroclaw, Poland
Posts
1,391
Thanked 342 Times in 324 Posts
Qt products
Platforms

## Re: Fill area of QPainterPath

Maybe for your purposes you can convert the QPainterPath to QPolygonF with 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.
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.

7. Beginner
Join Date
Jun 2009
Posts
7
Qt products
Platforms

## Re: Fill area of QPainterPath

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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.