Oh men... This time google wasn't helping much...
I was looking for an algorithm to find the centroid from a quadrilateral polygon. It wasn't necessary to be a general, any polygon, centroid algorithm... just one which give me the centroid of a regular convex quadrilateral. It was hard to find something that simple. But finally, here is.
The following code is a C example on how to compute the area and the centroid from a regular convex quadrilateral polygon:
I was looking for an algorithm to find the centroid from a quadrilateral polygon. It wasn't necessary to be a general, any polygon, centroid algorithm... just one which give me the centroid of a regular convex quadrilateral. It was hard to find something that simple. But finally, here is.
The following code is a C example on how to compute the area and the centroid from a regular convex quadrilateral polygon:
#include<stdio.h>
int main(void){
float verticesX[5];
float verticesY[5];
float centroidX = 0;
float centroidY = 0;
verticesX[0] = 3.58; verticesY[0] = 1.90;
verticesX[1] = 4.48; verticesY[1] = 1.88;
verticesX[2] = 4.56; verticesY[2] = 2.71;
verticesX[3] = 3.64; verticesY[3] = 2.74;
verticesX[4] = 3.58; verticesY[4] = 1.90; // Repeat the first vertex
int i, k;
float area = 0.0f;
float tmp = 0.0f;
for (i = 0; i <= 4; i++){
k = (i + 1) % (4 + 1);
tmp = verticesX[i] * verticesY[k] -
verticesX[k] * verticesY[i];
area += tmp;
centroidX += (verticesX[i] + verticesX[k]) * tmp;
centroidY += (verticesY[i] + verticesY[k]) * tmp;
}
area *= 0.5f;
centroidX *= 1.0f / (6.0f * area);
centroidY *= 1.0f / (6.0f * area);
printf("Centroid = (%1.2f, %1.2f), area = %1.2f\n", centroidX, centroidY, area);
return 0;
}
Comments
Post a Comment