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 🙏

© 2025 – Pkg Stats / Ryan Hefner

sphere-cap-random

v1.0.2

Published

Randomly sample points on the directed spherical cap

Readme

sphere-cap-random

Obtain random points on directed spherical caps.

In less high-falutin terms—random points on the surface of the Earth inside balls of fire like this one, caused by Vegeta in the series finale of Dragon Ball Z, circa 1996 😍:

Vegeta, © FUNimation

Introduction

An amateur ES2015 module (i.e., works browser and Node.js) to sample random points from a directed spherical cap of the unit sphere (Mathworld, Wikipedia).

The cap may be specified using either an angle (of the cone emanating from the origin) or a height below surface, and is associated with a 3D vector along which lies the cap’s center.

This module uses scijs extensively, if amateurishly.

Example

Click here to interact with a 3D visualization of random samples using plotly.js.

Example

Installation

Node.js npm install --save sphere-cap-random (or yarn add sphere-cap-random for yarn users). If you’re developing this library, add const capRandom = require('./cap-random.min.js'); and use the functions therein.

Download either random-cap.js (for development: sourcemap available) or random-cap.min.js (minified). Both are UMD—universal modules and can be used in either browser or server-side.

Browser Add <script src="cap-random.min.js"></script> to your HTML. This loads a global capRandom object which contains all the goodies.

API Usage

capRandom.sampleSphericalCap([params])

sampleSphericalCap() returns a 3×1 vector, as a scijs ndarray, drawn randomly from the spherical cap centered at the North Pole of the unit sphere with unit height—in other words, the North Hemisphere.

sampleSphericalCap(params) looks for the following slots in the params object:

  • N: return an 3×N array whose columns are the random points on the spherical cap;
  • z: the height above the xy-plane at the bottom of the cap. By default, z is 0, which describes the Northern Hemisphere; z=-1 would consider the entire sphere, rather than a cap.
  • deg: the angle in degrees of the cone emanating from the origin and expanding towards the North Pole, and whose intersection with the sphere’s surface describes the spherical cap. And,
  • rad: this same angle in radians.

The last three of these are all equivalent—specifying z or deg or rad sets the other two, but the code looks at these in this order.

capRandom.ndarrayToNative(...)

If you don’t want to deal with scijs ndarrays in your JavaScript code, this convenience function unpacks an M×P ndarray into a native JavaScript array of length M, each element of which is in turn a P-long array.

let [x, y, z] = capRandom.ndarrayToNative(capRandom.sampleSphericalCap({N: 10}));

This will give you three arrays, each with ten elements, specifying the XYZ components of the ten requested samples.

capRandom.ndarrayColsToNative(...)

This convenience function is offered for completeness. It transposes a scijs ndarray before unpacking into a JavaScript array.

let points = capRandom.ndarrayColsToNative(capRandom.sampleSphericalCap({N: 10}));

With this you get ten 3-long arrays.

capRandom.sampleDirectedSphericalCap(direction[, params])

Whereas sampleSphericalCap considered the spherical cap centered on the North Pole, this function instead considers the spherical cap centered on the intersection of a direction 3-vector with the surface of the sphere (in the image above, the cause of the explosion: Vegeta).

sampleDirectedSphericalCap can take the same parameter object as sampleSphericalCap: you may specify the number of samples via N, and/or the z-dimension at the bottom of the cap (were it still centered at the North Pole), or the angle of the cone describing the cap via deg or rad.

This function works by passing on the optional parameter object params to sampleSphericalCap and rotating the resulting point(s) via an axis–angle rotation to the desired direction.

Others

There are a number of other functions exported by the module (see index.js): these are experimental, or will be extracted into their own stand-alone scijs-oriented packages.

Background

This module started life as a Matlab function on a StackOverflow answer, cobbled together using insights from @joriki and @Jim Belk.

Mike Bostock’s d3.js 4.0 inspired me to investigate Rollup and ES2015 modules. What a shock.

Thanks to the creator and contributors of scijs and ndarray. Here’s to more adventures 🍻.