### las vegas lights

Adapted from a CFDG program
by mattpace, August 22th, 2006
contextfreeart.org/gallery2/#design/393

Created by ge1doot on 2019/2/10
60
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 Mat2D = class {
constructor (m) {
this.m = m;
}
rotate (v) {
const rad = Math.PI * v / 180;
const cos = Math.cos(rad);
const sin = Math.sin(rad);
return new Mat2D([
cos * this.m[0] + sin * this.m[2],
cos * this.m[1] + sin * this.m[3],
cos * this.m[2] - sin * this.m[0],
cos * this.m[3] - sin * this.m[1],
this.m[4],
this.m[5]
]);
}
translate (x, y = 0) {
return new Mat2D([
this.m[0],
this.m[1],
this.m[2],
this.m[3],
this.m[4] + x * this.m[0] + y * this.m[2],
this.m[5] + x * this.m[1] + y * this.m[3]
]);
}
scale (x = 1, y = x) {
return new Mat2D([
this.m[0] * x,
this.m[1] * x,
this.m[2] * y,
this.m[3] * y,
this.m[4],
this.m[5]
]);
}
size () {
const x = this.m[0] * this.m[0] + this.m[1] * this.m[1];
const y = this.m[2] * this.m[2] + this.m[3] * this.m[3];
return Math.sqrt(Math.max(x, y));
}
transform (x, y) {
const m0 = this.m[0] * zoom;
const m1 = this.m[1] * zoom;
const m2 = this.m[2] * zoom;
const m3 = this.m[3] * zoom;
const m4 = this.m[4] * zoom - ox;
const m5 = this.m[5] * zoom - oy;
return [
m0 * x + m2 * y + m4,
m1 * x + m3 * y + m5
];
}
boundingBox (box) {
const p0 = this.transform(0, 0);
const p1 = this.transform(0.5, 0);
const p2 = this.transform(0.5, 0.5);
const p3 = this.transform(0, 0.5);
const minx = Math.min(p0[0], p1[0], p2[0], p3[0]);
const maxx = Math.max(p0[0], p1[0], p2[0], p3[0]);
const miny = Math.min(p0[1], p1[1], p2[1], p3[1]);
const maxy = Math.max(p0[1], p1[1], p2[1], p3[1]);
if (minx < box[0]) box[0] = minx; else if (maxx > box[2]) box[2] = maxx;
if (miny < box[1]) box[1] = miny; else if (maxy > box[3]) box[3] = maxy;
}
};
///////////////////////////////////////////////////////
const shapes =  [];
let zoom = 1, ox = 0, oy = 0;
const box = [0, 0, 0, 0];
const rect = m => {
m.boundingBox(box);
shapes.push(m);
};
const draw = shape => {
// strokeRect
turtle.goto(shape.transform(-0.5, -0.5));
turtle.down();
turtle.goto(shape.transform( 0.5, -0.5));
turtle.goto(shape.transform( 0.5,  0.5));
turtle.goto(shape.transform(-0.5,  0.5));
turtle.goto(shape.transform(-0.5, -0.5));
turtle.up();
}
const scale = (margin = 0.95) => {
zoom = Math.min(
margin * 200 / (box[2] - box[0]),
margin * 200 / (box[3] - box[1])
);
ox = (box[0] + box[2]) * 0.5 * zoom;
oy = (box[3] + box[1]) * 0.5 * zoom;
};
//
//
//
//
/////////////////////////CFDG ///////////////////////////
const minSize = 0.1;
const RUN = m => {
if (m.size() < minSize) return;
const r = Math.random() * 50;
let weight = 0;
switch (true) {
//even more rarely, split off...
case r <= (weight += 0.5):
SHAPE(m);
RUN(m);
return SHARPRIGHT(m);
case r <= (weight += 0.5):
SHAPE(m);
RUN(m);
return SHARPLEFT(m);
case r <= (weight += 0.5):
SHAPE(m);
RUN(m);
return RUNRIGHT(m);
case r <= (weight += 0.5):
SHAPE(m);
RUN(m);
return RUNLEFT(m);
//every once in awhile, start a turn
case r <= (weight += 2):
SHAPE(m);
return SHARPRIGHT(m.translate(0, 4).rotate(-10).scale(0.997));
case r <= (weight += 2):
SHAPE(m);
return SHARPLEFT(m.translate(0, 4).rotate(10).scale(0.997));
case r <= (weight += 2):
SHAPE(m);
return RUNRIGHT(m.translate(0, 4).rotate(-10).scale(0.997));
case r <= (weight += 2):
SHAPE(m);
return RUNLEFT(m.translate(0, 4).rotate(10).scale(0.997));
default:
SHAPE(m);
return RUN(m.translate(0, 4).scale(0.997));
}
}
//the turns
const RUNLEFT = m => {
if (m.size() < minSize) return;
const r = Math.random() * 201;
let weight = 0;
switch (true) {
case r <= (weight += 1):
SHAPE(m);
return RUN(m.translate(0, 4).scale(0.997));
default:
SHAPE(m);
return RUN(m.translate(0, 4).rotate(10).scale(0.997));
}
}
const RUNRIGHT = m => {
if (m.size() < minSize) return;
const r = Math.random() * 201;
let weight = 0;
switch (true) {
case r <= (weight += 1):
SHAPE(m);
return RUN(m.translate(0, 4).scale(0.997));
default:
SHAPE(m);
return  RUN(m.translate(0, 4).rotate(-10).scale(0.997));
}
}
const SHARPLEFT = m => {
if (m.size() < minSize) return;
const r = Math.random() * 201;
let weight = 0;
switch (true) {
case r <= (weight += 1):
SHAPE(m);
return RUN(m.translate(0, 4).scale(0.997));
default:
SHAPE(m);
return SHARPLEFT(m.translate(0, 4).rotate(20).scale(0.997));
}
}
const SHARPRIGHT = m => {
if (m.size() < minSize) return;
const r = Math.random() * 201;
let weight = 0;
switch (true) {
case r <= (weight += 1):
SHAPE(m);
return RUN(m.translate(0, 4).scale(0.997));
default:
SHAPE(m);
return SHARPRIGHT(m.translate(0, 4).rotate(-20).scale(0.997));
}
}
const SHAPE = m => {
rect(m.scale(0.45, 3));
}

/////////////////// render scene //////////////////////////////
//
//
//
//
RUN(new Mat2D([1, 0, 0, -1, 0, 0]));
scale(0.95);

// The walk function will be called until it returns false.
function walk(i) {
const m = shapes.pop();
if (!m) return false;
draw(m);
return true;
}```