Example Code:
// Plot basic complex numbers
plot(new Complex(3, 2), 'red');
plot(new Complex(-1, -1), 'blue');
plot(new Complex(0, 2), 'green');
// Plot a unit circle
for (let theta = 0; theta <= 2 * Math.PI; theta += 0.1) {
const x = Math.cos(theta);
const y = Math.sin(theta);
plot(new Complex(x, y), 'purple');
}
// Plot a complex function z² + 1
function f(z) {
return z.multiply(z).add(new Complex(1, 0));
}
// Plot a grid and apply the function to each point
plotGrid(-2, 2, -2, 2, 0.5);
plotFunction(f, -2, 2, -2, 2, 0.5, 'red');
// Plot complex exponential
function complexExp(z) {
return exp(z);
}
// Plot original grid points
plotGrid(-2, 2, -2, 2, 0.2, '#aaa');
// Plot transformed points
plotFunction(complexExp, -2, 2, -2, 2, 0.2, 'blue');
// Mandelbrot Set preview (simplified)
function mandelbrot(c, maxIterations = 20) {
let z = new Complex(0, 0);
let iterations = 0;
while (iterations < maxIterations && z.magnitude() < 2) {
z = z.multiply(z).add(c);
iterations++;
}
if (iterations === maxIterations) {
return true; // Point is in the set
}
return false; // Point escaped
}
// Check if points are in the Mandelbrot set
for (let x = -2; x <= 0.5; x += 0.05) {
for (let y = -1.5; y <= 1.5; y += 0.05) {
const c = new Complex(x, y);
if (mandelbrot(c)) {
plot(c, 'black');
}
}
}