line-segment-ops

1.0.1 • Public • Published

line-segment-ops

Set Theory and Topological Relationships on Line Segments

Installation

Install with npm.

npm install --save line-segment-ops

Examples

Create Interval

Interval = require('line-interval-ops')

i = new Interval("1,2")             #from string
i = new Interval("1 2")             #from string
i = new Interval("1;2")             #from string

i = new Interval([1,2])             #from array

i = new Interval from: 1, to:2      #object
i = new Interval start: 1, end:2    #object
i = new Interval a: 1, b:2          #object

i = new Interval(1,2)               #from two numbers

##Spatial Relationships

I = require('line-interval-ops')

I(2, 8).within(I(1, 10))    #true
I(2, 8).within(I(1, 10))    #false, endpoints touching

I(1,1)                      #degenerate => true

I(2, 8).within(I(1, 10))    #disjoint => true

#Set Operations

Union

I(1, 10).union I(6, 13), I(3, 9)
# => I(1, 13)
I(1, 10).union I(12, 15)
# => [I(1, 10), I(12, 15)]

XOR

I(1, 10).xor I(6, 12)
# => [ I(1, 6), I(10, 12)]

API

Class Summary
Interval The Interval class represents a line segment on the the number line.

IntervalCLASS Back to Class List

The Interval class represents a line segment on the the number line.

An interval is a connected portion of the real line.

Also see A Small Set of Formal Topological Relationships Suitable for End User Interaction

A couple of definitions:

  • The boundary of an Interval is set of its two endpoints.
  • The interior of an Interval is the set of all points in the Interval less its boundary (endpoints).
  <tr>
    <td><code>:: <b>a</b>  </code></td>
    <td width="8%" align="center"><sub>public</sub></td>
    <td width="8%" align="center"><sub>instance</sub></td>
    <td width="8%" align="center"><sub><a href="#class-Interval">Interval</a></sub></td>
  </tr>
  <tr>
    <td><code>:: <b>b</b>  </code></td>
    <td width="8%" align="center"><sub>public</sub></td>
    <td width="8%" align="center"><sub>instance</sub></td>
    <td width="8%" align="center"><sub><a href="#class-Interval">Interval</a></sub></td>
  </tr>
  <tr>
    <td><code>:: <b>degenerate</b>  </code></td>
    <td width="8%" align="center"><sub>public</sub></td>
    <td width="8%" align="center"><sub>instance</sub></td>
    <td width="8%" align="center"><sub><a href="#class-Interval">Interval</a></sub></td>
  </tr>

Properties

Number The start/left endpoint of this Interval

</td>

Number The end/right endpoint of this Interval

</td>

Boolean True if this `{Instance}` has the same start and end points

</td>

Methods

:: constructor( arg1[, arg2] ) public instance Interval
  <p>Creates a immutable <a href="https://github.com/venkatperi/line-segment-ops/blob/v1.0.0/lib/Interval.coffee#L20">Interval</a> object</p>

arg1 can be a:

  • String: <number> <sep> <number> where sep can be any one of a comma, semicolon, or a space
  • Array of two Numbers
  • Object with one of these key combinations: {from, to} {start, end} {a, b}
  • a Number, in which case arg2 must be defined
</td>
:: contains( other ) public instance Interval
  <p>Checks if this <a href="https://github.com/venkatperi/line-segment-ops/blob/v1.0.0/lib/Interval.coffee#L20">Interval</a> contains the other.</p>

  <p>  <em>Returns</em></p>
</td>
:: overlaps( other ) public instance Interval
  <p>Checks if this <a href="https://github.com/venkatperi/line-segment-ops/blob/v1.0.0/lib/Interval.coffee#L20">Interval</a> overlaps another.</p>

For two Intervals to overlap they must have some points but not all points.

  <p>  <em>Returns</em></p>
</td>
:: within( other ) public instance Interval
  <p>Checks if this <a href="https://github.com/venkatperi/line-segment-ops/blob/v1.0.0/lib/Interval.coffee#L20">Interval</a> is fully within another.</p>

Interval X is said to be within another Interval Y if X is completely within Y and neither of their endpoints touch.

  <p>  <em>Returns</em></p>
</td>
:: touches( other ) public instance Interval
  <p>Checks if this <a href="https://github.com/venkatperi/line-segment-ops/blob/v1.0.0/lib/Interval.coffee#L20">Interval</a> touches anothe.</p>

Two line segments touch, if:

  • one their endpoints touch
  • their interiors do not share any common points
  <p>  <em>Returns</em></p>
</td>
:: disjoint( other ) public instance Interval
  <p>Checks if this <a href="https://github.com/venkatperi/line-segment-ops/blob/v1.0.0/lib/Interval.coffee#L20">Interval</a> is <code>disjoint</code> with another.</p>

Two Intervals are disjoint if they have no points in common, i.e. if their intersection is the empty set.

  <p>  <em>Returns</em></p>
</td>
:: union( others ) public instance Interval
  • others {ArrayInterval} One or more intervals
  <p>Calculates the union of the given `{Intervals}`</p>

A union of intervals can result in an array of unconnected parts.

  <p>  <em>Returns</em></p>
</td>
:: intersection( other ) public instance Interval
  <p>Calculates the intersection, i.e. the points where they concur.</p>

  <p>  <em>Returns</em></p>
  • Returns an Interval with the intersection or `` if the two do not intersect.
</td>
:: difference( other ) public instance Interval
  <p>Calculates the difference between this <a href="https://github.com/venkatperi/line-segment-ops/blob/v1.0.0/lib/Interval.coffee#L20">Interval</a> and another.</p>

The difference between Interval X and Y is all of the points in X which are not in Y.

  <p>  <em>Returns</em></p>
</td>
:: xor( other ) public instance Interval
  <p>Compute an XOR with the given <a href="https://github.com/venkatperi/line-segment-ops/blob/v1.0.0/lib/Interval.coffee#L20">Interval</a></p>

The set of elements belonging to one but not both of two given sets. It is therefore the union of the complement of A with respect to B and B with respect to A, and corresponds to the XOR operation in Boolean logic.

  <p>  <em>Returns</em></p>
</td>
:: equals( other ) public instance Interval
  <p>Check if both <a href="https://github.com/venkatperi/line-segment-ops/blob/v1.0.0/lib/Interval.coffee#L20">Interval</a>s are equal.</p>

Two intervals are equal if their line segments are equal, i.e same start and end points.

  <p>  <em>Returns</em></p>
</td>
:: toString( ) public instance Interval
  <p>Get a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">String</a> representation of this <a href="https://github.com/venkatperi/line-segment-ops/blob/v1.0.0/lib/Interval.coffee#L20">Interval</a></p>

Return String

</td>

Markdown generated by [atomdoc-md](https://github.com/venkatperi/atomdoc-md).

Readme

Keywords

none

Package Sidebar

Install

npm i line-segment-ops

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • venkatperi