JSFiddle - React, Tailwind, and code Playground
by artgas_pro
HTML
<!DOCTYPE html>
<head>
<title>Asteroid</title>
</head>
<body>
</body>
</html>
JavaScript
class Space {
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
makeAsteroidInRandomLocation() {
const randomX = Math.round(Math.random() * this.x);
const randomY = Math.round(Math.random() * this.y);
const randomZ = Math.round(Math.random() * this.z);
return new Asteroid(randomX, randomY, randomZ);
}
}
class Zond {
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
}
class Asteroid {
constructor(x, y, z) {
this._x = x;
this._y = y;
this._z = z;
}
distanceFromAsteroidToZond(zond) {
const distance = Math.sqrt(
(this._x - zond.x) ** 2 +
(this._y - zond.y) ** 2 +
(this._z - zond.z) ** 2
);
return distance;
}
}
const space = new Space(100, 100, 100);
const asteroid = space.makeAsteroidInRandomLocation();
const zond0 = new Zond(0, 0, 0);
const zondX100 = new Zond(100, 0, 0);
const zondY100 = new Zond(0, 100, 0);
const coordinateAsteroid_X = Math.round(
(Math.trunc(asteroid.distanceFromAsteroidToZond(zondX100) ** 2) -
Math.trunc(asteroid.distanceFromAsteroidToZond(zond0) ** 2) -
100 ** 2) /
-200
);
const coordinateAsteroid_Y = Math.round(
(Math.trunc(asteroid.distanceFromAsteroidToZond(zondY100) ** 2) -
Math.trunc(asteroid.distanceFromAsteroidToZond(zond0) ** 2) -
100 ** 2) /
-200
);
const coordinateAsteroid_Z = Math.round(
Math.sqrt(
Math.trunc(asteroid.distanceFromAsteroidToZond(zond0) ** 2) -
coordinateAsteroid_X ** 2 -
coordinateAsteroid_Y ** 2
)
);
document.body.innerHTML = `Calculation coordinates X: ${coordinateAsteroid_X}, Y: ${coordinateAsteroid_Y}, Z: ${coordinateAsteroid_Z}`;