I am writing a program where I need to work out the centroid of a quadrilateral given the coordinates of its 4 corners. I know this can be done graphically quite easily but I was wondering if there is a formula I can use to code it?
Here's one way to do it: draw either of the diagonals, dividing the quadrilateral into two triangles. Triangles have the nice property that the centroid of a triangle is the average of the three vertices- that is, add each the x coordinates of the three triangles, the y coordinates, and the z coordinates (if this is in three dimensions) and divide by three. Those three averages will be the x, y, and z coordinates of the centroid of the triangle.
Once you have the centroids of the two triangles, the centroid of the quadrilateral is the weighted average of the two points, weighted by the area of the triangles. That is, if and are the centroid and area, respectively, of the first triangle, and and of the second triangle, then the centroid of the quadrilateral is
What a pity HallsofIvy, you almost had a beautiful answer. The weighing is not necessary, just divide the quadrilateral by one of its two diagonals like you suggested and connect the centers of gravity of both resulting triangles, i.e. their centroids, and repeat the procedure with the OTHER diagonal of the quadrilateral. The intersection of both connectors of centers of gravity must be the center of gravity of the Quadrilateral!