Spiral clock

Show multiple days worth of time on a single clock! It makes perfect sense if you don't think about it too hard.

Log in to post a comment.

// LL 2021

const hours = -1; // min=-1 max=11 step=1
const minutes = -1; // min=-1 max=59 step=1
const seconds = -1; // min=-1 max=59 step=1
const draw_seconds = 2; // min=0 max=2 step=1 (Yes, No, Maybe)
const draw_hatching = 2; // min=0 max=2 step=1 (Yes, No, Maybe)
const loops = -0.5; // min=-0.5 max=10.5 step=1
const animation = 0; // min=0 max=1 step=0.01

const turtle = new Turtle();

var polygons = null;

var animated_seconds = seconds;

const scale = 95;

const hours_to_draw = (hours >= 0) ? hours : Math.floor(Math.random() * 12);
const minutes_to_draw = (minutes >= 0) ? minutes : Math.floor(Math.random() * 60);
const seconds_to_draw = (seconds >= 0) ? seconds : Math.floor(Math.random() * 60);
const loops_to_draw = (loops >= 0) ? loops : Math.floor(Math.random() * 5) + 1.5;
const hatching_to_draw = (draw_hatching==0) || ((draw_hatching==2) && (Math.random()<0.5));

Canvas.setpenopacity(.75);

function getCirclePoints(radius, steps) {
    const start_angle = -Math.PI / 2;
    return Array.from( {length: steps}, (_, id) => (
        [ Math.cos(id / steps * Math.PI*2 + start_angle) * radius, Math.sin(id / steps * Math.PI*2 + start_angle) * radius ]
    ));
}

function getLength(x, y) {
    return Math.sqrt(x*x + y*y);
}

function getTickAngle(t) {
    return t / 60 * Math.PI * 2 + Math.PI / 2;
}

function getTickPoints(t, radius_min, radius_max) {
    const thickness = 0.015 * scale;
    const radius_scale = radius_min + (radius_max - radius_min) * t / 60 / loops_to_draw;
    const radius_out = 1.0 * scale * radius_scale - thickness;
    const radius_in1 = 0.92 * scale * radius_scale - thickness;
    const radius_in2 = 0.85 * scale * radius_scale - thickness;

    const angle1 = getTickAngle(t-0.1);
    const angle2 = getTickAngle(t+0.1);
    const radius_in = ((t % 5) == 0) ? radius_in2 : radius_in1;
    
    var points = [];
    const angle_offset = 0.01;
    points.push( [ Math.cos(angle1) * radius_in,  Math.sin(angle1) * radius_in ] );
    points.push( [ Math.cos(angle1) * radius_out, Math.sin(angle1) * radius_out ] );
    points.push( [ Math.cos(angle2) * radius_out, Math.sin(angle2) * radius_out ] );
    points.push( [ Math.cos(angle2) * radius_in,  Math.sin(angle2) * radius_in ] );
    
    return points;
}

function drawTicks(shadow) {
    for (var t=0; t<=60 * loops_to_draw; t++) {
        var points = getTickPoints(t, 0.1, 1);
        
        if (!shadow) {
            const p1 = polygons.create();
            p1.addPoints(...points);
            p1.addOutline();
            if (hatching_to_draw) p1.addHatching(t / 60 * Math.PI * 2, .6);
            polygons.draw(turtle, p1, true);
        }
    
        if (shadow && hatching_to_draw) {
            const p2 = polygons.create();
            p2.addPoints(...points.map(p => [p[0]+1, p[1]+1]));
            p2.addHatching(-Math.PI/4, .5);
            polygons.draw(turtle, p2, false);
        }
    }
}

function drawCircle(radius, steps, shadow, hatching=false) {
    const points = getCirclePoints(radius * scale, steps);

    const p1 = polygons.create();

    p1.addPoints(...points);
    p1.addOutline();
    if (hatching) if (hatching_to_draw) p1.addHatching(Math.PI/4, 1);
    polygons.draw(turtle, p1, true);

    if (shadow !== undefined && hatching_to_draw) {
        const p2 = polygons.create();
        p2.addPoints(...points.map(p => [p[0]+shadow[0], p[1]+shadow[1]]));
        p2.addHatching(-Math.PI/4, .5);
        polygons.draw(turtle, p2, false);
    }
}

