2点間の距離を求めるクラス
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
distanceTo(other) {
const diffX2 = (this.x - other.x) ** 2;
const diffY2 = (this.y - other.y) ** 2;
const distance = Math.sqrt(diffX2 + diffY2);
return distance;
}
}
const point1 = new Point(1, 2);
const point2 = new Point(5, 8);
console.log(point1.distanceTo(point2));