` ````
class Point implements IPoint {
constructor(public x: number = 0, public y: number = 0) {
}
get length(): number {
var square = this.x * this.x + this.y * this.y;
return Math.sqrt(square);
}
set length(length: number) {
var point: IPoint = Point.getPolar(length, this.angle);
this.x = point.x;
this.y = point.y;
}
get angle(): number {
return Math.atan2(this.y, this.x);
}
set angle(angle: number) {
var point: IPoint = Point.getPolar(this.length, angle);
this.x = point.x;
this.y = point.y;
}
static get RAD_TO_DEG(): number {
return 180 / Math.PI;
}
static polar(length: number, angle: number): Point {
var point: IPoint = Point.getPolar(length, angle);
return new Point(point.x, point.y);
}
protected static getPolar(length: number, angle: number): IPoint {
var x: number = length * Math.cos(angle);
var y: number = length * Math.sin(angle);
return {x: x, y: y};
}
}
class Vector extends Point {
constructor(x: number = 0, y: number = 0) {
super(x, y);
}
scale(scaleX: number, scaleY?: number): void {
this.x *= scaleX;
this.y *= (isNaN(scaleY) ? scaleX : scaleY);
}
add(point: IPoint): void {
this.x += point.x;
this.y += point.y;
}
static polar(length: number, angle: number): Vector {
var point: IPoint = Point.getPolar(length, angle);
return new Vector(point.x, point.y);
}
}
interface IPoint {
x: number;
y: number;
}
var obj: Vector = Vector.polar(1, Math.PI / 3);
obj.scale(2);
// Point.RAD_TO_DEG = 0;
document.body.innerHTML = 'length: ' + obj.length + '<br>angle: ' + obj.angle * Point.RAD_TO_DEG;
```

999px

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Loading
..................

Alt
Opt
Find & Replace

Also see: Tab Triggers