function drawHand(value, thickness, length, shadow, hatching=false) {
    const start_angle = -Math.PI/2 + value * Math.PI*2;
    
    const points = Array.from( {length: 4}, (_, id) => {
        var radius = thickness;
        if (id==0) radius = length;
        if (id==2) radius *= 3;
        radius *= scale;
        return [ Math.cos(start_angle + id / 4 * Math.PI*2) * radius, Math.sin(start_angle + id / 4 * Math.PI*2) * radius ];
    });

    const p1 = polygons.create();
    p1.addPoints(...points);
    if (hatching) if (hatching_to_draw) p1.addHatching(Math.PI/4, 1);
    p1.addOutline();
    polygons.draw(turtle, p1, true);

    if (shadow !== undefined && hatching_to_draw) {
        const p2 = polygons.create();
        p2.addPoints(...points.map(p => [p[0]+shadow[0], p[1]+shadow[1]]));
        p2.addHatching(-Math.PI/4, .5);
        polygons.draw(turtle, p2, false);
    }

}

function drawSpiral(loops, radius_min, radius_max, shadow) {
    const thickness = 0.01 * scale;
    const steps_per_loop = 100;
    const steps = loops * steps_per_loop;
    var points = [];
    for (var i=0; i<steps; i++) {
        const r = (radius_min + (radius_max - radius_min) * i / steps) * scale;
        const angle = i / steps_per_loop * Math.PI * 2 + Math.PI / 1.9;

        const p1 = [ Math.cos(angle) * (r - thickness), Math.sin(angle) * (r - thickness) ];
        const p2 = [ Math.cos(angle) * (r + thickness), Math.sin(angle) * (r + thickness) ];

        points.push( p1 );
        points.unshift( p2 );
    }

    if (!shadow) {
        const p1 = polygons.create();
        p1.addPoints(...points);
        if (hatching_to_draw) p1.addHatching(-Math.PI/4, .5);
        p1.addOutline();
        polygons.draw(turtle, p1, true);
    }
    
    if (shadow && hatching_to_draw) {
        const p2 = polygons.create();
        p2.addPoints(...points.map(p => [p[0]+1, p[1]+1]));
        p2.addHatching(-Math.PI/4, .5);
        polygons.draw(turtle, p2, false);
    }
}

function drawCase(shadow) {
    drawSpiral(loops_to_draw, 0.1, 1, shadow)
}

function drawCenter() { drawCircle(0.05, 20, [0.5, 0.5], true); }

function drawHourHand() {
    const hour_value = hours_to_draw / 12 + minutes / 60 / 12 + animated_seconds / 60 / 60 / 12;
    drawHand(hour_value, 0.045, 0.5, [1, 1]);
}

function drawMinuteHand() {
    const minute_value = minutes_to_draw / 60 + animated_seconds / 60 / 60;
    drawHand(minute_value, 0.045, 0.84, [1, 1]);
}
function drawSecondHand() {
    if (draw_seconds == 1) return;
    if (draw_seconds == 2 && Math.random() < 0.5) return;
    
    const second_value = animated_seconds / 60;
    drawHand(second_value, 0.025, 0.84, [1, 1], true);
}

function walk(i, t) {
    animated_seconds = seconds_to_draw + 12 * 60 * 60 * (t+animation);

    if (i ==0) polygons = new Polygons();

    var index = 0;
    if (false) {}
    else if (i == index++) drawCenter();
    else if (i == index++) drawSecondHand();
    else if (i == index++) drawMinuteHand();
    else if (i == index++) drawHourHand();
    else if (i == index++) drawCase(false);
    else if (i == index++) drawTicks(false);
    else if (i == index++) drawCase(true);
    else if (i == index++) drawTicks(true);
    else return false;

    return true;
}


// Adapted from:
// Transforms. Created by Reinder Nijhoff 2019 - @reindernijhoff
// The MIT License
// https://turtletoy.net/turtle/a5befa1f8d
//

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