xxxxxxxxxx
94
function setup() {
createCanvas(512,512);
}
function makeRandomPattern()
{
ts = [];
let num = int(random(2,4));
for( let idx = 0; idx < num; ++idx ) {
ts.push( [random(TWO_PI),random(0.1,5),random(TWO_PI)])
}
let x = 0;
let y = 0;
let res = [];
for( let idx = 0; idx < 10000; ++idx ) {
res.push( [x,y] );
let t = map( idx, 0, 10000, 0, 2*TWO_PI );
let theta = 0.0;
for( ps of ts ) {
theta += ps[0]*sin(ps[1]*t + ps[2]);
}
// console.log( theta );
x += cos( theta );
y += sin( theta );
}
res.push( [x,y] );
// console.log( res );
return res;
}
function draw() {
background(128);
const pt = makeRandomPattern();
let xmin = pt[0][0];
let xmax = pt[0][0];
let ymin = pt[0][1];
let ymax = pt[0][1];
for( let p of pt ) {
xmin = min( xmin, p[0] );
xmax = max( xmax, p[0] );
ymin = min( ymin, p[1] );
ymax = max( ymax, p[1] );
}
let sc = min( width / (xmax-xmin), height / (ymax-ymin) );;
noFill();
push();
translate( width/2, height/2 );
scale( sc );
translate( -0.5*(xmin+xmax), -0.5*(ymin+ymax) );
strokeWeight( 0.5 / sc );
for( let idx = 0; idx < 50000; ++idx ) {
let i = int(random(pt.length-1));
let p = pt[i];
let q = pt[i+1];
let d = dist( p[0], p[1], q[0], q[1] ) / (50*abs(randomGaussian(0,1)));
let dx = (q[0]-p[0])/d;
let dy = (q[1]-p[1])/d;
stroke( 0, 5 );
line( p[0], p[1], p[0] - dy, p[1] + dx );
stroke(255,5);
line( p[0], p[1], p[0] + dy, p[1] - dx );
}
stroke( 0 );
strokeWeight( 2 / sc );
beginShape();
for( let p of pt ) {
vertex( p[0], p[1] );
}
endShape();
pop();
noLoop();
}
function keyPressed()
{
if( key == ' ' ) {
loop();
} else if( key == 's' ) {
save( "output.png" );
}
}