Heart ❤️

Follow your heart

Forked from "Stars" by @reinder Stars

Log in to post a comment.

// Forked from "Stars" by reinder
// https://turtletoy.net/turtle/a5befa1f8d

// Transforms. Created by Reinder Nijhoff 2019 - @reindernijhoff
// The MIT License
//
// I have cleaned up the code of my 'polygon clipping algorithm', as used in a lot
// of my turtles. The algorithm is pretty naive and far from perfect, but it will
// work most of the time :)
//
// You can use this utility to draw polygons. The lines of a polygon (this can be
// the outline, but you're free to add extra segments of hatching to the polygon), will
// be clipped with all polygons that are already drawn.
//
// https://turtletoy.net/turtle/a5befa1f8d
//

const turtle = new Turtle();
const polygons = new Polygons();

const total = 250;
function walk(i) {
    drawThing(turtle, i, total);
    return i < total - 1;
}

const hatchAngle = Math.PI*2*Math.random();
const sizes = [1.0, 0.33, 0.45, 0.66];
function drawThing(turtle,ii,total) {
    const p1 = polygons.create(), p2 = polygons.create();
    
    let x = 200*Math.random()-100, y = 200*Math.random()-100,
          a = Math.random() * Math.PI/2, b = 2+Math.random()*5|0, c = 1+Math.random()*3|0, s = 10 + 10 * Math.random();
    
    if (ii == 0) {
        x = 0,  y=0, s=35;
        
        const cp = []; // create clip points that define the shape of the polygon
        a = Math.PI/2;
        var heart = [0.7, 0.6, 0.6, 0.6, 0.7, 0.8, 0.9, 0.9, 0.62];
        for (let i=0; i<heart.length*2; i++) {
           const l = s * heart[(i > 8) ? 16-i : i];
            cp.push( [x+Math.cos(a+i/8*Math.PI)*l, y+Math.sin(a+i/8*Math.PI)*l]);  
        }
        
        p1.addPoints(...cp);
        p2.addPoints(...cp.map(p => [p[0]+2, p[1]+6])); // shadow offset
        
        const hatchiness = 2 + Math.random() * 7.5;
        if (Math.random() > 0.5) p1.addHatching(hatchAngle, hatchiness);
        if (Math.random() > 0.5) p1.addHatching(hatchAngle + Math.PI/2, hatchiness);
        p1.addOutline(); // polygon 1, star with outline
        p2.addHatching(-Math.PI*.25, 0.75); // polygon 2, star shadow
        
        polygons.draw(turtle, p1, true); 
        polygons.draw(turtle, p2, false);
    } else if (Math.sqrt(x * x + y * y) > 65) {
        const sizes = [1, .45, .66];
        const cp = []; // create clip points that define the shape of the polygon
        var sides = Math.random() * 6 | 0 + 4;
        for (let i=0; i<sides*2; i++) {
            //const l = s * sizes[Math.random()* sizes.length | 0];
            const l = s * (i % 4 ? 1 : (i % 2 ? .33 : .66) );
            cp.push( [x+Math.cos(a+i/sides*Math.PI)*l, y+Math.sin(a+i/sides*Math.PI)*l]);  
        }
        
        p1.addPoints(...cp);
        p2.addPoints(...cp.map(p => [p[0]+3, p[1]+3])); // shadow offset
        
        p1.addOutline(); // polygon 1, star with outline
        p2.addHatching(hatchAngle, 1); // polygon 2, star shadow
        if (ii > total*2/3) p2.addHatching(hatchAngle + Math.PI/2, 1); // polygon 2, star shadow
        
        if (ii < total/2) {
            polygons.draw(turtle, p1, true); 
            polygons.draw(turtle, p2, false);
        } else {
            polygons.draw(turtle, p2, false);
            polygons.draw(turtle, p1, true);
        }
    }
}


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