Skip to content

Instantly share code, notes, and snippets.

@maikkoko
Last active January 4, 2018 05:42
Show Gist options
  • Select an option

  • Save maikkoko/77edcc9002d0bd6533f92eaf7d5a49eb to your computer and use it in GitHub Desktop.

Select an option

Save maikkoko/77edcc9002d0bd6533f92eaf7d5a49eb to your computer and use it in GitHub Desktop.
// Given 5 Points
const coordsA = [
{x: 589.4542, y: 1250.2301},
{x: 682.36816, y: 487.62726},
{x: 904.16284, y: 836.44556},
{x: 373.65402, y: 839.444},
{x: 724.32935, y: 710.51117}
]
// Get SSS Of Triangle
// Input: Point Coordinates
function getTriangles(coords) {
let triangles = []
let ctr = 0
for (i=0; i<coords.length - 2; i++) {
let point1 = coords[i]
for (j=i+1; j<coords.length - 1; j++) {
let point2 = coords[j]
for (k=j+1; k<coords.length; k++) {
let point3 = coords[k]
let side1 = Math.sqrt(Math.pow(point1.x - point2.x, 2) + Math.pow(point1.y - point2.y, 2))
let side2 = Math.sqrt(Math.pow(point1.x - point3.x, 2) + Math.pow(point1.y - point3.y, 2))
let side3 = Math.sqrt(Math.pow(point2.x - point3.x, 2) + Math.pow(point2.y - point3.y, 2))
let sides = [side1, side2, side3]
triangles.push(sides.sort((a, b) => (a - b))) // Sorting not necessary
}
}
}
return triangles
}
// Getting Area of each triangle
// Input: Array of sides/lines formed by coordinates
function getArea(sides) {
let p = getPerimeter(sides);
return Math.sqrt(p * (p - sides[0]) * (p - sides[1]) * (p - sides.get[2]));
}
function getPerimeter(sides) {
let perimeter = 0.0;
for(i=0; i < sides.length; i++) {
perimeter += sides[i];
}
return perimeter / 2;
}
// Mean and Variance
// Input: Array of Triangle Areas
function getMean(array) {
let sum = 0
for(i=0; i<array.length; i++) {
sum += array[i]
}
return sum/array.length
}
function getVariance(array) {
let mean = getMean(array)
let sum = 0
for(i=0; i<array.length; i++) {
sum += Math.pow((mean - array[i]), 2)
}
return sum / array.length
}
// Comparing Mean and Variance
// Given 2 sets of triangle areas
const arr1 = [263.82097277861135, 333.78232568590397, 597.3546574254193]
const arr2 = [248.51006885764514, 343.4903046790758, 591.9754282979179]
console.log(getMean(arr1) / getMean(arr2)) // ~ 1.00
console.log(Math.sqrt(getVariance(arr1)) / Math.sqrt(getVariance(arr2))) // ~0.99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment