Points, lines, angles and areas

Plane geometry works on a flat surface. A position on it is a point, written with coordinates (x, y): how far right and how far up from a chosen origin (0, 0). Screens work the same way, with one twist: pixel coordinates usually start at the top-left corner and y grows downward.

Where two lines meet they form an angle, measured in degrees:

  • a full turn is 360^\circ;
  • a straight angle (a straight line) is 180^\circ;
  • a right angle, marked with a small square, is 90^\circ; lines that meet at a right angle are perpendicular;
  • angles below 90° are acute, between 90° and 180° obtuse.

The three angles of any triangle add up to 180^\circ. You can see why by drawing a line through one corner, parallel to the opposite side: the three angles then sit side by side along that straight line.

The standard areas and lengths:

Shape Area Perimeter
Rectangle, width w and height h w \cdot h 2w + 2h
Triangle, base b and height h \tfrac{1}{2} b h sum of the three sides
Circle, radius r \pi r^2 2 \pi r (the circumference)

Here \pi \approx 3.14159 is the ratio of a circle's circumference to its diameter, the same for every circle. A triangle's height is measured perpendicular to the base, so a triangle is half of a rectangle with the same base and height.

Worked example: a Wi-Fi access point in Kestrel's warehouse covers a circle with a radius of about 15 m in open space. The area it covers is \pi \cdot 15^2 = 225\pi \approx 706.9 m², and the edge of that circle is 2\pi \cdot 15 \approx 94.2 m long. Notice that doubling the radius quadruples the area: area grows with the square of the radius.

import math

r = 15                                   # coverage radius in meters
print(f"area: {math.pi * r ** 2:.1f} m2")
print(f"circumference: {2 * math.pi * r:.1f} m")
area: 706.9 m2
circumference: 94.2 m

The Pythagorean theorem

A right triangle has one angle of 90°. The side opposite that angle is the longest, and is called the hypotenuse; the other two sides are the legs. The Pythagorean theorem says:

a^2 + b^2 = c^2

"The square of the hypotenuse equals the sum of the squares of the legs." With legs 3 and 4: 9 + 16 = 25, so the hypotenuse is \sqrt{25} = 5.

Why it is true

Take four copies of the right triangle and place them inside a square with side a + b, one in each corner, with the hypotenuses facing inward. They leave a tilted square in the middle with side c. Now count the area of the big square in two ways:

  1. As one square: (a + b)^2 = a^2 + 2ab + b^2.
  2. As four triangles plus the middle square: 4 \cdot \tfrac{1}{2}ab + c^2 = 2ab + c^2.
  3. Both describe the same area, so a^2 + 2ab + b^2 = 2ab + c^2. Subtract 2ab from both sides: a^2 + b^2 = c^2.

The theorem also works backward: if the sides of a triangle satisfy a^2 + b^2 = c^2, the angle opposite c is a right angle. Builders use a rope with knots at 3, 4 and 5 units to set out a square corner.

Worked example: a cable run

A cable must run in a straight line diagonally across a server room floor from one corner to the opposite one. The room is 8 m long and 6 m wide.

  1. The walls meet at a right angle, so the diagonal is the hypotenuse: c^2 = 8^2 + 6^2 = 64 + 36 = 100.
  2. Take the square root: c = \sqrt{100} = 10 m.
  3. Sanity check: the diagonal must be longer than the longest wall (8 m) and shorter than the two walls together (14 m). 10 m fits.

Python's math.hypot computes \sqrt{a^2 + b^2} directly, and math.dist the distance between two points (more on that below):

import math

print(math.hypot(8, 6))                  # cable diagonal across an 8 m by 6 m room
print(math.dist((1, 2), (4, 6)))
print(math.dist((0, 0, 0), (2, 3, 6)))
10.0
5.0
7.0

The same calculation gives a screen's diagonal from its width and height, in inches or in pixels; it is worked out on Pixel density.

