Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas></canvas>
              
            
!

CSS

              
                body {
	margin: 0;
	background: #111145;
	overflow: hidden;
}
              
            
!

JS

              
                /** 

This line intersection algo comes from the book "Real Time Collision Detection" by Christer Ericson

https://realtimecollisiondetection.net

*/

const PILLS = 6; // I can check about 400 at 60FPS. I'd think you would be able to check at least 100 as part of a game.
const PILL_RADIUS = 20;

document.addEventListener(
	"touchstart",
	function(event) {
		event.preventDefault();
	},
	{ passive: false }
);

const { Vector3 } = Vexr;

var canvas = document.getElementsByTagName("canvas")[0];

var orig = new Vector3(window.innerWidth / 2, window.innerHeight / 2);

function resize() {
	orig.x = window.innerWidth / 2;
	orig.y = window.innerHeight / 2;
	canvas.width = window.innerWidth;
	canvas.height = window.innerHeight;
}

window.addEventListener("resize", resize);
resize();

const ctx = canvas.getContext("2d");

function drawPill(ctx, line, color = "#00FF00") {
	var angle = Math.atan2(line.start.x - line.end.x, line.start.y - line.end.y);

	ctx.beginPath();
	ctx.moveTo(line.start.x, line.start.y);
	ctx.strokeStyle = color;
	ctx.fillStyle = color;
	ctx.lineWidth = 2;
	ctx.beginPath();
	ctx.arc(line.end.x, line.end.y, line.r, -angle + Math.PI, -angle);
	ctx.arc(line.start.x, line.start.y, line.r, -angle, -angle + Math.PI);
	ctx.closePath();
	ctx.stroke();
	//ctx.endPath();
}

class Pill {
	constructor(start = new Vector3(0, 0), end = new Vector3(10, 10), r = 20) {
		this.start = start;
		this.end = end;
		this.r = r;
	}
	distance(ref = new Vector3()) {
		return Vector3.subtract(this.end, this.start, ref);
	}
}

function randomPill() {
	var x1 = Math.random() * window.innerWidth;
	var x2 = Math.random() * window.innerWidth;
	var y1 = Math.random() * window.innerHeight;
	var y2 = Math.random() * window.innerHeight;
	return new Pill(
		new Vector3(x1, y1),
		new Vector3(x2, y2),
		10 + Math.random() * 20
	);
}

function clamp(n = 0, min = 0, max = 1) {
	if (n < min) return min;
	if (n > max) return max;
	return n;
}

function testPill(pill1, pill2, c1 = new Vector3(), c2 = new Vector3()) {
	var s = (t = 0);
	var dist = closestPoint(pill1, pill2, c1, c2);
	var radius = pill1.r + pill2.r;
	return dist <= radius * radius;
}

// some static reference vectors I use for each calc instead of creating new instances
const d1a = new Vector3();
const d2a = new Vector3();
const c3 = new Vector3();
const r1 = new Vector3();
function closestPoint(line1, line2, c1 = new Vector3(), c2 = new Vector3()) {
	var d1 = line1.distance(d1a);
	var d2 = line2.distance(d2a);
	var r = Vector3.subtract(line1.start, line2.start, r1);

	var a = Vector3.dot(d1, d1);
	var e = Vector3.dot(d2, d2);
	var f = Vector3.dot(d2, r);

	var s = 0;
	var t = 0;

	if (a <= Number.EPSILON && e <= Number.EPSILON) {
		var s = 0;
		var t = 0;
		c1.set(line.start.x, line.start.y, line.start.x);
		c2.set(line2.start.x, line2.start.y, line2.start.x);
		var dist = Vector3.subtract(c1, c2, c3);
		return Vector3.dot(dist, dist);
	}

	if (a <= Number.EPSILON) {
		s = 0;
		t = f / e;
		t = clamp(t);
	} else {
		var c = Vector3.dot(d1, r);
		if (e <= Number.EPSILON) {
			t = 0;
			s = clamp(-c / a);
		} else {
			var b = Vector3.dot(d1, d2);
			var denom = a * e - b * b;
			if (denom != 0) {
				s = clamp((b * f - c * e) / denom);
			} else {
				s = 0;
			}

			t = (b * s + f) / e;

			if (t < 0) {
				t = 0;
				s = clamp(-c / a);
			} else if (t > 1) {
				t = 1;
				s = clamp((b - c) / a);
			}
		}
	}

	Vector3.add(line1.start, Vector3.multiply(d1, s), c1);
	Vector3.add(line2.start, Vector3.multiply(d2, t), c2);

	var dist = Vector3.subtract(c1, c2, c3);
	return Vector3.dot(dist, dist);
}

var mouse = new Vector3(0, 0);
window.addEventListener("mousemove", e => {
	mouse.x = e.pageX;
	mouse.y = e.pageY;
});

window.addEventListener("touchmove", e => {
	e.preventDefault();
	mouse.x = e.touches[0].pageX;
	mouse.y = e.touches[0].pageY;
	if (e.touches.length > 1) {
		orig.x = e.touches[1].pageX;
		orig.y = e.touches[1].pageY;
	}
});

var lineA = new Pill(orig, mouse, PILL_RADIUS);
lineA.start = orig;
lineA.end = mouse;

var lines = new Array(PILLS).fill(0).map(e => randomPill());

setInterval(() => {
	lines = new Array(PILLS).fill(0).map(e => randomPill());
}, 5000);

var c1 = new Vector3();
var c2 = new Vector3();

function update() {
	window.requestAnimationFrame(update);
	ctx.moveTo(0, 0);
	ctx.beginPath();
	ctx.clearRect(0, 0, canvas.width, canvas.height);

	lines.map(line => {
		drawPill(ctx, line, "#FF00FF");
		var out = testPill(lineA, line, c1, c2);

		if (out) {
			ctx.beginPath();
			ctx.strokeStyle = "#FFFF00";
			ctx.arc(
				c1.x,
				c1.y,
				lineA.r < line.r ? lineA.r - 4 : line.r - 4,
				0,
				2 * Math.PI
			);
			ctx.stroke();
		}
	});
	drawPill(ctx, lineA, "#00FF00");
}

update();

              
            
!
999px

Console