Class Version of Complex Numbers
class ComplexNumber {
constructor(x, y) {
this.x = x;
this.y = y;
this.mag = Math.sqrt(this.x * this.x + this.y * this.y);
this.ang = Math.atan(y, x);
}
static fromMagAng(mag, ang) {
const x = mag * Math.cos(ang);
const y = mag * Math.sin(ang);
const c = new ComplexNumber(x, y);
c.mag = mag;
c.ang = ang;
return c;
}
get realPart() {
return this.x;
}
get imagPart() {
return this.y;
}
get magnitude() {
return this.mag;
}
get angle() {
return this.ang;
}
}
const c = new ComplexNumber(1, 2);
console.log(c.realPart);
console.log(c.imagPart);
console.log(c.magnitude);
console.log(c.angle);
no comments