Degrees and radians

Degrees divide a full turn into 360 parts, a choice that goes back to Babylonian astronomy. Mathematics and every programming language's math library use a more natural unit. A radian is the angle at the center of a circle where the arc it cuts off is exactly as long as the radius. A full circle has a circumference of 2\pi r, so it holds 2\pi radians:

360^\circ = 2\pi \text{ rad} \qquad 180^\circ = \pi \text{ rad}

To convert, multiply by the ratio that cancels the unit you have:

\text{radians} = \text{degrees} \cdot \frac{\pi}{180} \qquad \text{degrees} = \text{radians} \cdot \frac{180}{\pi}

For example 90^\circ = 90 \cdot \pi/180 = \pi/2 radians, and 1 radian is 180/\pi \approx 57.3^\circ. Radians are usually written without a unit; an angle with no degree sign is in radians.

Degrees 0° 30° 45° 60° 90° 180° 360°
Radians 0 \pi/6 \pi/4 \pi/3 \pi/2 \pi 2\pi

Radians make formulas simpler. The length of an arc is just s = r\theta (radius times angle in radians), with no factor 360 anywhere. Python has math.radians() and math.degrees() for the conversion:

import math

print(math.radians(180))
print(math.degrees(math.pi / 2))
print(math.sin(math.radians(30)))
print(round(math.sin(math.radians(30)), 10))
print(math.cos(math.pi / 3), math.tan(math.pi / 4))
3.141592653589793
90.0
0.49999999999999994
0.5
0.5000000000000001 0.9999999999999999

The exact answers are 0.5, 0.5 and 1. The small errors appear because \pi itself cannot be stored exactly as a binary float, so the angle is already very slightly off before the sine is computed (see Floating-Point Numbers).

Sine, cosine and tangent

In a right triangle, pick one of the two acute angles and call it \theta (theta). The legs now get names relative to that angle: the opposite side lies across from it, the adjacent side touches it. The three basic trigonometric ratios are:

\sin \theta = \frac{\text{opposite}}{\text{hypotenuse}} \qquad \cos \theta = \frac{\text{adjacent}}{\text{hypotenuse}} \qquad \tan \theta = \frac{\text{opposite}}{\text{adjacent}}

The sine of the angle is the opposite side divided by the hypotenuse; the cosine is the adjacent side divided by the hypotenuse; the tangent is opposite divided by adjacent. A common memory aid is SOH-CAH-TOA. Because all right triangles with the same angle \theta have the same shape, these ratios depend only on the angle, not on the size of the triangle.

A right triangle with legs 4 (horizontal, adjacent to angle theta) and 3 (vertical, opposite angle theta), and the hypotenuse 5 opposite the right angle. Angle theta is at the left corner and is about 36.87 degrees. θ b = 4 (adjacent to θ) a = 3 (opposite θ) c = 5 (hypotenuse) 90°
For the angle θ, side a is opposite, side b adjacent and c the hypotenuse: sin θ = 3/5, cos θ = 4/5, tan θ = 3/4.

In the 3-4-5 triangle, \sin\theta = 3/5 = 0.6, \cos\theta = 4/5 = 0.8 and \tan\theta = 3/4 = 0.75. To get the angle back from a ratio, use the inverse functions \sin^{-1} (arcsine, math.asin), \cos^{-1} (math.acos) or \tan^{-1} (math.atan):

import math

# 3-4-5 triangle: opposite 3, adjacent 4, hypotenuse 5
print(3 / 5, 4 / 5, 3 / 4)
print(round(math.degrees(math.asin(3 / 5)), 2))
print(round(math.degrees(math.atan(3 / 4)), 2))
0.6 0.8 0.75
36.87
36.87

Worked example: a ladder against a rack row

