Get Triangle Height in JavaScript
Posted by Alistair Macdonald
Feb 23 2010
How to find the height of a triangle in JavaScript if you only have the 3 vertexes/points of the triangle.
Assuming that XY are the top of your triangle, and X1Y1, X2Y2 are the other two points, you can find the height of the triangle using Pythagorean theorem. This method works for Isosceles or Equilateral triangles.
// Pythagorean theorem for isosceles or equilateral triangle
var getTriangleHeight = function(x, y, x1, y1, x2, y2){
var b = Math.sqrt(Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2)) /2;
var c = Math.sqrt(Math.pow(x1-x, 2) + Math.pow(y1-y, 2));
return Math.sqrt((c*c)-(b*b));
};
Tagged in
Comments
We moved off of Disqus for data privacy and consent concerns, and are currently searching for a new commenting tool.
Contact Us
We'd love to hear from you. Get in touch!