// triangle dimensions in pixels (length is along the line)
var TRI_LENGTH = 20;
var TRI_WIDTH = 10;

// compute a triangle for a directed line
function computeMidTri(pt0, pt1)
{
  var x0 = pt0.x;
  var y0 = pt0.y;
  var x1 = pt1.x;
  var y1 = pt1.y;

  // compute the slope of the original line
  var slope = (y1 - y0) / (x1 - x0);
  if(slope == 0)
    slope = 0.0001;
  else if(slope == Infinity) {
    slope = 100000;
    var vert = true;
  } else if(slope == -Infinity) {
    slope = -100000;
    var vert = true;
  }
  // compute the point on the line where the base of the triangle will sit (currently halfway)
  var xt0 = (x0 + x1) / 2;
  var yt0 = slope * (xt0 - x0) + y0;
  if(vert)
    yt0 = (y0 + y1) / 2;

  // compute the two points in the triangle that lie OFF the line
  // this is derived from a system of two equations: the distance formula and the point-slope equation for the perpendicular line
  // the distance formula is quadratic so we get two points that satisfy both equations, one on either side of the original line
  var perpSlope = -1 / slope;
  var dist = TRI_WIDTH;
  var compu = dist / Math.sqrt(1 + perpSlope * perpSlope);   // compute this once, use it twice
  xt1 = compu + xt0;
  yt1 = perpSlope * (xt1 - xt0) + yt0;
  
  xt2 = -compu + xt0;
  yt2 = perpSlope * (xt2 - xt0) + yt0;

  // compute the point in the triangle that falls ON the line (the pointer)
  var dir = 1;
  if(x1 < x0)
    dir = -1;
  dist = dir * TRI_LENGTH;
  xt3 = dist / Math.sqrt(1 + slope * slope) + xt0;
  yt3 = slope * (xt3 - xt0) + yt0;

  var tri = {};
  tri.xPts = [xt1, xt2, xt3];
  tri.yPts = [yt1, yt2, yt3];

  return tri;
}