Sub topic 13
Drawing Polygons and Measuring Them on the Plane
This chapter assumes you can plot points in all four quadrants, find horizontal and vertical distances, and use the distance formula. You also need to be comfortable with absolute values and basic Python. Everything here builds on those skills. Here you use them on whole shapes instead of single points.
A polygon is an ordered list of points
A polygon is a closed shape made of straight segments. On the coordinate plane you describe one by listing its vertices (corner points) in order. Each vertex is joined to the next, and the last vertex is joined back to the first.
The order carries the shape. The same four points can give a square or a "bowtie" that crosses itself, depending on how you list them. Listing vertices by walking once around the outside is the safe habit.
| Vertex list | Shape you get |
|---|---|
| (0, 0), (2, 0), (2, 2), (0, 2) | A square |
| (0, 0), (2, 0), (0, 2), (2, 2) | A self-crossing bowtie |
Throughout the chapter, the working example is triangle ABC with A(1, 1), B(5, 1) and C(5, 4). To draw it, plot the three points, then draw A to B, B to C and C back to A.
Measuring sides
Every side is a segment between two consecutive vertices. How you measure it depends on its direction.
- A horizontal side has equal y-coordinates. Its length is the absolute difference of the x-coordinates.
- A vertical side has equal x-coordinates. Its length is the absolute difference of the y-coordinates.
- A slanted side needs the distance formula, d = √((x₂ − x₁)² + (y₂ − y₁)²).
For triangle ABC:
- AB is horizontal: |5 − 1| = 4.
- BC is vertical: |4 − 1| = 3.
- CA is slanted: √((1 − 5)² + (1 − 4)²) = √(16 + 9) = 5.
The perimeter is the sum of the side lengths, so 4 + 3 + 5 = 12. The sides 3, 4 and 5 form a Pythagorean triple, which tells you the angle at B is a right angle. The next sections use this.
Finding area
Area by decomposition
For shapes with axis-aligned sides, you can split the shape into rectangles and right triangles. Triangle ABC has legs 4 and 3 along the axes directions, so its area is ½ × 4 × 3 = 6.
An L-shape with vertices (0, 0), (4, 0), (4, 2), (2, 2), (2, 4), (0, 4) splits into a 4 × 2 rectangle and a 2 × 2 square. Its area is 8 + 4 = 12, and its perimeter is 4 + 2 + 2 + 2 + 2 + 4 = 16.
Decomposition works well by hand. It gets awkward when sides are slanted and no right triangles are obvious.
Area from coordinates alone: the shoelace formula
The shoelace formula gives the area of any simple polygon directly from its vertex list. For vertices (x₁, y₁), …, (xₙ, yₙ), take each vertex and the next one, with the last wrapping to the first:
signed area = ½ × Σ (xᵢ · yᵢ₊₁ − xᵢ₊₁ · yᵢ)
Apply it to triangle ABC. The three terms are:
- A to B: 1·1 − 5·1 = −4
- B to C: 5·4 − 5·1 = 15
- C to A: 5·1 − 1·4 = 1
The sum is 12, and half of that is 6, which matches the decomposition result.
The result is signed. If you list the vertices counterclockwise, the value is positive. If you list them clockwise, it is negative. Take the absolute value when you only want the area.
Putting it in code
The functions below work for any polygon given as a list of (x, y) tuples. math.dist handles horizontal, vertical and slanted sides in one call.
import math
def side_lengths(vertices):
n = len(vertices)
return [math.dist(vertices[i], vertices[(i + 1) % n]) for i in range(n)]
def perimeter(vertices):
return sum(side_lengths(vertices))
def signed_area(vertices):
n = len(vertices)
total = 0
for i in range(n):
x1, y1 = vertices[i]
x2, y2 = vertices[(i + 1) % n]
total += x1 * y2 - x2 * y1
return total / 2
def area(vertices):
return abs(signed_area(vertices))
triangle = [(1, 1), (5, 1), (5, 4)]
l_shape = [(0, 0), (4, 0), (4, 2), (2, 2), (2, 4), (0, 4)]
print(side_lengths(triangle))
print(perimeter(triangle), area(triangle))
print(perimeter(l_shape), area(l_shape))The output is:
[4.0, 3.0, 5.0]
12.0 6.0
16.0 12.0The (i + 1) % n expression is what closes the polygon. When i is the last index, it wraps to 0, so the last vertex connects back to the first.
Drawing the polygon
To draw a polygon with matplotlib, repeat the first vertex at the end of the coordinate lists so the outline closes.
import matplotlib.pyplot as plt
triangle = [(1, 1), (5, 1), (5, 4)]
closed = triangle + [triangle[0]]
xs = [p[0] for p in closed]
ys = [p[1] for p in closed]
fig, ax = plt.subplots()
ax.plot(xs, ys, marker="o")
for label, (x, y) in zip("ABC", triangle):
ax.annotate(f"{label}({x}, {y})", (x, y), textcoords="offset points", xytext=(5, 5))
ax.axhline(0, color="black")
ax.axvline(0, color="black")
ax.set_xlim(-1, 7)
ax.set_ylim(-1, 6)
ax.set_aspect("equal")
ax.grid(True)
plt.show()Keep set_aspect("equal"). Without it, one unit on the x-axis can be drawn longer than one unit on the y-axis. A right angle then looks like a different angle, and the shape looks distorted even though the coordinates are correct.
Reflecting a polygon
To reflect a whole polygon, reflect every vertex. Across the x-axis, (x, y) becomes (x, −y). Across the y-axis, (x, y) becomes (−x, y).
Reflecting triangle ABC across the x-axis gives A′(1, −1), B′(5, −1) and C′(5, −4). Side lengths and area stay the same, so the shape is congruent to the original. One thing does change: the vertex order flips from counterclockwise to clockwise. The code shows it:
def signed_area(vertices):
n = len(vertices)
total = 0
for i in range(n):
x1, y1 = vertices[i]
x2, y2 = vertices[(i + 1) % n]
total += x1 * y2 - x2 * y1
return total / 2
triangle = [(1, 1), (5, 1), (5, 4)]
mirrored = [(x, -y) for x, y in triangle]
print(mirrored)
print(signed_area(triangle))
print(signed_area(mirrored))This prints [(1, -1), (5, -1), (5, -4)], then 6.0, then -6.0. The area is the same in size but opposite in sign. That sign flip is a quick test for whether a transformation includes a reflection.
Checking angles and classifying shapes
You can test for a right angle at a vertex without trigonometry. Form two vectors from that vertex to its neighbours. The angle is a right angle exactly when their dot product is zero.
At B in triangle ABC, the vectors are A − B = (−4, 0) and C − B = (0, 3). The dot product is (−4)(0) + (0)(3) = 0, so the angle at B is a right angle. This agrees with the 3-4-5 side lengths.
def is_right_angle(prev_pt, corner, next_pt):
ux, uy = prev_pt[0] - corner[0], prev_pt[1] - corner[1]
vx, vy = next_pt[0] - corner[0], next_pt[1] - corner[1]
return ux * vx + uy * vy == 0
def is_rectangle(vertices):
if len(vertices) != 4:
return False
return all(
is_right_angle(vertices[i - 1], vertices[i], vertices[(i + 1) % 4])
for i in range(4)
)
print(is_right_angle((1, 1), (5, 1), (5, 4)))
print(is_rectangle([(-3, -2), (3, -2), (3, 2), (-3, 2)]))
print(is_rectangle([(0, 0), (4, 1), (5, 4), (1, 3)]))The output is True, True, False. Here vertices[i - 1] uses Python's negative indexing so that vertex 0 correctly looks back at vertex 3.
Summary of tests you can run from coordinates:
- Equal sides: compare the lengths from
side_lengths. - Right angle: dot product equals zero.
- Parallel sides: the two direction vectors have equal slope, or one is a multiple of the other.
- Rectangle: four right angles. Add equal adjacent sides to get a square.
With floating-point coordinates, compare with a tolerance such as math.isclose instead of ==. The integer examples above are safe with ==.
Common errors and edge cases
- Wrong vertex order. The square (0, 0), (2, 0), (0, 2), (2, 2) is listed in the bowtie order. The shoelace terms are 0, 4, −4 and 0, so the sum is 0 and the reported area is 0. The formula does not warn you. It assumes a simple polygon, one whose edges do not cross.
- Forgetting to close the shape. In a plot, leaving out the repeated first point gives an open path. In the shoelace sum, leaving out the wrap-around term gives a wrong area.
- Using the distance formula's square root as a final answer too early. Keep the exact form, such as √20, until the last step, and round only when you must.
- Unequal axis scales. A plot that stretches one axis hides right angles and equal lengths. Check the aspect ratio before you judge a shape by eye.
- Mixing up area and perimeter. Perimeter is a length, measured in units. Area is measured in square units.
Exercises
Exercise
A rectangle has vertices P(-3, -2), Q(3, -2), R(3, 2) and S(-3, 2). Find its side lengths, perimeter and area. Then reflect it across the y-axis and give the new vertices.
Show answer
PQ = |3 − (−3)| = 6 and QR = |2 − (−2)| = 4, so the sides are 6, 4, 6, 4. The perimeter is 6 + 4 + 6 + 4 = 20 and the area is 6 × 4 = 24. Reflecting across the y-axis changes (x, y) to (−x, y), giving P′(3, −2), Q′(−3, −2), R′(−3, 2) and S′(3, 2). The set of corners is the same as before, so the rectangle maps onto itself because it is centred on the y-axis.
Exercise
Use the shoelace formula to find the area of the quadrilateral with vertices (0, 0), (4, 0), (5, 3) and (1, 3). Is it a rectangle?
Show answer
The terms are: (0·0 − 4·0) = 0; (4·3 − 5·0) = 12; (5·3 − 1·3) = 12; (1·0 − 0·3) = 0. The sum is 24, so the area is 24 / 2 = 12. It is not a rectangle. At (4, 0) the vectors to the neighbours are (−4, 0) and (1, 3), and their dot product is −4, not 0, so the angle is not a right angle. The shape is a parallelogram with base 4 and height 3, which also gives area 12.