@id-sdk/extent

Extent class for creating bounding boxes

Usage no npm install needed!

<script type="module">
  import idSdkExtent from 'https://cdn.skypack.dev/@id-sdk/extent';
</script>

README

npm version

@id-sdk/extent

📦 Extent class for creating bounding boxes

Installing

npm install @id-sdk/extent

This library is distributed in ESM format only. It cannot be require()'d from CommonJS. For more, please read Sindre Sorhus’s FAQ.

import { Extent } from '@id-sdk/extent';

Contributing

This project is just getting started! 🌱

We're not able to support external contributors at this time, but check back in a bit when things have matured.

API Reference

Methods
Properties
  • min: Vec2 - minimum corner of the Extent
  • max: Vec2 - maximum corner of the Extent
Types
  • Vec2: [number, number]
  • BBox: { minX: number, minY: number, maxX: number, maxY: number }

Methods

# new Extent(otherOrMin?: Extent | Vec2, max?: Vec2) <>

Constructs a new Extent.

const e1 = new Extent();                // construct an initially empty extent
const e2 = new Extent([0, 0]);          // construct as a point (min and max both [0, 0])
const e3 = new Extent([0, 0], [5, 5]);  // construct as a point with given min and max
const e4 = new Extent(e3);              // copy an Extent to a new Extent

# equals(other: Extent): boolean <>

Test whether extent equals another extent. Returns true if they are equal, false if not.

const a = new Extent([0, 0], [10, 10]);
const b = new Extent([0, 0], [10, 10]);
const c = new Extent([0, 0], [12, 12]);
a.equals(b);   // returns true
a.equals(c);   // returns false

# area(): number <>

Returns the area of an extent.

new Extent([0, 0], [5, 10]).area();  // returns 50

# center(): Vec2 <>

Returns the center point of an extent.

new Extent([0, 0], [5, 10]).center();  // returns [2.5, 5]

# rectangle(): number[4] <>

Returns an array rectangle as [minX, minY, maxX, maxY].

new Extent([0, 0], [5, 10]).rectangle();  // returns [0, 0, 5, 10]

# toParam(): string <>

Returns a string representation of this extent's rectangle formatted as "minX,minY,maxX,maxY". This is often used to request a bounding box from a REST API.

new Extent([0, 0], [5, 10]).toParam();  // returns '0,0,5,10'

# bbox(): BBox <>

Returns a BBox Object with minX, minY, maxX, maxY properties.

new Extent([0, 0], [5, 10]).bbox();  // returns { minX: 0, minY: 0, maxX: 5, maxY: 10 };

# polygon(): Vec2[5] <>

Returns an array of coordinates as a polygon representing the extent wound clockwise.

new Extent([0, 0], [5, 10]).polygon();  // returns [[0, 0], [0, 10], [5, 10], [5, 0], [0, 0]]

# contains(other: Extent): boolean <>

Test whether this extent fully contains another extent. Returns true if it does, false if not.

const a = new Extent([0, 0], [5, 5]);
const b = new Extent([1, 1], [2, 2]);
a.contains(b);   // returns true
b.contains(a);   // returns false

# intersects(other: Extent): boolean <>

Test whether this extent intersects another extent. Returns true if it does, false if not.

const a = new Extent([0, 0], [5, 5]);
const b = new Extent([1, 1], [6, 6]);
a.intersects(b);   // returns true
b.intersects(a);   // returns true

# intersection(other: Extent): Extent <>

Returns a new Extent representing the intersection of this and other extent.

const a = new Extent([0, 0], [5, 5]);
const b = new Extent([1, 1], [6, 6]);
a.intersection(b);   // returns new Extent { min: [ 1, 1 ], max: [ 5, 5 ] }
b.intersection(a);   // returns new Extent { min: [ 1, 1 ], max: [ 5, 5 ] }

# percentContainedIn(other: Extent): number <>

Returns the percent of other extent contained within this extent, by area.

const a = new Extent([0, 0], [4, 1]);
const b = new Extent([3, 0], [4, 2]);
a.percentContainedIn(b);   // returns 0.25
b.percentContainedIn(a);   // returns 0.5

# extend(other: Extent): Extent <>

Extend the bounds of an extent, returning a new Extent. This method does not modify the original or other extents.

const a = new Extent([0, 0], [5, 10]);
const b = new Extent([4, -1], [5, 10]);
const c = a.extend(b);   // returns new Extent { min: [ 0, -1 ], max: [ 5, 10 ] }

# padByMeters(meters: number): Extent <>

Returns a new Extent representing the current extent (assumed to be defined in WGS84 geographic coordinates) padded by given meters. This method does not modify the original extent.

Properties

# min: Vec2
# max: Vec2

All of the Extent methods are designed to be used in an immutable programming style, and return new Extents instead of modifying the original object.

const a = new Extent();
const b = new Extent([0, 0], [5, 5]);
const c = a.extend(b);   // `extend` returns a new Extent, does not modify a or b

However we make the min and max properties publicly mutable, in case you need to modify an extent bounds directly for performance reasons.

const a = new Extent();    // construct an initially empty extent
a.min = [0, 0];            // adjust its min/max later
a.max = [5, 5];

Types

# Vec2

An array of two numbers.

[number, number]

# BBox

An Object containing minX, minY, maxX, maxY numbers.

{ minX: number, minY: number, maxX: number, maxY: number }