iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🙆

Visualizing the Euclidean Algorithm with Canvas

に公開
2023/08/16

Overview

Recently, I started learning Creative Coding. I'm still at a very basic level, but it's quite a fun mental workout as it often requires mathematical thinking.

I heard that visualizing the Euclidean algorithm is interesting, so I decided to actually give it a try.

What is the Euclidean Algorithm?

Turning to Wikipedia when in doubt.

https://ja.wikipedia.org/w/index.php?curid=7942

The Euclidean algorithm (also called the Euclidean algorithm) is one of the methods for finding the greatest common divisor of two natural numbers.

Exactly as stated. It is an algorithm for finding the greatest common divisor from two natural numbers.

Steps

For natural numbers a, b (a >= b), follow these steps:

  1. Divide a by b and find the remainder c1
  2. Divide b by c1 and find the remainder c2
  3. Divide c1 by c2 and find the remainder c3
  4. Repeat this process until the remainder becomes 0. The divisor when the remainder becomes 0 is the greatest common divisor.

Concrete Example

Let's apply the Euclidean algorithm to the natural numbers 20 and 12.

20 ÷ 12 = 1 ... 8
12 ÷ 8 = 1 ... 4
8 ÷ 4 = 2 ... 0 // Ended because the remainder became 0

Since the divisor when the remainder became 0 is 4, the greatest common divisor is 4.

It's not that difficult once you try it.

Replacing with TypeScript

Let's represent the Euclidean algorithm in TypeScript.

// Prepare natural numbers a, b (a > b)
let a = 20;
let b = 12;

while (b > 0) {
  const c = Math.floor(a / b); // Quotient
  const d = a % b; // Remainder
  console.log(a + "÷" + b + "=" + c + "..." + d);
  a = b;
  b = d;
}

/*
Execution result
20÷12=1...8
12÷8=1...4
8÷4=2...0
*/

Perfect.

Thinking Geometrically

Let's think about the Euclidean algorithm geometrically.

What a ÷ b = c ... d represents

Consider a rectangle where
the length of the long side is a
the length of the short side is b

If you fill the rectangle with c squares of side length b,
a rectangle with long side b and short side d remains.

If we draw it, it looks like this:

Euclidean Algorithm Illustration

If we keep repeating the same process for the remaining rectangle, eventually the remainder (d in the image above) will become 0, meaning the entire area will be filled with squares.

In other words, by using the Euclidean algorithm, we can divide a rectangle into squares.

Rendering on Canvas

Now that we've come this far, all that's left is to try rendering it on Canvas.

TypeScript code
const canvas = document.getElementById("canvas") as HTMLCanvasElement;
const context = canvas.getContext("2d")!;

let n1 = 56; // Natural number (large)
let n2 = 36; // Natural number (small)
const scale = Math.floor((window.innerWidth / n1) * 0.8); // Coefficient for determining the size of the rectangle. Adjusted to 0.8 times the window's width.
n1 = n1 * scale;
n2 = n2 * scale;

canvas.width = n1;
canvas.height = n2;
context.fillStyle = "#ccc";
context.fillRect(0, 0, n1, n2);

let a = n1;
let b = n2;

let posX = 0,
  posY = 0;

let count = 0; // Counter for deciding whether to divide horizontally or vertically
while (b > 0) {
  const c = Math.floor(a / b); // Quotient
  const d = a % b; // Remainder
  // You can obtain as many squares as the quotient
  for (let i = 0; i < c; i++) {
    context.fillStyle = `rgb(${Math.floor(Math.random() * 256)},${Math.floor(
      Math.random() * 256
    )},${Math.floor(Math.random() * 256)})`;
    context.fillRect(posX, posY, b, b);
    if (count % 2 === 0) {
      // Divide horizontally when count is divisible by 2
      posX += b;
    } else {
      // Divide vertically when count is not divisible by 2
      posY += b;
    }
  }
  a = b;
  b = d;
  count++;
}

We were able to divide the rectangle into squares beautifully.

Summary

This was a method for visualizing the Euclidean algorithm. While it is the basic of basics, I was impressed that such interesting expressions can be created with the help of mathematics. By continuing to study Creative Coding and mathematics, I hope to become capable of even more interesting expressions.

Discussion