Search code examples
javascriptmathgeometry2dcomputational-geometry

draw the altitude of a triangle


I want to draw a line that signifies the altitude of a triangle. I know all 3 points of the circle (a, b, c). I want to draw the altitude through a.

I have the following functions to work out the perpendicular gradient of bc

gradient = function(a, b) {  
  return b.y - a.y / b.x - a.x;
};

perpendicularGradient = function (a, b) {
  return -1 / gradient(a, b);
};

I can now get the equation of the line using y = mx + c for line of a with a perpendicular slope of bc and I can work out the y intercept.

function perpendicularLine(vertex, a, b) {
  // b = y - m * x
  var slope = perpendicularGradient(a, b),
      yIntercept = (- slope * vertex.x) + vertex.y;

  //how do I find the coordinates of the point on bc to draw a line from vertex
} 

I don't know what to do next, i.e. how do I find the coordinates of the point on bc to join the line from a.

From googling about, I know I could use vectors but I have not covered that yet on my maths course and I would prefer to use the equation of a line.


Solution

  • Right, after you have your yIntercept and the slope of the perpendicular line, you need to construct a system of 2 linear equations with 2 unknowns (x0, y0) from the 2 line equations (bc line and line passing through a). The solution to this system is your intersection point along the bc line.