pointsCollinear method

bool pointsCollinear(
  1. Coordinate pt1,
  2. Coordinate pt2,
  3. Coordinate pt3
)

Implementation

bool pointsCollinear(
    JTS.Coordinate pt1, JTS.Coordinate pt2, JTS.Coordinate pt3) {
  // does pt1->pt2->pt3 make a straight line?
  // essentially this is just checking to see if the slope(pt1->pt2) === slope(pt2->pt3)
  // if slopes are equal, then they must be collinear, because they share pt2
  var dx1 = pt1.x - pt2.x;
  var dy1 = pt1.y - pt2.y;
  var dx2 = pt2.x - pt3.x;
  var dy2 = pt2.y - pt3.y;
  var n1 = math.sqrt(dx1 * dx1 + dy1 * dy1);
  var n2 = math.sqrt(dx2 * dx2 + dy2 * dy2);
  // Assuming det(u, v) = 0, we have:
  // |det(u + u_err, v + v_err)| = |det(u + u_err, v + v_err) - det(u,v)|
  // =|det(u, v_err) + det(u_err. v) + det(u_err, v_err)|
  // <= |det(u, v_err)| + |det(u_err, v)| + |det(u_err, v_err)|
  // <= N(u)N(v_err) + N(u_err)N(v) + N(u_err)N(v_err)
  // <= eps * (N(u) + N(v) + eps)
  // We have N(u) ~ N(u + u_err) and N(v) ~ N(v + v_err).
  // Assuming eps << N(u) and eps << N(v), we end with:
  // |det(u + u_err, v + v_err)| <= eps * (N(u + u_err) + N(v + v_err))
  return (dx1 * dy2 - dx2 * dy1).abs() <= eps * (n1 + n2);
}