Bamboo tree forest
Log in to post a comment.
const turtle = new Turtle();
const polygons = new Polygons();
const total = 20; //min=5,max=80,step=1
const seed = 1; //min=1,max=100,step=1
const random = new Random(seed);
const order = [...Array(total).keys()];
random.shuffle(order);
function walk(i) {
let x = -95 + (order[i] / total) * 200;
let y = random.range(100, 150);
const size = random.range(22, 25);
const dir = random.range(-0.02, 0.02);
let angle = random.range(-0.1, 0.1);
let trunkInterval = random.range(3, 10) | 0;
let c = 0;
while (y > -100) {
c ++;
x += Math.sin(angle) * size;
y -= Math.cos(angle) * size;
if (random.next() > 0.8) {
const leafs = [];
if (random.next() > 0.4) leafs.push(() => drawLeaf(turtle, x + random.range(-2,0), y, Math.PI + random.range(0,0.5)));
if (random.next() > 0.4) leafs.push(() => drawLeaf(turtle, x + random.range(-2,0), y, Math.PI + random.range(0.5,0.85)));
if (random.next() > 0.4) leafs.push(() => drawLeaf(turtle, x + random.range(0,2), y, -random.range(0,0.5)));
if (random.next() > 0.4) leafs.push(() => drawLeaf(turtle, x + random.range(0,2), y, -random.range(0.5,0.85)));
if (random.next() > 0.1) leafs.push(() => drawLeaf(turtle, x, y, -Math.PI/2 + random.range(-0.2, 0.2)));
random.shuffle(leafs);
leafs.forEach(leaf => leaf());
}
if (y < 25 && c % trunkInterval == 0) {
drawTrunkWithLeaves(turtle, x, y + 1)
}
drawStump(turtle, x, y, lerp(7, 1, i / total), size, angle);
if (random.next() > 0.95) drawLeaf(turtle, x, y, 0.7 * random.next());
if (random.next() > 0.95) drawLeaf(turtle, x, y, -0.7 * random.next());
if (y < 50 && random.next() > 0.9) {
drawTrunkWithLeaves(turtle, x, y + 1, angle + random.either(Math.PI/2, -Math.PI/2))
}
angle += dir;
}
return i < total;
}
function drawTrunkWithLeaves(turtle, x, y, startAngle, depth = 0) {
const size = depth == 0 ? random.range(0.8, 1.3) : (depth == 1 ? random.range(0.6, 0.9) : random.range(0.3, 0.6));
const length = depth == 0 ? random.range(10,25) : random.range(5,10);
const dir = random.range(0.02, 0.03) * random.either(-1, 1);
let angle = Math.PI + random.range(0.4, 1.3) * random.either(-1, 1);;
const line = polygons.create();
//const translated = (p) => transform(p, x, y, 1, 1, -angle);
let leafScale = 1.0;
if (depth == 1) leafScale = 0.85;
else if (depth == 2) leafScale = 0.5;
let c = 0;
while (c < length) {
x += Math.sin(angle) * size;
y += Math.cos(angle) * size;
const t = 1-c/length;
let thickness = lerp(0.5, 0.25, t*t);
let x1 = x + Math.sin(angle + Math.PI/2) * thickness * leafScale;
let y1 = y + Math.cos(angle + Math.PI/2) * thickness * leafScale;
line.addPoints([x1,y1]);
if (depth < 2 && c > length / 6 && c < length - length / 4 && c % 4 == 1) {
if (random.next() > 0.7) drawTrunkWithLeaves(turtle, x, y, angle + (Math.PI/2 * random.either(-1, 1)), depth + 1);
}
if (c > length / 3 && c < length - length / 10 && c % 8 == 2) {
if (random.either()) drawLeaf(turtle, x, y, angle + random.range(-0.5, -0.5), random.range(0.4, 0.7) * leafScale )
if (random.either()) drawLeaf(turtle, x, y, angle + Math.PI + random.range(0.5, 0.5), random.range(0.4, 0.7) * leafScale )
}
angle += dir;
c++;
}
const leafs = [];
if (random.either()) {
leafs.push(() =>drawLeaf(turtle, x, y, angle - Math.PI/2 + random.range(-0.8, -0.3), random.range(0.4, 0.7) * leafScale, 0));
leafs.push(() =>drawLeaf(turtle, x, y, angle - Math.PI/2 + random.range(0.3, 0.8), random.range(0.4, 0.7) * leafScale, 0));
}
else {
leafs.push(() =>drawLeaf(turtle, x, y, angle - Math.PI/2 + random.range(0.1, -0.1), random.range(0.6, 0.7) * leafScale, 0));
}
if (random.either()) {
leafs.push(() => drawLeaf(turtle, x, y, angle - Math.PI/2 + random.range(0.1, -0.1), random.range(0.6, 0.7) * leafScale, 0));
}
random.shuffle(leafs);
leafs.forEach(leaf => leaf());
while (c > 0) {
x -= Math.sin(angle) * size;
y -= Math.cos(angle) * size;
const t = c/length;
let thickness = lerp(0.5, 0.25, t*t);
let x2 = x + Math.sin(angle - Math.PI / 2) * thickness * leafScale;
let y2 = y + Math.cos(angle - Math.PI / 2) * thickness * leafScale;
line.addPoints([x2,y2]);
angle -= dir;
c--;
}
line.addOutline();
polygons.draw(turtle, line);
}
function drawStump(turtle, x, y, width, height, angle) {
const stump = polygons.create();
const translated = (p) => transform(p, x, y, 1, 1, -angle);
stump.addPoints(
translated([-width / 2 - 0.65, 0]),
translated([width / 2 + 0.65, 0]),
translated([width / 2, height]),
translated([-width / 2, height]),
translated([-width / 2 - 0.65, 0])
);
stump.addOutline();
polygons.draw(turtle, stump);
}
function drawLeaf(turtle, x, y, angle, scaleX = 0.7 + random.range(-0.1, -0.1), maxTrunkSize = 5) {
const scaleY = scaleX;
const translated = (p) => transform(p, x, y, scaleX, scaleY, angle);
const leaf = polygons.create();
const leafTrunkSize = random.range(0, maxTrunkSize);
leaf.addPoints(
translated([0, 0.5]),
translated([leafTrunkSize, 0.5]),
translated([leafTrunkSize + 1, -2]),
translated([leafTrunkSize + 3, -3]),
translated([leafTrunkSize + 4, -3]),
translated([leafTrunkSize + 7, -3]),
translated([leafTrunkSize + 10, -2]),
translated([leafTrunkSize + 15, -1]),
translated([leafTrunkSize + 20, 0]),
translated([leafTrunkSize + 15, 1]),
translated([leafTrunkSize + 10, 2]),
translated([leafTrunkSize + 5, 3]),
translated([leafTrunkSize + 4, 3]),
translated([leafTrunkSize + 2, 3]),
translated([leafTrunkSize + 1, 2]),
translated([leafTrunkSize, 0]),
translated([0, 0]),
translated([20, 0]),
translated([0, 0]),
);
leaf.addOutline();
polygons.draw(turtle, leaf);
}
function transform(p, x, y, scaleX, scaleY, angle) {
scale(p, scaleX, scaleY);
rotate(p, angle);
translate(p, x, y);
return p;
}
function rotate(p, a) {
const [px, py] = p;
p[0] = px * Math.cos(a) + py * Math.sin(a);
p[1] = py * Math.cos(a) - px * Math.sin(a);
return p;
}
function translate(p, x, y) {
p[0] += x;
p[1] += y;
return p;
}
function scale(p, x, y) {
p[0] *= x;
p[1] *= y;
return p;
}
function lerp(a, b, t) {
return a + (b - a) * t;
}
// Seeded random - Mulberry32
function Random(seed) {
class Random {
constructor(seed) {
this.seed = seed;
}
next() {
var t = this.seed += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
}
range(from, to) {
var r = this.next();
return from + (to - from) * r;
}
either(a = 0, b = 1, chance = 0.5) {
return this.next() > chance ? a : b;
}
shuffle(arr) {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(this.next() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
}
return new Random(seed);
}
////////////////////////////////////////////////////////////////
// Polygon Clipping utility code - Created by Reinder Nijhoff 2019
// https://turtletoy.net/turtle/a5befa1f8d
////////////////////////////////////////////////////////////////
function Polygons() {
const polygonList = [];
const Polygon = class {
constructor() {
this.cp = []; // clip path: array of [x,y] pairs
this.dp = []; // 2d lines [x0,y0],[x1,y1] to draw
this.aabb = []; // AABB bounding box
}
addPoints(...points) {
// add point to clip path and update bounding box
let xmin = 1e5, xmax = -1e5, ymin = 1e5, ymax = -1e5;
(this.cp = [...this.cp, ...points]).forEach( p => {
xmin = Math.min(xmin, p[0]), xmax = Math.max(xmax, p[0]);
ymin = Math.min(ymin, p[1]), ymax = Math.max(ymax, p[1]);
});
this.aabb = [(xmin+xmax)/2, (ymin+ymax)/2, (xmax-xmin)/2, (ymax-ymin)/2];
}
addSegments(...points) {
// add segments (each a pair of points)
points.forEach(p => this.dp.push(p));
}
addOutline() {
for (let i = 0, l = this.cp.length; i < l; i++) {
this.dp.push(this.cp[i], this.cp[(i + 1) % l]);
}
}
draw(t) {
for (let i = 0, l = this.dp.length; i < l; i+=2) {
t.jump(this.dp[i]), t.goto(this.dp[i + 1]);
}
}
addHatching(a, d) {
const tp = new Polygon();
tp.cp.push([-1e5,-1e5],[1e5,-1e5],[1e5,1e5],[-1e5,1e5]);
const dx = Math.sin(a) * d, dy = Math.cos(a) * d;
const cx = Math.sin(a) * 200, cy = Math.cos(a) * 200;
for (let i = 0.5; i < 150 / d; i++) {
tp.dp.push([dx * i + cy, dy * i - cx], [dx * i - cy, dy * i + cx]);
tp.dp.push([-dx * i + cy, -dy * i - cx], [-dx * i - cy, -dy * i + cx]);
}
tp.boolean(this, false);
this.dp = [...this.dp, ...tp.dp];
}
inside(p) {
let int = 0; // find number of i ntersection points from p to far away
for (let i = 0, l = this.cp.length; i < l; i++) {
if (this.segment_intersect(p, [0.1, -1000], this.cp[i], this.cp[(i + 1) % l])) {
int++;
}
}
return int & 1; // if even your outside
}
boolean(p, diff = true) {
// bouding box optimization by ge1doot.
if (Math.abs(this.aabb[0] - p.aabb[0]) - (p.aabb[2] + this.aabb[2]) >= 0 &&
Math.abs(this.aabb[1] - p.aabb[1]) - (p.aabb[3] + this.aabb[3]) >= 0) return this.dp.length > 0;
// polygon diff algorithm (narrow phase)
const ndp = [];
for (let i = 0, l = this.dp.length; i < l; i+=2) {
const ls0 = this.dp[i];
const ls1 = this.dp[i + 1];
// find all intersections with clip path
const int = [];
for (let j = 0, cl = p.cp.length; j < cl; j++) {
const pint = this.segment_intersect(ls0, ls1, p.cp[j], p.cp[(j + 1) % cl]);
if (pint !== false) {
int.push(pint);
}
}
if (int.length === 0) {
// 0 intersections, inside or outside?
if (diff === !p.inside(ls0)) {
ndp.push(ls0, ls1);
}
} else {
int.push(ls0, ls1);
// order intersection points on line ls.p1 to ls.p2
const cmpx = ls1[0] - ls0[0];
const cmpy = ls1[1] - ls0[1];
int.sort( (a,b) => (a[0] - ls0[0]) * cmpx + (a[1] - ls0[1]) * cmpy -
(b[0] - ls0[0]) * cmpx - (b[1] - ls0[1]) * cmpy);
for (let j = 0; j < int.length - 1; j++) {
if ((int[j][0] - int[j+1][0])**2 + (int[j][1] - int[j+1][1])**2 >= 0.001) {
if (diff === !p.inside([(int[j][0]+int[j+1][0])/2,(int[j][1]+int[j+1][1])/2])) {
ndp.push(int[j], int[j+1]);
}
}
}
}
}
return (this.dp = ndp).length > 0;
}
//port of http://paulbourke.net/geometry/pointlineplane/Helpers.cs
segment_intersect(l1p1, l1p2, l2p1, l2p2) {
const d = (l2p2[1] - l2p1[1]) * (l1p2[0] - l1p1[0]) - (l2p2[0] - l2p1[0]) * (l1p2[1] - l1p1[1]);
if (d === 0) return false;
const n_a = (l2p2[0] - l2p1[0]) * (l1p1[1] - l2p1[1]) - (l2p2[1] - l2p1[1]) * (l1p1[0] - l2p1[0]);
const n_b = (l1p2[0] - l1p1[0]) * (l1p1[1] - l2p1[1]) - (l1p2[1] - l1p1[1]) * (l1p1[0] - l2p1[0]);
const ua = n_a / d;
const ub = n_b / d;
if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) {
return [l1p1[0] + ua * (l1p2[0] - l1p1[0]), l1p1[1] + ua * (l1p2[1] - l1p1[1])];
}
return false;
}
};
return {
list: () => polygonList,
create: () => new Polygon(),
draw: (turtle, p, addToVisList=true) => {
for (let j = 0; j < polygonList.length && p.boolean(polygonList[j]); j++);
p.draw(turtle);
if (addToVisList) polygonList.push(p);
}
};
}
////////////////////////////////////////////////////////////////
// Tortoise utility code (Minimal Turtle and Transforms)
// https://turtletoy.net/turtle/102cbd7c4d
////////////////////////////////////////////////////////////////
function Tortoise(x, y) {
class Tortoise extends Turtle {
constructor(x, y) {
super(x, y);
this.ps = Array.isArray(x) ? [...x] : [x || 0, y || 0];
this.transforms = [];
}
addTransform(t) {
this.transforms.push(t);
this.jump(this.ps);
return this;
}
applyTransforms(p) {
if (!this.transforms) return p;
let pt = [...p];
this.transforms.map(t => { pt = t(pt); });
return pt;
}
goto(x, y) {
const p = Array.isArray(x) ? [...x] : [x, y];
const pt = this.applyTransforms(p);
if (this.isdown() && (this.pt[0]-pt[0])**2 + (this.pt[1]-pt[1])**2 > 4) {
this.goto((this.ps[0]+p[0])/2, (this.ps[1]+p[1])/2);
this.goto(p);
} else {
super.goto(pt);
this.ps = p;
this.pt = pt;
}
}
position() { return this.ps; }
}
return new Tortoise(x,y);
}