Get Triangle Height in JavaScript

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));
  };

This entry was posted by Al MacDonald on February 23, 2010 in JavaScript.

Comments

Training

We offer comprehensive beginner and advanced jQuery, JavaScript and HTML5 training onsite and at The Bocoup Loft in Boston, MA.

Check out our upcoming courses:

More about training →

Categories

AJAX, Apache, Arduino, Audio, audio data api, Augmented Reality, Backbone, Bash, Bocoup, boston, Build Tool, Burst, burst-engine, Canvas, Chrome, Chromium, conflict, CouchDB, CSS, CSS3, Data, Data Visualization, EventSource, facebook, Feature Detection, Fieldrunners, Firebug, Firefox, fx4, Games, Gecko, Git, Google, Google Analytics, Grunt, hardware, heart, hole, html, HTML5, inset, Internet Explorer, Inventions, JavaScript, Johnny-Five, jQuery, jQuery Plugins, jQuery.Hive, JSARToolkit, Linux, list, Media, merge, Minefield, Mobile, moz10, mozilla, Node.js, OOP, Open Source, Open Web Apps, Opera, opposite-winding, Performance, PHP, PollenJS, Popcorn, Popcorn.js, primatives, Processing.js, RaphaƫlJS, Regular Expressions, Ringmark, Ringmark Gallery, Safari, Security, shadow, social, Social Networking, specs, SpiderMonkey, sprite-viking, Standards, SVG, Sys Admin, terminal, Testing, Tools, Training, Tutorial, ubuntu, Uncategorized, Uselessware, V8, Video, Visualization, VML, W3C, Web Project Management, Web Workers, webgl, WebKit, WebRTC, wget, Workflow, wtfjs and xml.

Twitter