Posted By

parkerkrhoyt on 01/16/11


Tagged

math two line algorithm distance points


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

aanoojgmailcom


Distance Between Two Points


 / Published in: JavaScript
 

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.

  1. function lineDistance( point1, point2 )
  2. {
  3. var xs = 0;
  4. var ys = 0;
  5.  
  6. xs = point2.x - point1.x;
  7. xs = xs * xs;
  8.  
  9. ys = point2.y - point1.y;
  10. ys = ys * ys;
  11.  
  12. return Math.sqrt( xs + ys );
  13. }

Report this snippet  

You need to login to post a comment.