Shortest distance between objects in JavaScript

In JavaScript, calculating the shortest distance between two objects requires their coordinates and the Euclidean distance formula. This is commonly used in game development, mapping applications, and geometric calculations.

Understanding the Problem

To find the shortest distance between two objects, we need their position coordinates (x, y). The distance is calculated using the Euclidean distance formula: ?[(x?-x?)² + (y?-y?)²]

The Distance Formula

The Euclidean distance formula calculates the straight-line distance between two points in a coordinate system:

Object 1 (x?,y?) Object 2 (x?,y?) Distance X Y

Basic Distance Calculation

function calculateDistance(x1, y1, x2, y2) {
   const deltaX = x2 - x1;
   const deltaY = y2 - y1;
   const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
   return distance;
}

// Example usage
const object1 = { x: 0, y: 1 };
const object2 = { x: 4, y: 6 };
const distance = calculateDistance(object1.x, object1.y, object2.x, object2.y);
console.log(`Distance: ${distance.toFixed(2)}`);
Distance: 6.40

Object-Oriented Approach

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
    
    distanceTo(otherPoint) {
        const deltaX = otherPoint.x - this.x;
        const deltaY = otherPoint.y - this.y;
        return Math.sqrt(deltaX * deltaX + deltaY * deltaY);
    }
}

// Create objects
const point1 = new Point(2, 3);
const point2 = new Point(5, 7);
const point3 = new Point(8, 1);

console.log(`Point1 to Point2: ${point1.distanceTo(point2).toFixed(2)}`);
console.log(`Point1 to Point3: ${point1.distanceTo(point3).toFixed(2)}`);
console.log(`Point2 to Point3: ${point2.distanceTo(point3).toFixed(2)}`);
Point1 to Point2: 5.00
Point1 to Point3: 6.32
Point2 to Point3: 7.21

Finding Shortest Distance Among Multiple Objects

function findShortestDistance(objects) {
    let shortestDistance = Infinity;
    let closestPair = null;
    
    for (let i = 0; i < objects.length; i++) {
        for (let j = i + 1; j < objects.length; j++) {
            const distance = calculateDistance(
                objects[i].x, objects[i].y,
                objects[j].x, objects[j].y
            );
            
            if (distance < shortestDistance) {
                shortestDistance = distance;
                closestPair = [objects[i], objects[j]];
            }
        }
    }
    
    return { distance: shortestDistance, pair: closestPair };
}

// Test with multiple objects
const objects = [
    { name: 'A', x: 1, y: 1 },
    { name: 'B', x: 4, y: 5 },
    { name: 'C', x: 2, y: 3 },
    { name: 'D', x: 7, y: 1 }
];

const result = findShortestDistance(objects);
console.log(`Shortest distance: ${result.distance.toFixed(2)}`);
console.log(`Between: ${result.pair[0].name} and ${result.pair[1].name}`);
Shortest distance: 2.24
Between: A and C

Complexity Analysis

Operation Time Complexity Space Complexity
Two objects distance O(1) O(1)
Shortest among n objects O(n²) O(1)

Common Use Cases

Distance calculations are used in:

  • Game development for collision detection
  • Mapping applications for finding nearest locations
  • Data visualization for clustering
  • Robotics for path planning

Conclusion

Calculating shortest distance between objects in JavaScript uses the Euclidean distance formula. For two objects, it's O(1) complexity, while finding the shortest among multiple objects requires O(n²) comparisons.

Updated on: 2026-03-15T23:19:00+05:30

653 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements