## center of gravity

Moderator: andrew

Forum rules

Attach drawing files and screenshots.

Post one question per topic.

greed
Newbie Member
Posts: 5
Joined: Thu Sep 28, 2017 5:29 pm

### center of gravity

How can I find the geometric center of a polygon ?
How would I find the center of gravity or geometric center of two separated polygons ?
I'm trying to find the CLR (center of lateral resistance and the CE (center of effort ) on a sailboat plan.

andrew
Posts: 8058
Joined: Fri Mar 30, 2007 6:07 am

### Re: center of gravity

I'm afraid QCAD does not include tools to compute any of these specific points.

CVH
Premier Member
Posts: 1601
Joined: Wed Sep 27, 2017 4:17 pm

### Re: center of gravity

https://en.wikipedia.org/wiki/Centroid
Sroll down to
Centroid of a polygon

CVH
Premier Member
Posts: 1601
Joined: Wed Sep 27, 2017 4:17 pm

### Re: center of gravity

Andrew,

Stumbled on:
InfoPolylineArea.prototype.getCenter()
InfoArea.prototype.getCenter()

As far as I can tell that is the centroid of a non-self-intersecting closed polygon defined by n vertices.

But here only valid for polygons with CW orientation.
There the getArea() returns an absolute value and the polygon is assumed to be CCW.

The source in the wiki defines the area of a polygon as:
http://paulbourke.net/geometry/polygonmesh/
-> "Calculating the area and centroid of a polygon"

polyarea6.gif (2.67 KiB) Viewed 1072 times
And that seems to not match with:

Code: Select all

``````    var j = nPts - 1;
for (var i = 0; i < nPts; j = i++) {
p1 = pts[i];
p2 = pts[j];
area += p1.x * p2.y;
area -= p1.y * p2.x;
}
area /= 2;``````
There p2 is the previous point and not the next point aka (xi+1, yi+1)

Same goes for the centroid formula and the js code of both the getCenter() mentioned above:
polyarea5.gif (5.28 KiB) Viewed 1072 times

Bottom line:
When I swap p1 and p2 and allow for negative values for the area then all turns to be OK.

Further simplified there both getCenter & getArea iterate the same point cloud and value f can be summed to the signed area:

Code: Select all

``````InfoArea.prototype.getCenter = function() {
var pts = this.polyline.getVertices();
var nPts = pts.length;
var x = 0;
var y = 0;
var f;
var j = nPts - 1;
var p1;
var p2;
var area = 0;

for (var i = 0; i < nPts; j = i++) {
p2 = pts[i];
p1 = pts[j];
f = p1.x * p2.y - p2.x * p1.y;
x += (p1.x + p2.x) * f;
y += (p1.y + p2.y) * f;
area += f;
}

f = area * 3;
return new RVector(x / f, y / f);
};``````
Regards,
CVH

andrew
Posts: 8058
Joined: Fri Mar 30, 2007 6:07 am

### Re: center of gravity

Thanks, looks indeed like a bit of orphaned code. I've removed it.

CVH
Premier Member
Posts: 1601
Joined: Wed Sep 27, 2017 4:17 pm

### Re: center of gravity

My intention was to correct it and include it as the centroid position of a polygon ...
Pitty...
CVH

andrew
Posts: 8058
Joined: Fri Mar 30, 2007 6:07 am

### Re: center of gravity

Yes, I might get back to this if time allows...

guyrinf
Registered Member
Posts: 2
Joined: Wed Apr 16, 2014 12:51 pm

### Re: center of gravity

operating system: Ubuntu 18.04

Too bad, I have to use Turbocad to find the center of gravity. It would be so good to include it in Qcad. I often work with sailboat plans on Qcad.

CVH
Premier Member
Posts: 1601
Joined: Wed Sep 27, 2017 4:17 pm

guy,