` ````
class Point implements IPoint {
static RAD_TO_DEG: number = 180 / Math.PI;
constructor(public x: number, public y: number) {
}
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 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, y: number) {
super(x, y);
}
scale(scale): void {
this.x *= scale;
this.y *= scale;
}
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.length = 2;
obj.angle = Math.PI / 6;
// console.log(obj.length, obj.angle * Point.RAD_TO_DEG);
document.body.innerHTML = 'length: ' + obj.length + '<br>angle: ' + obj.angle * Point.RAD_TO_DEG;
```

999px

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

Alt F
Opt F
Find & Replace

Also see: Tab Triggers