Quadrilaterals
A quadrilateral is a polygon with four sides. Squares, rectangles and parallelograms form a family tree where each member inherits the properties of its parent. Rectangles are also the most common shape in software: every window, button, sprite and bounding box is one.
The family
Every quadrilateral's angles add up to 360° (a diagonal splits it into two triangles).
| Shape | Definition | Extra properties |
|---|---|---|
| trapezoid (trapezium in British English) | exactly one pair of parallel sides | the parallel sides are the bases |
| parallelogram | both pairs of opposite sides parallel | opposite sides equal, opposite angles equal, neighbouring angles add to 180°, diagonals bisect each other |
| rectangle | parallelogram with a right angle | all angles 90°, diagonals equal |
| rhombus | parallelogram with all sides equal | diagonals perpendicular and bisect the angles |
| square | rectangle and rhombus at once | all of the above |
| kite | two pairs of equal neighbouring sides | diagonals perpendicular, one is bisected |
So every square is a rectangle and a rhombus, every rectangle is a parallelogram, but not the other way round. (Some books let a trapezoid have "at least one" pair of parallel sides, which makes parallelograms trapezoids too. Here we use "exactly one".)
Area formulas
The trapezoid formula is the average of the two bases times the height. The diagonal formula works because perpendicular diagonals cut the shape into four right triangles that fit inside a rectangle, filling exactly half of it.
Classifying from coordinates
With vectors the definitions become tests. Two sides are parallel when the 2D cross product of their direction vectors, , is zero, and perpendicular when the dot product is zero (see Vectors in Geometry):
def classify(p):
sides = [(p[(i + 1) % 4][0] - p[i][0], p[(i + 1) % 4][1] - p[i][1]) for i in range(4)]
cross = lambda u, v: u[0] * v[1] - u[1] * v[0]
dot = lambda u, v: u[0] * v[0] + u[1] * v[1]
sq = [dot(s, s) for s in sides] # squared lengths, exact for integers
par1 = cross(sides[0], sides[2]) == 0
par2 = cross(sides[1], sides[3]) == 0
if par1 and par2:
right = dot(sides[0], sides[1]) == 0
equal = len(set(sq)) == 1
return "square" if right and equal else "rectangle" if right else "rhombus" if equal else "parallelogram"
if par1 or par2:
return "trapezoid"
if (sq[0] == sq[1] and sq[2] == sq[3]) or (sq[1] == sq[2] and sq[3] == sq[0]):
return "kite"
return "general quadrilateral"
shapes = {
"A": [(0, 0), (4, 0), (4, 3), (0, 3)],
"B": [(0, 0), (3, 1), (2, 4), (-1, 3)],
"C": [(0, 0), (5, 0), (7, 3), (2, 3)],
"D": [(0, 0), (5, 0), (8, 4), (3, 4)],
"E": [(0, 0), (6, 0), (4, 3), (1, 3)],
"F": [(0, 0), (2, -1), (5, 0), (2, 1)],
"G": [(0, 0), (4, 0), (5, 2), (1, 5)],
}
for name, pts in shapes.items():
print(name, classify(pts))A rectangle
B square
C parallelogram
D rhombus
E trapezoid
F kite
G general quadrilateral
Shape B is a tilted square: its sides and have equal length and dot product . Using squared lengths keeps all arithmetic in integers, so == is safe.
In software: axis-aligned bounding boxes
An axis-aligned bounding box (AABB) is the smallest rectangle with sides parallel to the axes that contains a shape. It is stored as (xmin, ymin, xmax, ymax). Games and UI toolkits test boxes first because the test is just four comparisons, and only run expensive exact tests when boxes overlap:
def bbox(points):
xs, ys = [p[0] for p in points], [p[1] for p in points]
return (min(xs), min(ys), max(xs), max(ys))
def overlap(a, b):
return a[0] < b[2] and b[0] < a[2] and a[1] < b[3] and b[1] < a[3]
def overlap_area(a, b):
w = min(a[2], b[2]) - max(a[0], b[0])
h = min(a[3], b[3]) - max(a[1], b[1])
return max(w, 0) * max(h, 0)
ship = bbox([(1, 2), (4, 1), (3, 5)])
rock = bbox([(3, 4), (6, 4), (5, 7)])
coin = bbox([(7, 0), (8, 1)])
print("ship:", ship, " rock:", rock)
print("ship/rock overlap:", overlap(ship, rock), " area:", overlap_area(ship, rock))
print("ship/coin overlap:", overlap(ship, coin), " area:", overlap_area(ship, coin))ship: (1, 1, 4, 5) rock: (3, 4, 6, 7)
ship/rock overlap: True area: 1
ship/coin overlap: False area: 0
Two boxes overlap exactly when their x-ranges overlap and their y-ranges overlap. Boxes that merely touch along an edge count as not overlapping here because of the strict <. More in AABB and Collision Geometry.
Note: A bounding box overlap does not prove the shapes touch, only that they might. It is a fast filter, called the broad phase in physics engines.
Exercises
Exercise 1: Parallelogram angles
One angle of a parallelogram is 72°. Find the other three.
Answer
108°, 72°, 108°
Solution
Neighbouring angles add to 180°, so the next angle is 108°; opposite angles are equal. Check: .
Exercise 2: Trapezoid area
A trapezoid has parallel sides 8 cm and 14 cm, 5 cm apart. Find its area.
Answer
Solution
Exercise 3: Rhombus from diagonals
A rhombus has diagonals 10 and 24. Find its area and its side length.
Answer
Area 120, side 13.
Solution
Exercise 4: Box overlap
Box A spans to and box B spans to . Do they overlap, and what is the area of the overlap?
Answer
Yes; overlap area 4.
Solution
Exercise 5: Which quadrilateral?
Classify the quadrilateral , , , by hand, then check with classify.
Answer
A rhombus (not a square).
Solution
Test yourself
Which statement is always true?
- Every rectangle is a square
- Every rhombus is a rectangle
- Every square is a rhombus
- Every trapezoid is a parallelogram
What is the area of a kite with diagonals 6 and 9?
- 54
- 27
- 15
- 30
Two axis-aligned boxes overlap when…
- their x-ranges overlap and their y-ranges overlap
- their x-ranges overlap or their y-ranges overlap
- their centres are within 1 unit
- they have the same area