How to calculate the distance between two points in JavaScript

1 Answer

0 votes
class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }

    static className = "class Point";
    
    static distance(p1, p2) {
        const dx = p1.x - p2.x;
        const dy = p1.y - p2.y;

        return Math.hypot(dx, dy);
    }
}

const p1 = new Point(7, 11);
const p2 = new Point(8, 20);

console.log(Point.className); 
console.log(Point.distance(p1, p2)); 


      
/*
run:
      
class Point
9.055385138137417

*/

 



answered May 3, 2024 by avibootz
...