Last active
February 23, 2018 15:55
-
-
Save mritzco/9f3b2363960d5aca02a53f499f3f4585 to your computer and use it in GitHub Desktop.
Calculating triangles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://www.teacherschoice.com.au/Maths_Library/Trigonometry/triangle_given_3_points.htm | |
const triangle = { | |
corners: {}, | |
angles:{}, | |
sides: {}, | |
create: function (a,b,c,d,e,f) { | |
this.corners.A = [ a, b ], | |
this.corners.B = [ c, d ], | |
this.corners.C = [ e, f ] | |
return this; | |
}, | |
distance: function (q,w) { | |
let x = q[0] - w[0], | |
y = q[1] - w[1]; | |
return Math.sqrt( Math.pow(x,2) + Math.pow(y,2)) | |
}, | |
largest_side: function() { | |
let largest_length = Math.max(this.sides.a, this.sides.b, this.sides.c); | |
let largest_side = Object.keys(this.sides).filter ( key => { | |
return this.sides[key] == largest_length; | |
}); | |
return largest_side[0]; | |
}, | |
find_angle: function (a,b,c) { | |
return Math.acos( (Math.pow(a,2) + Math.pow(c,2) -Math.pow(b,2) ) / (2 * a * c)) * ( 180 / Math.PI ) | |
}, | |
lengths: function () { | |
this.sides.a = this.distance(this.corners.C,this.corners.B); | |
this.sides.b = this.distance(this.corners.A,this.corners.C); | |
this.sides.c = this.distance(this.corners.A,this.corners.B); | |
return this; | |
}, | |
calc_angles:function(){ | |
// largets angle first | |
let l_side = this.largest_side(); | |
let other_sides = ['a','b', 'c'].filter( side => { | |
return side != l_side; | |
}); | |
let adj1 = this.sides[other_sides[0]]; | |
let adj2 = this.sides[other_sides[1]]; | |
this.angles[l_side] = this.find_angle(adj1, this.sides[l_side], adj2); | |
// calculate remaining angles | |
return this; | |
} | |
} | |
console.log(triangle.create(-2,-2,3,-1,1,3).lengths().calc_angles()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment