FiguresJS contains implementations for some simple 2D geometrical figures.
This is a Node.js module available through the npm registry.
Before installing, download and install Node.js. Node.js 0.10 or higher is required.
If this is a brand new project, make sure to create a package.json first with
the npm init command.
Installation is done using the
npm install command:
$ npm install figuresjs- Figure area and perimeter calculation
- Figure area and perimeter caching
- Easy to inherit new figures
Circle class:
const circle = new Circle(5);
const circleArea = circle.area; // pi * 5 ^ 2
circle.radius = 7;
const circlePerimeter = circle.perimeter; // 2 * pi * 7Square class:
const square = new Square(4);
const squareDiagonal = square.diagonal; // 4 * sqrt(2)And so on.
To implement new figure, that is not in any examples provided, you should extend nearest class. Than you should implement calculateArea and calculatePerimeter (if needed) methods corresponding to the figure being implemented.
Example:
class Rhombus extends Quadrilateral {
private _diagonals: [number, number];
constructor(side: number, diagonals: [number, number]) {
super([side, side, side, side]);
diagonals.forEach((d) => this.checkForNegative(d, "Diagonal"));
this._diagonals = diagonals;
}
protected calculateArea(): void {
this._area = this._diagonals[0] * this._diagonals[1] / 2;
}
}