UNPKG

@g20/euclid

Version:
75 lines (71 loc) 2.92 kB
/** * @g20/euclid 1.0.0-alpha.46 * (c) David Geo Holmes david.geo.holmes@gmail.com * Released under the MIT License. */ System.register(['@g20/core'], (function (exports) { 'use strict'; var G20, dispose; return { setters: [function (module) { G20 = module.G20; dispose = module.dispose; }], execute: (function () { exports("circle_circle_intersection", circle_circle_intersection); /** * The vector from the center of circleA to the center of circleB. */ const D = G20.vector(0, 0); function circle_circle_intersection(circleA, circleB) { const point1 = G20.zero.clone(); const point2 = G20.zero.clone(); const disposables = []; let enabled = false; const calculate = () => { if (enabled) { update_circle_crcle_intersection_points(point1, point2, circleA.X, circleB.X, circleA.radius.a, circleB.radius.a); } }; disposables.push(circleA.X.change$.subscribe(calculate)); disposables.push(circleB.X.change$.subscribe(calculate)); disposables.push(circleA.radius.change$.subscribe(calculate)); disposables.push(circleB.radius.change$.subscribe(calculate)); enabled = true; calculate(); const disposable = { dispose() { dispose(disposables); } }; return [point1, point2, disposable]; } function update_circle_crcle_intersection_points(point1, point2, ca, cb, R, r) { if (R !== -1 && r !== -1) { D.copyVector(cb).sub(ca); const dd = D.quaditude(); const rr = r * r; const RR = R * R; const d = Math.sqrt(dd); const λ = (dd - rr + RR) / (2 * d); const aa = RR - λ * λ; if (aa >= 0) { const a = Math.sqrt(aa); const dhat = D.clone().scale(1 / d); const ahat = dhat.clone().mul(G20.I); const λdhat = dhat.clone().scale(λ); const avec = ahat.clone().scale(a); point1.copyVector(ca).add(λdhat).add(avec); point2.copyVector(ca).add(λdhat).sub(avec); } else { // No intersection. point1.set(NaN, NaN); point2.set(NaN, NaN); } } } }) }; })); //# sourceMappingURL=index.js.map