# 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:

• main.py

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 quadrilateralareascoordinates.py 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:

[[x1,y1],[x2,y2],[x3,y3],[x4,y4]]
'''

first = c*c + c*c + c*c + c*c

second = c*c + c*c + c*c + c*c

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:

[[x1,y1],[x2,y2],[x3,y3],[x4,y4]]

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.

main.py

def main():

print("-----------------------------------")
print("| codedrome.com                   |")
print("| Calculating Areas of            |")
print("-----------------------------------\n")

quadrilateral_1 = {"coordinates": [[6,19],[3,2],[17,5],[19,15]], "area": 0}

print()

quadrilateral_2 = {"coordinates": [[-4,9],[-7,-8],[7,-5],[9,5]], "area": 0}

main()

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:

python3 main.py

This is the output.

Program Output

-----------------------------------
| codedrome.com                   |
| Calculating Areas of            |