Skip to main content

Quadrilateral Centroid Algorithm

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:

#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

Popular posts from this blog

More trickery with gnuplot dumb terminal

In my post " Plotting memory usage on console " the chart doesn't pan the data. Now, using a named pipe, the effect got a little bit nicer. First, we have to run the memUsage.sh script to get a file filled with memory usage info: ./memUsage.sh > memUsage.dat & Then we have to create a named pipe: mkfifo pipe Now we have to run another process to tail only the last 64 lines from the memUsage.dat while [ 1 ]; do tail -64 memUsage.dat> pipe; done & And now we just have to plot the data from the pipe: watch -n 1 'gnuplot -e "set terminal dumb;p \"pipe\" with lines"' And that is it!

Replace transparency in PNG images with white background (for lots of files...)

I had to remove transparency from a PNG image file from the command line... and stack overflow came into my help[1]... But I needed it for lots of files... then, adding a "while read line" did the job: ls -1 *.png |  cut -d . -f 1 | while read line; do convert -flatten $line.png flatten/$line.png ; done; [1] Replace transparency in PNG images with white background https://stackoverflow.com/questions/2322750/replace-transparency-in-png-images-with-white-background

Pettry rendered LaTeX equations using PHP

When I was writing a simple scientific webapp, during my undergrads studies, I needed to generate some equations to be shown by the app. I was already familiar with LaTeX equation formatting syntax, so I decided to use this nice peace of software. So, after "googling" a little, I found the imgtex , written by Koji Nakamaru , which is a fast CGI script, written in perl. What I did, was port it to PHP. To run it, you must have a LaTeX distribution and the dvipng software both installed on the same machine which you will run the PHP script. Here is the PHP code: To use this code, you just have to pass the LaTeX commands through GET to the PHP. For example, adding the following string to your URL: http://localhost/imgtex.php?res=300&cmd=x=\frac{-b\pm\sqrt{-4ac}}{2a} The res variable sets the resolution for the generated image and the cmd specifies the LaTeX command. This way, the above URL will produce the following image: