Wall clock

Plot your own stopped clock with an accuracy of +/- 6 hrs.
Reload or adjust the sliders for more shapes.

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 shape = 0; // min=0 max=8 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 = 90;

var case_sides = [ 3, 4, 4, 5, 6, 9, 12, 100 ];

var tick_scale = [ 0.85, 0.98, 0.98, 1, 1, 1, 1, 1 ];

var hand_scale = [ 0.5, 0.7, 0.7, 0.8, 0.85, 0.95, 1, 1 ];

const shape_to_draw = (shape > 0) ? (shape-1) : Math.floor(Math.random() * case_sides.length);

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

Canvas.setpenopacity(.75);

function getCirclePoints(radius, steps) {
    const start_angle = -Math.PI / ((shape_to_draw != 1) ? 2 : 4);
    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 getTickScaling(t) {
    const steps = 60 / case_sides[shape_to_draw];
    const toffset = (shape_to_draw != 1) ? 0 : (60 / 2 / case_sides[shape_to_draw]);
    const t0 = Math.floor(((t+toffset)) / steps) * steps - toffset;
    const t1 = t0 + steps;
    const dt = (t - t0) / steps;
    const x0 = Math.cos(getTickAngle(t0));
    const y0 = Math.sin(getTickAngle(t0));
    const x1 = Math.cos(getTickAngle(t1));
    const y1 = Math.sin(getTickAngle(t1));
    const xt = x0 + (x1 - x0) * dt;
    const yt = y0 + (y1 - y0) * dt;
    return getLength(xt, yt);
}

function getTickPoints(t) {
    const radius_scale = getTickScaling(t);
    const radius_out = 0.85 * tick_scale[shape_to_draw] * scale * radius_scale;
    const radius_in1 = 0.80 * tick_scale[shape_to_draw] * scale * radius_scale;
    const radius_in2 = 0.75 * tick_scale[shape_to_draw] * scale * radius_scale;

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

function drawTicks() {
    for (var t=0; t<60; t++) {
        var points = getTickPoints(t);
        const p1 = polygons.create();
        p1.addPoints(...points);
        p1.addOutline();
        p1.addHatching(t / 60 * Math.PI * 2, .6);
        polygons.draw(turtle, p1, true);
    }
}

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

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

    if (shadow !== undefined) {
        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 * hand_scale[shape_to_draw];
        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) p1.addHatching(Math.PI/4, 1);
    p1.addOutline();
    polygons.draw(turtle, p1, true);

    if (shadow !== undefined) {
        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 drawCase() {
    drawCircle(0.9, case_sides[shape_to_draw], undefined, false);
    drawCircle(0.98, case_sides[shape_to_draw], [2, 3], true);
}

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

function drawHourHand() {
    const hour_value = hours_to_draw / 12 + minutes_to_draw / 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++) drawTicks();
    else if (i == index++) drawCase();
    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);
		}
	};
}