-
-
Save byTimo/c51bc16d8c48e676da840f3f892b14c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Задание: | |
Напишите TypeScript типы для этого кода | |
*/ | |
class Vector { | |
constructor(x, y) { | |
this.x = x; | |
this.y = y; | |
this.length = Math.sqrt(x * x + y * y); | |
this.angle = Math.atan2(y, x); | |
} | |
add(vector) { | |
return new Vector(this.x + vector.x, this.y + vector.y); | |
} | |
sub(vector) { | |
return new Vector(this.x - vector.x, this.y - vector.y); | |
} | |
negate() { | |
return new Vector(-this.x, -this.y); | |
} | |
dot(vector) { | |
return this.x * vector.x + this.y * vector.y; | |
} | |
} | |
Vector.zero = new Vector(0, 0); | |
Vector.up = new Vector(0, 1); | |
Vector.down = new Vector(0, -1); | |
Vector.left = new Vector(-1, 0); | |
Vector.right = new Vector(1, 0); | |
Vector.equals = function (a, b) { | |
return a.x === b.x && a.y === b.y; | |
} | |
Vector.isVector = function (obj) { | |
return obj instanceof Vector; | |
} | |
function createVector(...args) { | |
if (args.length === 2) { | |
return new Vector(args[0], args[1]); | |
} | |
if (Array.isArray(args[0])) { | |
return new Vector(args[0][0], args[0][1]); | |
} | |
return new Vector(args[0].x, args[0].y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment