Results 1 to 7 of 7

Thread: Fill area of QPainterPath

  1. #1
    Join Date
    Jun 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default 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. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Fill area of QPainterPath

    You can use this
    Qt Code:
    1. 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. #3
    Join Date
    Jun 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fill area of QPainterPath

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

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default 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. #5
    Join Date
    Jun 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default 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. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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. #7
    Join Date
    Jun 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

Similar Threads

  1. Replies: 5
    Last Post: 17th December 2014, 12:07
  2. Replies: 10
    Last Post: 7th March 2012, 18:22
  3. Replies: 1
    Last Post: 9th May 2011, 15:19
  4. QPainterPath::quadTo(...) calls QPainterPath::cubicTo(...) ?
    By brucelamond in forum Qt Programming
    Replies: 0
    Last Post: 28th April 2011, 23:30
  5. Replies: 6
    Last Post: 18th December 2008, 21:16

Bookmarks

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.