Skip to content

Commit f9a5f25

Browse files
committed
changed example to use mandelbrot
1 parent 690c8d9 commit f9a5f25

1 file changed

Lines changed: 72 additions & 35 deletions

File tree

index.html

Lines changed: 72 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -194,44 +194,81 @@ <h1>C++ Mode for<br><span class="proc">Processing</span></h1>
194194
<div class="examples-section">
195195
<h2>Examples</h2>
196196
<div class="examples-grid">
197-
<a class="example-card" href="/examples/tree.html" id="card1">
198-
<iframe class="example-canvas" id="c1" width="300" height="300" style="width:100%;aspect-ratio:1;border:none;display:block;background:#000;" scrolling="no"></iframe><script>(function(){const iframe=document.getElementById('c1');const doc=iframe.contentDocument||iframe.contentWindow.document;doc.open();doc.write(`<!DOCTYPE html><html><head><style>*{margin:0;padding:0;}body{overflow:hidden;}</style><script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.0/p5.min.js"><\/script></head><body><script>let theta;
199-
200-
function setup() {
197+
<a class="example-card" href="/examples/mandelbrot.html" id="card1">
198+
<iframe class="example-canvas" id="c1" width="300" height="300" style="width:100%;aspect-ratio:1;border:none;display:block;background:#000;" scrolling="no"></iframe><script>(function(){const iframe=document.getElementById('c1');const doc=iframe.contentDocument||iframe.contentWindow.document;doc.open();doc.write(`<!DOCTYPE html><html><head><style>*{margin:0;padding:0;}body{overflow:hidden;}</style><script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.0/p5.min.js"><\/script></head><body><script>function setup() {
201199
createCanvas(300,300);
200+
pixelDensity(1);
201+
noLoop();
202+
background(255);
203+
204+
let w = 4;
205+
let h = (w * height) / width;
206+
207+
let xmin = -w / 2;
208+
let ymin = -h / 2;
209+
210+
loadPixels();
211+
212+
let maxiterations = 100;
213+
214+
let xmax = xmin + w;
215+
let ymax = ymin + h;
216+
217+
let dx = (xmax - xmin) / width;
218+
let dy = (ymax - ymin) / height;
219+
220+
let y = ymin;
221+
for (let j = 0; j < height; j++) {
222+
let x = xmin;
223+
for (let i = 0; i < width; i++) {
224+
225+
let a = x;
226+
let b = y;
227+
let n = 0;
228+
let maxAbs = 4.0;
229+
let absOld = 0.0;
230+
let convergeNumber = maxiterations;
231+
232+
while (n < maxiterations) {
233+
let aa = a * a;
234+
let bb = b * b;
235+
let absVal = sqrt(aa + bb);
236+
if (absVal > maxAbs) {
237+
let diffToLast = absVal - absOld;
238+
let diffToMax = maxAbs - absOld;
239+
convergeNumber = n + diffToMax / diffToLast;
240+
break;
241+
}
242+
let twoab = 2.0 * a * b;
243+
a = aa - bb + x;
244+
b = twoab + y;
245+
n++;
246+
absOld = absVal;
247+
}
248+
249+
let idx = (i + j * width) * 4;
250+
if (n == maxiterations) {
251+
pixels[idx] = 0;
252+
pixels[idx + 1] = 0;
253+
pixels[idx + 2] = 0;
254+
pixels[idx + 3] = 255;
255+
} else {
256+
let norm = map(convergeNumber, 0, maxiterations, 0, 1);
257+
let bright = map(sqrt(norm), 0, 1, 0, 255);
258+
pixels[idx] = bright;
259+
pixels[idx + 1] = bright;
260+
pixels[idx + 2] = bright;
261+
pixels[idx + 3] = 255;
262+
}
263+
x += dx;
264+
}
265+
y += dy;
266+
}
267+
updatePixels();
202268
}
203269
204-
function draw() {
205-
background(0);
206-
frameRate(30);
207-
stroke(255);
208-
let a = (mouseX / width) * 90;
209-
theta = radians(a);
210-
translate(width / 2, height);
211-
line(0, 0, 0, -120);
212-
translate(0, -120);
213-
branch(120);
214-
}
215-
216-
function branch(h) {
217-
h *= 0.66;
218-
if (h > 2) {
219-
push();
220-
rotate(theta);
221-
line(0, 0, 0, -h);
222-
translate(0, -h);
223-
branch(h);
224-
pop();
225-
226-
push();
227-
rotate(-theta);
228-
line(0, 0, 0, -h);
229-
translate(0, -h);
230-
branch(h);
231-
pop();
232-
}
233-
}<\/script></body></html>`);doc.close();})();</script>
234-
<div class="example-info"><h3>Tree</h3><p>Fractals And L-Systems example</p></div>
270+
function draw() {}<\/script></body></html>`);doc.close();})();</script>
271+
<div class="example-info"><h3>Mandelbrot</h3><p>Fractals And L-Systems example</p></div>
235272
</a>
236273
<a class="example-card" href="/examples/rotate-push-pop.html" id="card2">
237274
<iframe class="example-canvas" id="c2" width="300" height="300" style="width:100%;aspect-ratio:1;border:none;display:block;background:#000;" scrolling="no"></iframe><script>(function(){const iframe=document.getElementById('c2');const doc=iframe.contentDocument||iframe.contentWindow.document;doc.open();doc.write(`<!DOCTYPE html><html><head><style>*{margin:0;padding:0;}body{overflow:hidden;}</style><script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.0/p5.min.js"><\/script></head><body><script>let a = 0;

0 commit comments

Comments
 (0)