Skip to main content

This function will calculate the distance between two provided points. This is useful in game development when calculating paths of motion for sprites. The function expects two points to be provided. A point is defined as an object with an "x" and a "y" property. The result is a numeric value of the distance.

/**
 * This function will calculate the distance between two provided points.
 * This is useful in game development when calculating paths of motion for sprites.
 * The function expects two points to be provided. A point is defined as an object
 * with an "x" and a "y" property. The result is a numeric value of the distance.

 * @param {Number} point1
 * @param {Number} point2

 * @returns Number The result is a numeric value of the distance.
 * @type Number
 */
function lineDistance(point1, point2) {
  var xs = 0;
  var ys = 0;

  xs = point2.x - point1.x;
  xs = xs * xs;

  ys = point2.y - point1.y;
  ys = ys * ys;

  return Math.sqrt(xs + ys);
}