figuresjs
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

FiguresJS

FiguresJS contains implementations for some simple 2D geometrical figures.

Installation

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

Features

  • Figure area and perimeter calculation
  • Figure area and perimeter caching
  • Easy to inherit new figures

Examples

Circle class:

const circle = new Circle(5);

const circleArea = circle.area; // pi * 5 ^ 2

circle.radius = 7;

const circlePerimeter = circle.perimeter; // 2 * pi * 7

Square class:

const square = new Square(4);

const squareDiagonal = square.diagonal; // 4 * sqrt(2)

And so on.

New Figures

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;
    }
}

Readme

Keywords

none

Package Sidebar

Install

npm i figuresjs

Weekly Downloads

1

Version

1.0.1

License

ISC

Unpacked Size

39.5 kB

Total Files

42

Last publish

Collaborators

  • pazaderey