Measuring distance between two points

Simple example of measuring distance between two points.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>

var p1 = new Object();
    p1.x = 10;
    p1.y = 15;
    
var p2 = new Object();
    p2.x = 23;
    p2.y = 32;
    
function distance( p1, p2 )
{
  var xp = 0;
  var yp = 0;
 
  xp = p2.x - p1.x;
  xp = xp * xp;
 
  yp = p2.y - p1.y;
  yp = yp * yp;
 
  return Math.sqrt(xp + yp);
}

var distance = distance(p1,p2);    
    
console.log("The distance between two points is " + distance);

</script>
</body>
</html>

Responses

0 Replies