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

graham_scan

v1.0.5

Published

Implementation of the Graham Scan algorithm to calculate a convex hull from a given array of x, y coordinates.

Downloads

9,502

Readme

JavaScript Graham's Scan Convex Hull Algorithm

I required a simple implementation to calculate a convex hull from a given array of x, y coordinates, the convex hull's in js I found either were a little buggy, or required dependencies on other libraries. This implementation just takes the x,y coordinates, no other libraries are needed.

These four examples show how to utilise with Google Maps:

Example 1 Example 2 Example 3 Example 4

View GitHub pages

Building

This produces graham_scan.min.js:

npm install
grunt build

Testing

The source is tested with qUnit, tests executed with Google's JS Test Driver.

Usage

//Create a new instance.
var convexHull = new ConvexHullGrahamScan();

//add points (needs to be done for each point, a foreach loop on the input array can be used.)
convexHull.addPoint(x, y);

//getHull() returns the array of points that make up the convex hull.
var hullPoints = convexHull.getHull();

Algorithm

GRAHAM_SCAN(Q)
    Find p0 in Q with minimum y-coordinate (and minimum x-coordinate if there are ties).
    Sort the remaining points of Q (that is, Q − {p0}) by polar angle in counterclockwise order with respect to p0.
    TOP [S] = 0                ▷ Lines 3-6 initialize the stack to contain, from bottom to top, first three points.
    PUSH (p0, S)
    PUSH (p1, S)
    PUSH (p2, S)
    for i = 3 to m             ▷ Perform test for each point p3, ..., pm.
        do while the angle between NEXT_TO_TOP[S], TOP[S], and pi makes a non-left turn  ▷ remove if not a vertex
            do POP(S)
            PUSH (S, pi)
    return S

References

  • http://www.personal.kent.edu/~rmuhamma/Compgeometry/MyCG/ConvexHull/GrahamScan/grahamScan.htm
  • http://en.wikipedia.org/wiki/Graham_scan

License

MIT License