can I get code in p5 that generates an apollonian gasket
- User
can I get code in p5 that generates an apollonian gasket
- Assistant
can I get code in p5 that generates an apollonian gasket
Certainly! Below is a p5.js code that generates an Apollonian gasket. This code uses recursion and Descartes' Theorem to calculate the curvatures of tangent circles and places them appropriately to create the gasket.
You can run this code in the p5.js web editor by copying and pasting it there.
// Apollonian Gasket in p5.js
let maxDepth = 5; // Controls the depth of recursion
function setup() {
createCanvas(800, 800);
noLoop();
}
function draw() {
background(255);
stroke(0);
noFill();
// Define the initial three mutually tangent circles
let c1 = new Circle(width / 2, height / 2, 300);
let c2 = new Circle(width / 2 - 300, height / 2, 150);
let c3 = new Circle(width / 2 + 300, height / 2, 150);
// Draw the initial circles
c1.display();
c2.display();
c3.display();
// Start recursive drawing of tangent circles
apollonian(c1, c2, c3, maxDepth);
apollonian(c2, c3, c1, maxDepth);
apollonian(c3, c1, c2, maxDepth);
}
function apollonian(c1, c2, c3, depth) {
if (depth <= 0) return;
let newCircle = getInnerCircle(c1, c2, c3);
if (newCircle != null) {
newCircle.display();
apollonian(newCircle, c1, c2, depth - 1);
apollonian(newCircle, c2, c3, depth - 1);
apollonian(newCircle, c3, c1, depth - 1);
}
}
function getInnerCircle(c1, c2, c3) {
// Calculate curvatures
let k1 = 1.0 / c1.r;
let k2 = 1.0 / c2.r;
let k3 = 1.0 / c3.r;
let s = k1 + k2 + k3;
let k4 = s + 2 * sqrt(k1 * k2 + k2 * k3 + k3 * k1);
// Calculate positions using complex numbers
let z1 = math.complex(c1.x, c1.y);
let z2 = math.complex(c2.x, c2.y);
let z3 = math.complex(c3.x, c3.y);
let numerator = math.add(
math.add(
math.multiply(k1, z1),
math.multiply(k2, z2)
),
math.multiply(k3, z3)
);
let denominator = k1 + k2 + k3;
let z4 = math.divide(numerator, denominator);
// Adjust for the circle inversion (choose the smaller circle)
let r4 = 1.0 / k4;
if (r4 < 0) {
k4 = s - 2 * sqrt(k1 * k2 + k2 * k3 + k3 * k1);
r4 = 1.0 / k4;
}
return new Circle(z4.re, z4.im, r4);
}
class Circle {
constructor(x, y, r) {
this.x = x;
this.y = y;
this.r = abs(r);
}
display() {
ellipse(this.x, this.y, this.r * 2);
}
}
Explanation:
Circle Class:
(x, y) and radius r.display() method draws the circle on the canvas using ellipse().Setup and Draw Functions:
setup() initializes the canvas and sets noLoop() to prevent continuous redrawing.draw() sets the background, stroke, and initializes three mutually tangent circles (c1, c2, c3).apollonian() is called for each combination.Apollonian Function:
maxDepth).getInnerCircle() to compute a new circle tangent to c1, c2, and c3.apollonian() with the new circle and pairs of the existing circles.getInnerCircle Function:
k = 1/r) of each circle.z = x + yi) of the new circle's center.
Additional Notes:
Math.js Library:
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/9.4.4/math.min.js"></script>
Adjusting Recursion Depth:
maxDepth controls how many levels of circles are drawn.Curvature and Circle Relations:
Running the Code:
sketch.js file.math.js library:
sketch.js file.Visual Output:
The code will generate an Apollonian gasket similar to this:
[image: Apollonian Gasket Example] open
Understanding the Math:
Descartes' Theorem:
Complex Numbers for Circle Centers:
Customizing the Gasket:
maxDepth variable to control the level of detail.Troubleshooting:
maxDepth value.I hope this helps you generate an Apollonian gasket using p5.js! If you have any questions or need further clarification, feel free to ask.
Not yet included in a published release.