This is the first in a set of three articles describing how to calculate the areas of various types of quadrilaterals using the Python programming language.

The project consists of the following files:

- quadrilateralareas.py
- main.py

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

Source Code Links

## Quadrilaterals and Their Areas

The types of quadrilateral we'll look at and calculate the areas of are:

- square
- rectangle
- parallelogram
- trapezium
- rhombus
- kite

## Square

Of course it's pretty silly describing what a square is and how to calculate its area but I have included it for completeness.

The diagram below shows a square with the dimensions I'll use in the sample code. (I haven't included any particular units in these examples as they are superfluous.)

The six types of quadrilateral listed above are not completely separate or exclusive, and some are special cases of others. A square can, if you wish, be regarded as a special case of all of the other five quadrilaterals with equal length sides and four angles of 90°

## Rectangle

Another very simple shape: the humble rectangle. All four angles are 90° but the two pairs of opposite sides are not necessarily equal in length. Again of course we just multiply the width and height to get the area.

## Parallelogram

Things are getting slightly more interesting: a parallelogram which has two pairs of parallel sides. To calculate the area we multiply the length of one side by the height, NOT the length of the other side.

The square and rectangle are special cases of a parallelogram with 90° angles, as is the rhombus which I'll look at further down.

## Trapezium or Trapezoid

The trapezium has two parallel sides called bases, the other sides being known as legs.

The area of a trapezium is calculated using this formula . . .

0.5 * height * ( width_1 + width_2 )

. . . which in this case is:

0.5 * 12 * ( 12 + 16 ) = 168

You might be expecting me to say that the square, rectangle and rhombus are special cases of a trapezium but strangely mathematicians can't agree on this!

## Rhombus

A rhombus has four equal length sides like a square but not necessarily equal angles. This is how we calculate the area:

0.5 * height * width

= 0.5 * 24 * 10

= 120

## Kite

Lastly we have a kite. (Please feel free to print this diagram, cut it out and attach a piece of string. Aeronautical capabilities not guaranteed.) It has two pairs of equal length sides.

This is how we calculate the area.

0.5 * height * width

= 0.5 * 40 * 24 = 480

Similar to the trapezium, whether squares and rhombuses/rhombi are special cases of a kite is a matter of opinion.

## The Code

The code for this project is very simple and consists of six one-line functions to implement the formulas shown above, and a few bits of code to try them out. This is the file containing the six functions.

quadrilateralareas.py

def square(side):

return side ** 2

def rectangle(width, height):

return width * height

def parallelogram(side, height):

return side * height

def trapezium(height, width_1, width_2):

return 0.5 * height * ( width_1 + width_2 )

def rhombus(height, width):

return 0.5 * height * width

def kite(height, width):

return 0.5 * height * width

And here we have a file with a main function to demonstrate the functions.

main.py

import quadrilateralareas

def main():

print("---------------------------")

print("| codedrome.com |")

print("| Areas of Quadrilaterals |")

print("---------------------------\n")

square = { "side": 12, "area": 0 }

square["area"] = quadrilateralareas.square(square["side"])

print(f"square: {square}")

print()

rectangle = { "width": 16, "height": 8, "area": 0 }

rectangle["area"] = quadrilateralareas.rectangle(rectangle["width"], rectangle["height"])

print(f"rectangle: {rectangle}")

print()

parallelogram = { "side": 12, "height": 12, "area": 0 }

parallelogram["area"] = quadrilateralareas.parallelogram(parallelogram["side"], parallelogram["height"])

print(f"parallelogram: {parallelogram}")

print()

trapezium = { "width_1": 12, "width_2": 16, "height": 12, "area": 0 }

trapezium["area"] = quadrilateralareas.trapezium(trapezium["width_1"], trapezium["width_2"], trapezium["height"],)

print(f"trapezium: {trapezium}")

print()

rhombus = { "height": 24, "width": 10, "area": 0 }

rhombus["area"] = quadrilateralareas.rhombus(rhombus["height"], rhombus["width"])

print(f"rhombus: {rhombus}")

print()

kite = { "height": 40, "width": 24, "area": 0 }

kite["area"] = quadrilateralareas.kite(kite["height"], kite["width"])

print(f"kite: {kite}")

main()

In each case I have created a dictionary containing the values needed to calculate the area, as well as an area initialized to 0. This is set using a call to the relevant function, and finally the dictionary in printed.

Let's run the code:

python3 main.py

Which will give us this output:

Program Output

---------------------------

| codedrome.com |

| Areas of Quadrilaterals |

---------------------------

square: {'side': 12, 'area': 144}

rectangle: {'width': 16, 'height': 8, 'area': 128}

parallelogram: {'side': 12, 'height': 12, 'area': 144}

trapezium: {'width_1': 12, 'width_2': 16, 'height': 12, 'area': 168.0}

rhombus: {'height': 24, 'width': 10, 'area': 120.0}

kite: {'height': 40, 'width': 24, 'area': 480.0}

## Whatâ€™s Next?

I mentioned that this is the first of a trilogy. In the sequels I will cover:

- Calculating the area of any quadrilateral using Bretschneider's Formula
- Calculating the area of quadrilaterals from the coordinates of their corners