The Sickness

Iterations on @ge1doot's https://turtletoy.net/turtle/f70582722f

Sick in bed, but still inspired

Created by dlc on 2019/2/17
70
0

Log in to post a comment.

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(0.75);
// Global code will be evaluated once.
const turtle = new Turtle();
turtle.penup();
const points = [];
let d = 1;
for (let j = 0; j < 1; j++) {
    let x = (-75 + Math.random() * 150) * d;
    let y = (-75 + Math.random() * 150) * d;
    let a = 0;
    turtle.goto(x, y);
    turtle.down();
    for (let i = 0; i < 3000; i++) {
        let dx1 = distanceFromClosestWall([x,y])
        
        x += d * Math.cos(a) * 1;
        y += d * Math.sin(a) * 1;
        
        let dx2 = distanceFromClosestWall([x,y])
        
        let angler = (Math.random() - Math.random()) *ease(0.50)
        
        if (dx1 >= dx2 && dx2 < 50) {
            let mp = map(dx2, 100, 0, 0.01, 0.3)
            angler = Math.random() * mp
        }
        
        a += angler
        points.push([x, y]);
        turtle.goto(x, y);
    }
    d = -d;
    turtle.up();
}

function ease(p) {
  return 3*p*p - 2*p*p*p;
}

function arrayMax(arr) {
  var len = arr.length, max = -Infinity;
  while (len--) {
    if (arr[len] > max) {
      max = arr[len];
    }
  }
  return max;
};

function arrayMin(arr) {
  var len = arr.length, min = Infinity;
  while (len--) {
    if (Number(arr[len]) < min) {
      min = Number(arr[len]);
    }
  }
  return min;
};

function map (value, istart, istop, ostart, ostop) {
	return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}

function distanceFromTopWall(p0){
    return distance(p0, [p0[0], -100])
}

function distanceFromBottomWall(p0){
    return distance(p0, [p0[0], 100])
}

function distanceFromRightWall(p0){
    return distance(p0, [100, p0[1]])
}

function distanceFromLeftWall(p0){
    return distance(p0, [-100, p0[1]])
}

function distanceFromClosestWall (p0) {
    return arrayMin([
        distanceFromTopWall(p0),
        distanceFromBottomWall(p0),
        distanceFromRightWall(p0),
        distanceFromLeftWall(p0),
    ])
}

function distance (p0, p1) {
    const dx = p1[0] - p0[0];
    const dy = p1[1] - p0[1];
    return Math.sqrt(dx * dx + dy * dy)
}

// The walk function will be called until it returns false.
function walk(i) {
    const p0 = points[i];
    for (let j = i+1; j < Math.min(points.length, i+3); j++) {
        const p1 = points[j];
        const dx = p1[0] - p0[0];
        const dy = p1[1] - p0[1];
        
        if (distance(p0, p1) < 5 && Math.random() > 0.99) {
            turtle.goto([p0[0]+ 3, p0[1]+3]);
            turtle.down();
            turtle.goto([p1[0]+3, p1[1]+3]);
            turtle.up();
            
            turtle.goto([p0[0]- 3, p0[1]-3]);
            turtle.down();
            turtle.goto([p1[0]-3, p1[1]-3]);
            turtle.up();
        }
        
        if (distance(p0, p1) < 5 && Math.random() > 0.95) {
            turtle.goto([p0[0]+ 1, p0[1]+1]);
            turtle.down();
            turtle.goto([p1[0]+1, p1[1]+1]);
            turtle.up();
            
            turtle.goto([p0[0]- 1, p0[1]-1]);
            turtle.down();
            turtle.goto([p1[0]-1, p1[1]-1]);
            turtle.up();
        }
        
    }
    return i < points.length;
}