Results 1 to 3 of 3

Thread: QPainterPath or QPolygon centroid/center/origin - How to determine ?

  1. #1
    Join Date
    Dec 2008
    Posts
    16
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QPainterPath or QPolygon centroid/center/origin - How to determine ?

    I want to graphically depict the center point (center point of gravity) of a complex/non-symmetrical Polygon. There is no

    QPointF QPolygon::center()

    Finding the center of the boundingRect was easy, but wrong.

    Any code/idea ?
    Last edited by muenalan; 3rd December 2008 at 11:25.

  2. #2
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    5
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainterPath or QPolygon centroid/center/origin - How to determine ?

    I can't provide you with any mathematical code snippets, but I use the GEOSGetCentroid function from the C++ library GEOS to get the centroid. It has an easy-to-use C wrapper, which the developers recommend. It's quite a nice library and is under the LGPL. Here's an example of using the library for getting the centroid.

    Qt Code:
    1. GEOSGeometry *getPolygon(bool &error) {
    2. //free the returned value with GEOSGeom_destroy
    3. error = false;
    4. if (points.size() < 3) {
    5. error = true;
    6. return 0;
    7. } else {
    8. GEOSCoordSequence *seq = GEOSCoordSeq_create(points.size()+1, 2);
    9. for (int a=0;a<points.size();++a) {
    10. GEOSCoordSeq_setX(seq, a, points[a].x());
    11. GEOSCoordSeq_setY(seq, a, points[a].y());
    12. }
    13. GEOSCoordSeq_setX(seq, points.size(), points[0].x());
    14. GEOSCoordSeq_setY(seq, points.size(), points[0].y());
    15. GEOSGeometry *ring = GEOSGeom_createLinearRing(seq);
    16. return GEOSGeom_createPolygon(ring, NULL, 0);
    17. }
    18. }
    19.  
    20. QPointF getCenter(bool &error) {
    21. GEOSGeometry *poly = getPolygon(error);
    22. if (error) {
    23. return QPointF();
    24. } else {
    25. GEOSGeometry *cpoint = GEOSGetCentroid(poly);
    26. const GEOSCoordSequence *cseq = GEOSGeom_getCoordSeq(cpoint);
    27. double x=0, y=0;
    28. GEOSCoordSeq_getX(cseq, 0, &x);
    29. GEOSCoordSeq_getY(cseq, 0, &y);
    30. GEOSGeom_destroy(poly);
    31. GEOSGeom_destroy(cpoint);
    32. return QPointF(x, y);
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by init2null; 3rd December 2008 at 21:52.
    ImageRocket - My Qt4-based image editing program
    Project Page / Screenshots / Source

  3. The following user says thank you to init2null for this useful post:

    muenalan (24th January 2009)

  4. #3
    Join Date
    Dec 2008
    Posts
    16
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainterPath or QPolygon centroid/center/origin - How to determine ?

    Thanks, I'll have a look at

    GEOSGetCentroid(poly)

    Thanks for the helpfull reply,
    Mu

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.