Calculating Areas of Quadrilaterals from Coordinates in Python

In this, the last of my trilogy on calculating the areas of quadrilaterals, I will demonstrate how to use the coordinates of the four corners.

The Project

This project consists of the following files:



The files can be downloaded as a zip, or you can clone/download the Github repository if you prefer.

Source Code Links

ZIP File

Example Quadrilaterals

The diagrams below show the two quadrilaterals I will be using as examples. The corners or vertices are numbered 1-4, and their x,y coordinates are also shown.

The quadrilaterals are identical (congruent) and only differ in their coordinates.

The Formula

This is the formula for calculating the area of a quadrilateral from the x,y coordinates of its corners. The numbers 1-4 correspond to the corner numbering in the diagrams, and the x and y refer to the horizontal and vertical coordinates respectively.

The formula requires only multiplication, addition and subtraction - basic arithmetic - but the large number of similar terms make the formula difficult to grok. To make things easier the following diagram shows how the two sets of inner brackets are calculated.

This is the entire process:

  • Multiply each of the pairs of numbers connected by green lines, and add up the results

  • Repeat with the numbers connected by red lines

  • Subtract the second result from the first

  • Multiply by 0.5

The Code

This is which contains the function to implement the above formula.

def calculate_area(c):

    Calculates the area of any quadrivaletral
    from a list of x,y coordinates in the following format:


    first = c[0][0]*c[1][1] + c[1][0]*c[2][1] + c[2][0]*c[3][1] + c[3][0]*c[0][1]

    second = c[1][0]*c[0][1] + c[2][0]*c[1][1] + c[3][0]*c[2][1] + c[0][0]*c[3][1]

    area = 0.5*(first - second)

    return area

As you can see from the docstring, the single argument to calculate_area is a list of lists in this format:


We then carry out the calculations represented by the sets of inner brackets. To obtain individual values we use an expression in the format c[a][b] where a is the corner (starting from 0 rather than 1 of course), and b is 0 for x and 1 for y.

I don't often use single letter variable names but if I had called the argument coordinates these two lines of code would have been ridiculously long!

Lastly we just subtract the second result from the first and multiply by 0.5.

Now let's try the function out on the two example quadrilaterals.

import quadrilateralareascoordinates

def main():

    print("|                   |")
    print("| Calculating Areas of            |")
    print("| Quadrilaterals from Coordinates |")

    quadrilateral_1 = {"coordinates": [[6,19],[3,2],[17,5],[19,15]], "area": 0}
    quadrilateral_1["area"] = quadrilateralareascoordinates.calculate_area(quadrilateral_1["coordinates"])


    quadrilateral_2 = {"coordinates": [[-4,9],[-7,-8],[7,-5],[9,5]], "area": 0}
    quadrilateral_2["area"] = quadrilateralareascoordinates.calculate_area(quadrilateral_2["coordinates"])


For each quadrilateral I have created a dictionary containing the coordinates in the required format and also an area set to 0. The areas are then set with calls to calculate_area, and finally the dictionary is printed.

Running the Program

Now we can run the program like this:


This is the output.

Program Output

|                   |
| Calculating Areas of            |
| Quadrilaterals from Coordinates |

{'coordinates': [[6, 19], [3, 2], [17, 5], [19, 15]], 'area': 183.5}
{'coordinates': [[-4, 9], [-7, -8], [7, -5], [9, 5]], 'area': 183.5}