A technician sets a 4 m ladder against a wall at an angle of about 75° with the floor, close to the 76° of the common ladder-safety rule (one unit out for every four units up). How high does it reach, and how far is its foot from the wall?

  1. The ladder is the hypotenuse (4 m). The height is the side opposite the 75° angle, the distance to the wall the adjacent side.
  2. From \sin\theta = \text{opposite}/\text{hypotenuse}: height = 4 \cdot \sin 75^\circ \approx 4 \cdot 0.966 \approx 3.86 m.
  3. From \cos\theta = \text{adjacent}/\text{hypotenuse}: distance = 4 \cdot \cos 75^\circ \approx 4 \cdot 0.259 \approx 1.04 m.
  4. Check with Pythagoras: 3.86^2 + 1.04^2 \approx 14.9 + 1.08 \approx 16 = 4^2. Correct, within rounding.
import math

length, angle = 4.0, math.radians(75)    # a 4 m ladder at 75 degrees
print(f"top reaches {length * math.sin(angle):.2f} m high")
print(f"foot stands {length * math.cos(angle):.2f} m from the wall")
top reaches 3.86 m high
foot stands 1.04 m from the wall

Exact values for the special angles

A few angles have exact values that are worth recognizing. They come from two triangles: half a square (45°, 45°, 90°, with sides 1, 1, \sqrt{2}) and half an equilateral triangle (30°, 60°, 90°, with sides 1, \sqrt{3}, 2).

θ 0° 30° 45° 60° 90°
sin θ 0 \tfrac{1}{2} \tfrac{\sqrt{2}}{2} \approx 0.707 \tfrac{\sqrt{3}}{2} \approx 0.866 1
cos θ 1 \tfrac{\sqrt{3}}{2} \tfrac{\sqrt{2}}{2} \tfrac{1}{2} 0
tan θ 0 \tfrac{\sqrt{3}}{3} \approx 0.577 1 \sqrt{3} \approx 1.732 not defined

The cosine row is the sine row backward, because the two acute angles of a right triangle add up to 90°: \cos\theta = \sin(90^\circ - \theta). The tangent of 90° is not defined, because the adjacent side would be 0 and you cannot divide by 0.

The unit circle

A right triangle only has angles up to 90°. To give sine and cosine a meaning for every angle, use the unit circle: the circle with radius 1 around the origin. Measure the angle \theta from the positive x-axis, counterclockwise. The point where the angle's radius meets the circle is

(\cos\theta, \sin\theta)

For angles between 0° and 90° this matches the triangle definition: the radius is a hypotenuse of length 1, so the adjacent side is the cosine and the opposite side is the sine. Beyond 90° the definition simply continues around the circle.

A circle of radius 1 around the origin, with tick marks at the special angles. A radius at an angle theta of 30 degrees from the positive x-axis ends at the point (cos theta, sin theta), about (0.866, 0.5). The horizontal distance from the origin is the cosine, the height of the point is the sine. x y 1 −1 1 −1 45° 60° θ = 30° (cos θ, sin θ) ≈ (0.866, 0.5) sin θ = 0.5 cos θ ≈ 0.866 1
On the unit circle the cosine is the horizontal coordinate and the sine the vertical coordinate of the point at angle θ.

Four consequences follow directly from the picture:

  • Range: sine and cosine are always between −1 and 1, because the circle has radius 1.
  • Signs: strictly between 90° and 270° the cosine is negative; strictly between 180° and 360° the sine is negative.
  • Periodicity: after a full turn you are back at the same point, so \sin(\theta + 360^\circ) = \sin\theta and the same for cosine. Their graphs are waves that repeat every 2\pi.
  • The Pythagorean identity: the point lies on a circle of radius 1, so by Pythagoras \sin^2\theta + \cos^2\theta = 1 for every angle. And the tangent is \tan\theta = \sin\theta / \cos\theta, the slope of the radius.
import math

# Pythagorean identity holds (up to rounding) for any angle
for deg in (0, 30, 45, 60, 90, 123):
    t = math.radians(deg)
    print(deg, math.isclose(math.sin(t) ** 2 + math.cos(t) ** 2, 1.0))
