Wandering again

This is "art", with capital quotes.

Log in to post a comment.

// LL 2021

Canvas.setpenopacity(1);

const density = 0.5; // min=0.01 max=2 step=0.01
const steps = 1000; // min=100 max=10000 step=1
const round = 0.1; // min=-1 max=1 step=0.01
const turn = 2.0; // min=0 max=10 step=0.1
const wobble = 0; // min=0 max=1 step=1 (No,Yes)
const draw_collision = 0; // min=0 max=1 step=1 (No,Yes)

const canvas_size = 110;

const turtle = new Turtle();

const cx = (Math.random() - 0.5) * canvas_size * 2;
const cy = (Math.random() - 0.5) * canvas_size * 2;
const ca = (Math.random() - 0.5) * Math.PI * 2;

let current_point;
let points;

function walk(i) {
    if (i==0) {
        current_point = new Point(cx, cy, density, ca);
        points = [ current_point ];
    }
    
    if (i >= steps * 100) return false;

    var r = current_point.r;
    var a = current_point.a + round / 10 * ((Math.random()<0.5) && wobble ? -1 : 1); //(Math.random() - 0.5) * 1;
    var dx = (current_point.r + r) * 2 * Math.cos(a)
    var dy = (current_point.r + r) * 2 * Math.sin(a)
    var x = current_point.x + dx;
    var y = current_point.y + dy;
    
    var new_point = new Point(x, y, r, a);
    var need_to_draw = true;
    
    if (!new_point.checkBounds() || new_point.checkCollision()) 
    {
        if (draw_collision) draw(current_point, new_point);

        const ri = Math.floor(Math.random() * points.length);
        r = points[ri].r;
        a = points[ri].a + Math.PI * ((Math.random()<0.5) && !round ? -1 : 1) / (turn ? turn : (Math.random()));
        x = points[ri].x;
        y = points[ri].y;
        
        new_point = new Point(x, y, r, a);
        need_to_draw = false;
    }
    
    points.push(new_point);
    
    if (need_to_draw) {
        draw(current_point, new_point);
        //sleep(1);
    }
    
    current_point = new_point;

    return true;    
}

function draw(p1, p2) {
    turtle.jump(p1.x, p1.y);
    turtle.goto(p2.x, p2.y);
}

class Point {
    constructor(x, y, r, a) { this.x = x; this.y = y; this.r = r; this.a = a; }
    
    checkBounds() { return (Math.abs(this.x - this.r) < canvas_size) && (Math.abs(this.y - this.r) < canvas_size); }
    
    checkCollision() {
        // TODO: optimization / spacial partitioning
        for (var i=0, l=points.length; i<l; i++) {
            //const distance = Math.hypot(points[i].x - this.x, points[i].y - this.y) / 2;
            //if (distance < this.r) return true;
            const dist2 = ((points[i].x - this.x) * (points[i].x - this.x) + (points[i].y - this.y) * (points[i].y - this.y)) / 4;
            if (dist2 < this.r * this.r) return true;
        }

        return false;
    }
}

function sleep(ms) { const start = Date.now(); while (Date.now() - start < ms); }