How to get polygon centroid

95 views
Skip to first unread message

Ddm

unread,
Oct 24, 2016, 3:36:29 AM10/24/16
to JSXGraph
Hi, 
I'm trying to access the polygon centroid to center a polygon figure against a point. 
It seems that centroid is being used to position the polygon label but doesn't seems to be externally accessible. 

Looking at examples, I see that there is a quite old example at https://github.com/stepheneb/jsxgraph/blob/master/examples/centroid_trace.html
that creates centroid for a give array of points  -> p4 = board.createElement('centroid', [p1, p2, p3], {name:"S", trace:true});

It is similar to the way geogebra constructor, but doesn't seems to be available/work anymore


In a group example, it seems that it si also internally available for some internal computations :

   var pol = board.create('polygon', p, {hasInnerPoints: true});
   var g = board.create('group', p).setRotationCenter('centroid').setRotationPoints([p[1], p[2]]);


 But again, it doesn't seem to be externally available to access the point. 

How can I get that center point of the polygon?

Thanks 

Alfred Wassermann

unread,
Oct 24, 2016, 4:08:27 AM10/24/16
to JSXGraph
You are right, there is no method to get the coordinates of the centroid of a polygon. I will introduce it.
Until then you can compute like that:

        getCentroid = function (polygon) {
           
var center, len, i;

            center
= [0.0, 0.0];
            len
= polygon.vertices.length;
           
for (i = 0; i < len; i++) {
                center
[0] += polygon.vertices[i].coords.usrCoords[1];
                center[1] += polygon.vertices[i].coords.usrCoords[2];
            }
           
if (len > 0) {
                center
[0] /= len;
                center
[1] /= len;
           
} else {
                center = [NaN, NaN];
            }
           
return center;
       
};

The centroid object you mentioned is an example how to add new elements to JSXGraph and is not part of jsxgraphcore.js.

Best wishes,
Alfred


Ddm

unread,
Oct 24, 2016, 5:01:10 AM10/24/16
to JSXGraph
Hi Alfred, 
as helpfull as usual.

Thanks a lot Alfred

Alfred Wassermann

unread,
Oct 26, 2016, 9:50:36 AM10/26/16
to JSXGraph
Sorry, there is a small mistake in the above code to determine the coordinates of the centroid of a polygon.
It was neglected that there is an additional point wihich is identical to the first point.
Thus the code must look like that:

        getCentroid = function (polygon) {
           
var center, len, i;

            center
= [0.0, 0.0];

            len
= polygon.vertices.length - 1;

           
for (i = 0; i < len; i++) {
                center
[0] += polygon.vertices[i].coords.usrCoords[1];
                center[1] += polygon.vertices[i].coords.usrCoords[2];
            }
           
if (len > 0) {
                center
[0] /= len;
                center
[1] /= len;
           
} else {
                center
= [NaN, NaN];
           
}
           
return center;
       
};

Best wishes,
Alfred
 

Ddm

unread,
Oct 27, 2016, 1:28:54 AM10/27/16
to JSXGraph
Thanks for pointing that out Alfred,

I the initial examples, it was not relevant and I sould not see it, but I guess that I would shortly face issues.

Best regards

David
Reply all
Reply to author
Forward
0 new messages