JSFiddle - React, Tailwind, and code Playground
by artgas_pro
HTML
<head>
<title>Asteroid</title>
</head>
<body>
</body>
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(asteroid, x, y, z) {
this.x = x;
this.y = y;
this.z = z;
this.asteroid = asteroid;
}
distanceToasteroid() {
const distance = Math.sqrt(
(this.asteroid.getX() - this.x) ** 2 +
(this.asteroid.getY() - this.y) ** 2 +
(this.asteroid.getZ() - this.z) ** 2
);
return distance;
}
}
class Asteroid {
constructor(x, y, z) {
this._x = x;
this._y = y;
this._z = z;
}
getX() {
return this._x;
}
getY() {
return this._y;
}
getZ() {
return this._z;
}
}
const space = new Space(100, 100, 100);
const asteroid = space.makeAsteroidInRandomLocation();
const zond0 = new Zond(asteroid, 0, 0, 0);
const zondX100 = new Zond(asteroid, 100, 0, 0);
const zondY100 = new Zond(asteroid, 0, 100, 0);
const coordinateAsteroid_X = Math.round(
(Math.trunc(zondX100.distanceToasteroid() ** 2) -
Math.trunc(zond0.distanceToasteroid() ** 2) -
100 ** 2) /
-200
);
const coordinateAsteroid_Y = Math.round(
(Math.trunc(zondY100.distanceToasteroid() ** 2) -
Math.trunc(zond0.distanceToasteroid() ** 2) -
100 ** 2) /
-200
);
const coordinateAsteroid_Z = Math.round(
Math.sqrt(
Math.trunc(zond0.distanceToasteroid() ** 2) -
coordinateAsteroid_X ** 2 -
coordinateAsteroid_Y ** 2
)
);
document.body.innerHTML = `<h3>Координаты астероида задаются произмольно при загрузки страницы</h3><br> Вычесленые координаты с помощью формулы X: ${coordinateAsteroid_X}, Y: ${coordinateAsteroid_Y}, Z: ${coordinateAsteroid_Z}<br><br>
Изначально заданые координаты астероида X: ${asteroid.getX()},...