Nodejs Distance Calculate distance(p1, p2)

Here you can find the source of distance(p1, p2)

Method Source Code

function Point(x, y) {
    this.x = x;/*www . j av a 2  s  .co m*/
    this.y = y;
}

function randomPoint() {
    var randomx = randomNumber(WIDTH);
    var randomy = randomNumber(HEIGHT);
    var randomPoint = new Point(randomx, randomy);
    return randomPoint;
}

function randomNumber(boundary) {
    return parseInt(Math.random() * boundary);
    //return Math.floor(Math.random() * boundary);
}

function distance(p1, p2) {
    return euclidean(p1.x - p2.x, p1.y - p2.y);
}

function euclidean(dx, dy) {
    return Math.sqrt(dx * dx + dy * dy);
}

Related

  1. calculateDistance(lat2, lon2)
    function calculateDistance(lat2, lon2) {
      var R = 3959; 
      var lat1 = 47.3193057;
      var lon1 = -93.2895824;
      var dLat = (lat2 - lat1).toRad();
      var dLon = (lon2 - lon1).toRad(); 
      var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
              Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
              Math.sin(dLon / 2) * Math.sin(dLon / 2); 
    ...
    
  2. distance(lat1, lon1, lat2, lon2)
    exports.distance = function(lat1, lon1, lat2, lon2) {
      var radlat1 = Math.PI * lat1/180;
      var radlat2 = Math.PI * lat2/180;
      var radlon1 = Math.PI * lon1/180;
      var radlon2 = Math.PI * lon2/180;
      var theta = lon1-lon2;
      var radtheta = Math.PI * theta/180;
      var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
      dist = Math.acos(dist);
    ...
    
  3. distance(lat1, lon1, lat2, lon2)
    function distance(lat1, lon1, lat2, lon2) {
      var R = 6371;
      var a = 0.5 - Math.cos((lat2 - lat1) * Math.PI / 180)/2 + 
         Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * 
         (1 - Math.cos((lon2 - lon1) * Math.PI / 180))/2;
      var m = R * 2 * Math.asin(Math.sqrt(a))*1000;
      return Math.floor(m);
    
  4. distance(lat1,lon1,lat2,lon2)
    function distance(lat1,lon1,lat2,lon2) {
      var R = 6371; 
      var dLat = deg2rad(lat2-lat1);  
      var dLon = deg2rad(lon2-lon1);
      var a =
        Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
        Math.sin(dLon/2) * Math.sin(dLon/2)
        ;
    ...
    
  5. distance(x1, x2, y1, y2)
    Math.distance = function(x1, x2, y1, y2){
      var x = x2 - x1;
      var y = y2 - y1;
      return Math.sqrt(x * x + y * y);
    };
    
  6. distanceBetweenTwoPoints(p1, p2)
    Util.distanceBetweenTwoPoints = function(p1, p2) {
      return Math.sqrt(Math.pow(p2.y - p1.y, 2) + Math.pow(p2.x - p1.x, 2)).toFixed(2);
    };
    
  7. distanceOfTimeInWords(from, to)
    window.distanceOfTimeInWords = function (from, to) {
      var distance_in_milliseconds = to - from;
      var distance_in_minutes = Math.round(Math.abs(distance_in_milliseconds / 60000));
      var words = "";
      if (distance_in_minutes == 0) {
        words = "less than a minute";
      } else if (distance_in_minutes == 1) {
        words = "1 minute";
      } else if (distance_in_minutes < 45) {
    ...
    
  8. getEditDistance(a, b)
    function getEditDistance(a, b){
      if(a.length == 0) return b.length;
      if(b.length == 0) return a.length;
      var matrix = [];
      var i;
      for(i = 0; i <= b.length; i++){
        matrix[i] = [i];
      var j;
    ...