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 😍:
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.
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.
<script src="cap-random.min.js"></script> to your HTML. This loads a global
capRandom object which contains all the goodies.
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
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,
zis 0, which describes the Northern Hemisphere;
z=-1would 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
rad sets the other two, but the code looks at these in this order.
If you don’t want to deal with scijs
M, each element of which is in turn a
let x y z = capRandom;
This will give you three arrays, each with ten elements, specifying the XYZ components of the ten requested samples.
This convenience function is offered for completeness. It transposes a scijs
let points = capRandom;
With this you get ten 3-long arrays.
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
This function works by passing on the optional parameter object
sampleSphericalCap and rotating the resulting point(s) via an axis–angle rotation to the desired direction.
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.
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 🍻.