Skip to content

Instantly share code, notes, and snippets.

@MarcSymonds
Last active September 27, 2023 15:00
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save MarcSymonds/1fb6c875e85553032213e804bfaf2cbb to your computer and use it in GitHub Desktop.
Javascript code for calculating triangle properties
// Note: Javascript works in radians.
//
// pa, pb, pc are 3 points. Each point is an array with 2 values; x and y
//
function triangleCalculations(pa, pb, pc) {
var a = lineLength(pc, pb); // Length of line from c to b.
var b = lineLength(pa, pc); // Length of line from a to c.
var c = lineLength(pa, pb); // Length of line from a to b.
//var A = Math.acos(((b * b) + (c * c) - (a * a)) / (2 * b * c)); // Angle at point a.
//var B = Math.acos(((a * a) + (c * c) - (b * b)) / (2 * a * c)); // Angle at point b.
//var C = Math.acos(((a * a) + (b * b) - (c * c)) / (2 * a * b)); // Angle at point c.
//var area = (a * b * Math.sin(C)) / 2; // Area of the triangle.
// Medians:-
//
//var ma = Math.sqrt(((2 * (b * b)) + (2 * (c * c)) - (a * a)) / 4); // Distance from point a to the middle of line a/c.
//var mb = Math.sqrt(((2 * (a * a)) + (2 * (c * c)) - (b * b)) / 4); // Distance from point b to the middle of line b/c.
var mc = Math.sqrt(((2 * (a * a)) + (2 * (b * b)) - (c * c)) / 4); // Distance from point c to the middle of line a/b.
//var mc2 = Math.sqrt(((c / 2) * (c / 2)) + (b * b) - (b * c * Math.cos(A)));
// Inradius and circumradius.
//
//var inradius = area / ((a + b + c) / 2);
//var cirumradius = a / (2 * Math.sin(A));
return mc;
}
// Calculate the distance between 2 points - p1 and p2.
//
// p1 and p2 are arrays with 2 values; x and y
//
function lineLength(p1, p2) {
var xd = Math.abs(p1[0] - p2[0]);
var yd = Math.abs(p1[1] - p2[1]);
return Math.sqrt((xd * xd) + (yd * yd));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment