npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@assanepoi/area-calculator

v1.0.2

Published

A simple module for calculating areas of different shapes

Downloads

164

Readme

Area Calculator

A Node.js module for calculating areas of various shapes and geographical regions. This module now supports basic geometric shapes as well as areas derived from latitude and longitude.

Features

  • Calculate the area of a rectangle.
  • Calculate the area of a circle.
  • Calculate the area of a triangle.
  • Calculate the approximate area between two geographical points (latitude and longitude).
  • Calculate the area of a polygon from an array of geographical coordinates.

Installation

Install the module via npm:

npm install @assanepoi/area-calculator

Usage

Import the module in your Node.js project and start using it:

Example Code

const {
    rectangleArea,
    circleArea,
    triangleArea,
    areaFromLatLon,
    areaFromLatLonArray
} = require('@assanepoi/area-calculator');

try {
    // Rectangle Area
    console.log("Rectangle Area (5x10):", rectangleArea(5, 10)); // Output: 50

    // Circle Area
    console.log("Circle Area (radius 7):", circleArea(7)); // Output: 153.93804002589985

    // Triangle Area
    console.log("Triangle Area (base 6, height 8):", triangleArea(6, 8)); // Output: 24

    // Area from Latitude and Longitude (two points)
    console.log("Area from Lat/Lon:", areaFromLatLon(40.748817, -73.985428, 34.052235, -118.243683)); 

    // Area from Latitude and Longitude Array (polygon)
    const coords = [
        [37.7749, -122.4194],
        [34.0522, -118.2437],
        [36.7783, -119.4179]
    ];
    console.log("Area from Lat/Lon Array:", areaFromLatLonArray(coords));
} catch (error) {
    console.error("Error:", error.message);
}

Functions

rectangleArea(width, height)

Calculates the area of a rectangle.

  • Parameters:
    • width (number): The width of the rectangle. Must be greater than 0.
    • height (number): The height of the rectangle. Must be greater than 0.
  • Returns: The area of the rectangle.

circleArea(radius)

Calculates the area of a circle.

  • Parameters:
    • radius (number): The radius of the circle. Must be greater than 0.
  • Returns: The area of the circle.

triangleArea(base, height)

Calculates the area of a triangle.

  • Parameters:
    • base (number): The base length of the triangle. Must be greater than 0.
    • height (number): The height of the triangle. Must be greater than 0.
  • Returns: The area of the triangle.

areaFromLatLon(lat1, lon1, lat2, lon2)

Calculates the approximate area between two geographical points using their latitude and longitude.

  • Parameters:
    • lat1, lon1 (number): Latitude and longitude of the first point.
    • lat2, lon2 (number): Latitude and longitude of the second point.
  • Returns: The approximate area in square meters.

areaFromLatLonArray(coords)

Calculates the area of a polygon from an array of geographical coordinates.

  • Parameters:
    • coords (array): An array of [latitude, longitude] pairs. Must contain at least 3 coordinates.
  • Returns: The area in square meters.

Error Handling

All functions will throw an error if invalid parameters are provided. Examples:

  • Negative or zero dimensions for geometric shapes.
  • Insufficient coordinates for geographical calculations.

Example

try {
    console.log(rectangleArea(-5, 10)); // Throws Error
} catch (error) {
    console.error(error.message); // Output: Width and height must be greater than 0.
}

License

This project is licensed under the MIT License. See the LICENSE file for details.


Happy coding! 🚀