TExcelTAD

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

rectangleA=length⋅widthsquareA=s2parallelogramA=base⋅height(height measured perpendicular to the base)trapezoidA=12⋅(b1+b2)⋅hrhombus, kiteA=12⋅d1⋅d2(d1,d2=diagonals)\begin{aligned} \text{rectangle} &\quad A = \text{length} \cdot \text{width} \\ \text{square} &\quad A = s^2 \\ \text{parallelogram} &\quad A = \text{base} \cdot \text{height} && \text{(height measured perpendicular to the base)} \\ \text{trapezoid} &\quad A = \tfrac{1}{2} \cdot (b_1 + b_2) \cdot h \\ \text{rhombus, kite} &\quad A = \tfrac{1}{2} \cdot d_1 \cdot d_2 && (d_1, d_2 = \text{diagonals}) \end{aligned}

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 d1×d2d_1 \times d_2 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, uxvy−uyvxu_x v_y - u_y v_x, is zero, and perpendicular when the dot product uxvx+uyvyu_x v_x + u_y v_y is zero (see Vectors in Geometry):

python · try it yourself
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 (3,1)(3, 1) and (−1,3)(-1, 3) have equal length 10\sqrt{10} and dot product −3+3=0-3 + 3 = 0. 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:

python · try it yourself
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: 72+108+72+108=36072 + 108 + 72 + 108 = 360.

Exercise 2: Trapezoid area

A trapezoid has parallel sides 8 cm and 14 cm, 5 cm apart. Find its area.

Answer

55 cm255\ \text{cm}^2

Solution
A=12⋅(8+14)⋅5=12⋅22⋅5=55A = \tfrac{1}{2} \cdot (8 + 14) \cdot 5 = \tfrac{1}{2} \cdot 22 \cdot 5 = 55

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
A=12⋅10⋅24=120the diagonals bisect each other at right angles: half-diagonals 5 and 12side=52+122=13\begin{aligned} &A = \tfrac{1}{2} \cdot 10 \cdot 24 = 120 \\ &\text{the diagonals bisect each other at right angles: half-diagonals } 5 \text{ and } 12 \\ &\text{side} = \sqrt{5^2 + 12^2} = 13 \end{aligned}

Exercise 4: Box overlap

Box A spans (0,0)(0, 0) to (4,3)(4, 3) and box B spans (2,1)(2, 1) to (6,5)(6, 5). Do they overlap, and what is the area of the overlap?

Answer

Yes; overlap area 4.

Solution
x-overlap:from max⁡(0,2)=2 to min⁡(4,6)=4→width 2y-overlap:from max⁡(0,1)=1 to min⁡(3,5)=3→height 2area=2⋅2=4\begin{aligned} x\text{-overlap:} &\quad \text{from } \max(0, 2) = 2 \text{ to } \min(4, 6) = 4 && \to \text{width } 2 \\ y\text{-overlap:} &\quad \text{from } \max(0, 1) = 1 \text{ to } \min(3, 5) = 3 && \to \text{height } 2 \\ \text{area} &= 2 \cdot 2 = 4 \end{aligned}

Exercise 5: Which quadrilateral?

Classify the quadrilateral (1,1)(1, 1), (5,2)(5, 2), (6,6)(6, 6), (2,5)(2, 5) by hand, then check with classify.

Answer

A rhombus (not a square).

Solution
sides: (4,1),(1,4),(−4,−1),(−1,−4)opposite sides are negatives of each other→parallelogramall squared lengths=17→all sides equal→rhombus(4,1)⋅(1,4)=4+4=8≠0→no right angle, so not a square\begin{aligned} &\text{sides: } (4, 1), (1, 4), (-4, -1), (-1, -4) \\ &\text{opposite sides are negatives of each other} \to \text{parallelogram} \\ &\text{all squared lengths} = 17 \to \text{all sides equal} \to \text{rhombus} \\ &(4, 1) \cdot (1, 4) = 4 + 4 = 8 \ne 0 \to \text{no right angle, so not a square} \end{aligned}

Test yourself

Which statement is always true?

  1. Every rectangle is a square
  2. Every rhombus is a rectangle
  3. Every square is a rhombus
  4. Every trapezoid is a parallelogram

What is the area of a kite with diagonals 6 and 9?

  1. 54
  2. 27
  3. 15
  4. 30

Two axis-aligned boxes overlap when…

  1. their x-ranges overlap and their y-ranges overlap
  2. their x-ranges overlap or their y-ranges overlap
  3. their centres are within 1 unit
  4. they have the same area