JSFiddle - React, Tailwind, and code Playground
HTML
<div class="cont">
<canvas width="500" height="500" id="test"></canvas>
</div>
CSS
canvas {border: 1px solid;}
JavaScript
var Vector2d = function (x, y) {
this.x = x;
this.y = y;
}
Vector2d.prototype = {
// длина вектора
length: function() {
return Math.sqrt(this.x * this.x + this.y * this.y);
},
// скалярное произведение
dp: function(v) {
return this.x * v.x + this.y * v.y;
},
// сложение векторов
_add: function(v) {
return new Vector2d(this.x + v.x, this.y + v.y);
},
add: function(v) {
this.x += v.x;
this.y += v.y;
return this;
},
// вычитание векторов
_sub: function(v) {
return new Vector2d(this.x - v.x, this.y - v.y);
},
sub: function(v) {
this.x -= v.x;
this.y -= v.y;
return this;
},
// умножение вектора на скаляр
_mult: function(s) {
return new Vector2d(this.x * s, this.y * s);
},
mult: function(s) {
this.x *= s;
this.y *= s;
return this;
},
// нормирование вектора
_normalize: function() {
var length = this.length();
if (length > 0) {
return new Vector2d(this.x / length, this.y / length);
} else {
return new Vector2d(0, 0);
}
},
normalize: function() {
var length = this.length();
if (length > 0) {
this.x /= length;
this.y /= length;
}
return this;
},
// расстояние между векторами
distance: function(v) {
var x = this.x - v.x;
var y = this.y - v.y;
return Math.sqrt(x * x + y * y);
},
// косинус угла между векторами
cos: function(v) {
var a = this._normalize();
var b = v._normalize();
return a.dp(b);
},
// проекция вектора на вектор
proj: function(v) {
var dp1 = this.dp(v);
var dp2 = v.dp(v);
return new Vector2d(v.x, v.y).mult(dp1 / dp2);
},
// левая нормаль
ln: function() {
return new Vector2d(this.y, -this.x);
},
// правая нормаль
rn: function() {
return new Vector2d(-this.y, this.x);
},
zero: function() {
this.x = 0;
this.y = 0;
}
};
var Point = function(points, x, y) {
this.position = new Vector2d(x, y);
this.previous = new Vector2d(x, y);
this.acceleration = new Vector2d(0,...