Skip to content

Instantly share code, notes, and snippets.

View agusmade's full-sized avatar

Agus Made agusmade

  • Jakarta, Indonesia
View GitHub Profile
@agusmade
agusmade / IntersectTwoCircles.js
Created December 25, 2022 08:30 — forked from jupdike/IntersectTwoCircles.js
Find the intersections (two points) of two circles, if they intersect at all
// based on the math here:
// http://math.stackexchange.com/a/1367732
// x1,y1 is the center of the first circle, with radius r1
// x2,y2 is the center of the second ricle, with radius r2
function intersectTwoCircles(x1,y1,r1, x2,y2,r2) {
var centerdx = x1 - x2;
var centerdy = y1 - y2;
var R = Math.sqrt(centerdx * centerdx + centerdy * centerdy);
if (!(Math.abs(r1 - r2) <= R && R <= r1 + r2)) { // no intersection
@agusmade
agusmade / polygon-area.js
Created December 25, 2022 08:27
Calculating Polygon Area
function polygonArea(X, Y) {
var area = 0; // Accumulates area in the loop
var numPoints = X.length;
var j = numPoints-1; // The last vertex is the 'previous' one to the first
for (var i=0; i<numPoints; i++) {
area = area + (X[j]+X[i]) * (Y[j]-Y[i]);
j = i; //j is previous vertex to i
}
return area/2;