muenalan
3rd December 2008, 10:53
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 ?
init2null
3rd December 2008, 20:41
I can't provide you with any mathematical code snippets, but I use the GEOSGetCentroid function from the C++ library GEOS (http://trac.osgeo.org/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.
GEOSGeometry *getPolygon(bool &error) {
//free the returned value with GEOSGeom_destroy
error = false;
if (points.size() < 3) {
error = true;
return 0;
} else {
GEOSCoordSequence *seq = GEOSCoordSeq_create(points.size()+1, 2);
for (int a=0;a<points.size();++a) {
GEOSCoordSeq_setX(seq, a, points[a].x());
GEOSCoordSeq_setY(seq, a, points[a].y());
}
GEOSCoordSeq_setX(seq, points.size(), points[0].x());
GEOSCoordSeq_setY(seq, points.size(), points[0].y());
GEOSGeometry *ring = GEOSGeom_createLinearRing(seq);
return GEOSGeom_createPolygon(ring, NULL, 0);
}
}
QPointF getCenter(bool &error) {
GEOSGeometry *poly = getPolygon(error);
if (error) {
return QPointF();
} else {
GEOSGeometry *cpoint = GEOSGetCentroid(poly);
const GEOSCoordSequence *cseq = GEOSGeom_getCoordSeq(cpoint);
double x=0, y=0;
GEOSCoordSeq_getX(cseq, 0, &x);
GEOSCoordSeq_getY(cseq, 0, &y);
GEOSGeom_destroy(poly);
GEOSGeom_destroy(cpoint);
return QPointF(x, y);
}
}
muenalan
24th January 2009, 08:11
Thanks, I'll have a look at
GEOSGetCentroid(poly)
Thanks for the helpfull reply,
Mu
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.