In digital design and image processing, an aspect ratio represents the proportional relationship between a width and a height. A screen with a width of 1920 pixels and a height of 1080 pixels has an aspect ratio of 16:9. But how do we simplify large, complex resolution coordinates into these simple, core integer proportions?
Euclid's Greatest Common Divisor Algorithm
The mathematical backbone of ratio simplification is the Greatest Common Divisor (GCD). The GCD of two integers is the largest positive integer that divides both numbers without a remainder. AspectRatio uses the ancient Euclidean Algorithm, which computes the GCD of two numbers $A$ and $B$ recursively:
For example, let's simplify a standard 1080p resolution grid (1920 × 1080):
- $\text{GCD}(1920, 1080) = \text{GCD}(1080, 1920 \pmod{1080}) = \text{GCD}(1080, 840)$
- $\text{GCD}(1080, 840) = \text{GCD}(840, 1080 \pmod{840}) = \text{GCD}(840, 240)$
- $\text{GCD}(840, 240) = \text{GCD}(240, 840 \pmod{240}) = \text{GCD}(240, 120)$
- $\text{GCD}(240, 120) = \text{GCD}(120, 240 \pmod{120}) = \text{GCD}(120, 0) = 120$
The Greatest Common Divisor is 120. Dividing both dimensions by 120 yields:
Thus, we arrive at the simplified aspect ratio of 16:9. By calculating the GCD instantly inside the client-side calculator, we are able to preserve proportional coordinates when scaling images to fit different targets.