0 True
30 True
45 True
60 True
90 True
123 True

math.isclose compares with a small tolerance: with floats, == would sometimes say False although the mathematics says the values are equal.

From a point back to its angle: atan2

Going the other way, from a point (x, y) to its angle, \tan^{-1}(y/x) is not enough: (1, 1) and (-1, -1) have the same ratio y/x = 1 but point in opposite directions, and x = 0 would divide by zero. The two-argument function atan2(y, x), available in almost every language, looks at the signs of both coordinates and returns the angle in the correct quadrant, between −180° and 180°:

import math

# Direction of a vector (x, y) with atan2, in degrees
for x, y in ((1, 0), (1, 1), (0, 1), (-1, 0), (0, -1)):
    print((x, y), round(math.degrees(math.atan2(y, x)), 1))
(1, 0) 0.0
(1, 1) 45.0
(0, 1) 90.0
(-1, 0) 180.0
(0, -1) -90.0

Note the argument order: y first, then x.

Distances in two and more dimensions

The distance between two points follows from Pythagoras. For P = (1, 2) and Q = (4, 6):

  1. The horizontal difference is 4 - 1 = 3, the vertical difference 6 - 2 = 4.
  2. These are the legs of a right triangle whose hypotenuse is the straight line from P to Q.
  3. Distance = \sqrt{3^2 + 4^2} = \sqrt{25} = 5.

In general the Euclidean distance, the straight-line distance, between (x_1, y_1) and (x_2, y_2) is

d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}

In three dimensions you add a third squared difference, for the height; applying Pythagoras twice shows why. From (0, 0, 0) to (2, 3, 6): \sqrt{4 + 9 + 36} = \sqrt{49} = 7. Nothing stops you from continuing: with points of n coordinates each,

d(\mathbf{p}, \mathbf{q}) = \sqrt{\sum_{i=1}^{n} (p_i - q_i)^2}

"the square root of the sum, over every coordinate i from 1 to n, of the squared difference". The space of all such points is written \mathbb{R}^n. Nobody can picture 1,000 dimensions, but the formula works exactly the same, and math.dist (in the example in the Pythagoras section above) accepts any number of coordinates.

That is not an academic exercise. AI systems turn texts into embeddings: lists of hundreds or thousands of numbers, one point per text. Similar texts get nearby points, so finding related documents comes down to finding the nearest points (see Embeddings), and the k-nearest neighbors algorithm classifies a new case by the classes of its closest neighbors (see k-nearest neighbors). Lengths, angles between such points and cosine similarity are on Vectors and Matrices.

Where geometry and trigonometry show up in IT

  • Displays: screen diagonals, aspect ratios and pixel density (see Aspect ratio).
  • Graphics and user interfaces: rotating a point (x, y) by an angle \theta around the origin gives (x\cos\theta - y\sin\theta,\ x\sin\theta + y\cos\theta); that is what a CSS rotate() or a game engine computes for every corner.
  • Data centers and offices: cable lengths, Wi-Fi coverage areas, the reach of a camera's field of view.
  • Signals: alternating current and radio waves are sine waves; phase differences are angles.
  • AI and search: distances and angles between embeddings in hundreds of dimensions.
import math

def rotate(x, y, degrees):
    t = math.radians(degrees)
    return (round(x * math.cos(t) - y * math.sin(t), 6),
            round(x * math.sin(t) + y * math.cos(t), 6))

print(rotate(1, 0, 90))
print(rotate(1, 0, 30))
print(rotate(2, 1, 180))
(0.0, 1.0)
(0.866025, 0.5)
(-2.0, -1.0)

Rotating (1, 0) by 30° lands exactly on the unit-circle point (\cos 30^\circ, \sin 30^\circ). The rotation formula written as a matrix is on Vectors and Matrices.