Bricks 🧱

Use the slider to make variations.

Uses hatching util from Stars by @reinder

Log in to post a comment.

const turtle = new Turtle();

const seed = 50; //// min=1, max=100, step=1
const rows = 24; // min=4, max=50, step=1
const size = 90; // min=10, max=100, step=1
let itemMinSize = 5; // min=1, max=50, step=1
let itemMaxSize = 20; // min=1, max=50, step=1
const space = 2; // min=1, max=20, step=1

const hatch = 0; // min=0, max=4, step=0.1
const hatchDirection = 2; // min=0, max=4, step=1 (Random, Horizontal/Vertical, Diagonal, Radial, Gradient)
const hatchFill = 0; // min=0, max=3, step=1 (Solid, Random, Gradient, Radial)

const border = 0;  // min=0, max=5, step=0.25
const distortion = 0; // min=0, max=1, step=0.01

if (itemMinSize > itemMaxSize) {
    [itemMinSize, itemMaxSize] = [itemMaxSize, itemMinSize];
}

function walk(i) {
    let height = size / (rows * 0.5) - space;
    let mod = i % 2;
    let x = -size + space * 0.5;
    let y = space * 0.5 + -size + i * (height + space);
    while (x < size - space  - itemMaxSize) {
        let width = itemMinSize + Math.random() * (itemMaxSize - itemMinSize);
        drawBrick([x, y, width, height], mod++);
        x += width + space;
    }
    drawBrick([x, y, size - space *0.5 - x, height], mod++); // closing brick
    return i < rows - 1;
}

function drawBrick(rect, idx = 0) {
    let [x,y,w,h] = rect;
    const rectPoints = [[x,y],[x+w,y],[x+w,y+h],[x,y+h]];
    const randomizedPoints = randomizePoints(rectPoints, distortion);
    if (border) {
        const borderPoints = [[x-border,y-border],[x+w+border,y-border],[x+w+border,y+h+border],[x-border,y+h+border]];
        drawPoints(randomizePoints(borderPoints, distortion), turtle);
    }
    drawPoints(randomizedPoints, turtle);
    if (hatch > 0) {
        let util = new Polygons();
        let p = util.create();
        randomizedPoints.forEach(pt => p.addPoints(pt));
        
        let angle;
        if (hatchDirection === 0) angle = Math.random() * Math.PI*2;
        else if (hatchDirection === 1) angle = (idx % 2)/2 * Math.PI + Math.PI/2;
        else if (hatchDirection === 2) angle = (idx % 2)/2 * Math.PI+ Math.PI/4;
        else if (hatchDirection === 3) angle = -Math.atan2(y+h*0.5, x+w*0.5);
        else if (hatchDirection === 4) angle = Math.abs((x+w/2)/size)*Math.PI/2 + Math.PI/2;
        
        let fill;
        if (hatchFill === 0) fill = hatch + 0.5;
        else if (hatchFill === 1) fill = 0.5 + Math.random()*hatch;
        else if (hatchFill === 2) fill = 0.5 + hatch *Math.abs((x+w/2)/size)/2;
        else if (hatchFill === 3) fill = 0.5 + hatch * Math.pow(Math.abs(Math.atan2(y+h*0.5, x+w*0.5)/3),2);
        
        p.addHatching(angle, fill);
        util.draw(turtle,p);
    }
}

function drawPoints(points, turtle) {
    if (points.length > 0) turtle.jump(points[points.length-1]);
    points.forEach(point => turtle.goto(point));
}


function scl2(a,b)   { return [a[0]*b, a[1]*b]; }
function add2(a,b)   { return [a[0]+b[0], a[1]+b[1]]; }
function sub2(a,b)   { return [a[0]-b[0], a[1]-b[1]]; }
function dot2(a,b)   { return a[0]*b[0] + a[1]*b[1]; }

function randomizePoints(points, scale = 1) {
    return points.map( point => add2(point, scl2( add2( [hash(dot2(point, [11, 13])), hash(dot2(point, [17, 19]))], [-.5,-.5]), scale)));
}


// pseudo random methods
function hash(p) {
    p += seed;
    p = 1103515245 * (((p) >> 1) ^ (p));
    p = 1103515245 * (p ^ (p>>3));
    p = p ^ (p >> 16);
    return p / 1103515245 % 1;	
}

let rseed = seed;
function rand() {
    let r = 1103515245 * (((rseed) >> 1) ^ (rseed++));
    r = 1103515245 * (r ^ (r>>3));
    r = r ^ (r >> 16);
    return r / 1103515245 % 1;	
}

////////////////////////////////////////////////////////////////
// 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);
		}
	};
}