Line

Line class provides static functions to create and operate on lines. A line is usually represented as a Group of 2 Pts. You can use the static functions as-is, or apply the Group.op or Pt.op to enable functional programming. See Op guide for details.

Class in src/Op.ts

Methods

STATIC collinear ( p1, p2, p3, threshold )

Check if three Pts are collinear, ie, on the same straight path.

parameters
p1: PtLike

first Pt

p2: PtLike

second Pt

p3: PtLike

third Pt

threshold: number (default value: 0.01)

a threshold where a smaller value means higher precision threshold for the straight line. Default is 0.01.

returns
boolean

source

STATIC crop ( line, size, index, cropAsCircle )

Crop this line by a circle or rectangle at end points. This can be useful for creating arrows that connect to an object's edge.

parameters
line: PtIterable

a Group or an Iterable representing a line to crop

size: PtLike

size of circle or rectangle as Pt

index: number (default value: 0)

line's end point index, ie, 0 = start and 1 = end.

cropAsCircle: boolean (default value: true)

a boolean to specify whether the size parameter should be treated as circle. Default is true.

returns
Pt

an intersecting point on the line that can be used for cropping.

source

STATIC distanceFromPt ( line, pt )

Given a line and a point, find the shortest distance from the point to the line.

parameters
line: GroupLike

a Group of 2 Pts

pt: PtLike | number[]

a Pt

returns
number

see

Line.perpendicularFromPt

source

STATIC fromAngle ( anchor, angle, magnitude )

Create a line that originates from an anchor point, given an angle and a magnitude.

parameters
anchor: PtLike

an anchor Pt

angle: number

an angle in radian

magnitude: number

magnitude of the line

returns
Group

a Group of 2 Pts representing a line segement

source

STATIC intercept ( p1, p2 )

Calculate the slope and xy intercepts of a line.

parameters
p1: PtLike

line's first end point

p2: PtLike

line's second end point

returns
{ slope:`number`, xi:`number`, yi:`number` }

an object with slope, xi, yi properties

source

STATIC intersectGridWithLine2D ( line, gridPt )

Get two intersection Pts of a line segment with a 2D grid point.

parameters
line: GroupLike

a ray specified by 2 Pts

gridPt: PtLike | number[]

a Pt on the grid

returns
Group

a group of two intersecting Pts. The first one is horizontal intersection and the second one is vertical intersection.

source

STATIC intersectGridWithRay2D ( ray, gridPt )

Get two points of a ray that intersects with a point on a 2D grid.

parameters
ray: PtIterable

a Group or an Iterable representing a ray

gridPt: PtLike

a Pt on the grid

returns
Group

a group of two intersecting Pts. The first one is horizontal intersection and the second one is vertical intersection.

source

STATIC intersectLine2D ( la, lb )

Given two line segemnts, find their intersection point if any.

parameters
la: PtIterable

a Group or an Iterable with 2 Pt representing a line segment

lb: PtIterable

a Group or an Iterable with 2 Pt representing a line segment

returns
Pt

an intersection Pt or undefined if no intersection

source

STATIC intersectLines2D ( lines1, lines2, isRay )

Find intersection points of 2 sets of lines. This checks all line segments in the two lists. Consider using a bounding-box check before calling this. If you are checking convex polygon intersections, using Polygon.intersectPolygon2D will be more efficient.

parameters
lines1: Iterable

an Array/Iterable of (Groups or Iterables)

lines2: Iterable

an Array/Iterable of (Groups or Iterables)

isRay: boolean (default value: false)

a boolean value to treat the line as a ray (infinite line). Default is false.

returns
Group

source

STATIC intersectLineWithRay2D ( line, ray )

Given a line segemnt and a ray (infinite line), find their intersection point if any.

parameters
line: PtIterable

a Group of 2 Pts representing a line segment

ray: PtIterable

a Group of 2 Pts representing a ray

returns
Pt

an intersection Pt or undefined if no intersection

source

STATIC intersectPolygon2D ( lineOrRay, poly, sourceIsRay )

Given a line segemnt or a ray (infinite line), find its intersection point(s) with a polygon.

parameters
lineOrRay: PtIterable

a Group or an Iterable with 2 Pt representing a line or ray

poly: PtIterable

a Group or an Iterable representing a polygon

sourceIsRay: boolean (default value: false)

a boolean value to treat the line as a ray (infinite line). Default is false.

returns
Group

source

STATIC intersectRay2D ( la, lb )

Given two lines as rays (infinite lines), find their intersection point if any.

parameters
la: PtIterable

a Group or an Iterable with 2 Pt representing a ray

lb: PtIterable

a Group or an Iterable with 2 Pts representing another ray

returns
Pt

an intersection Pt or undefined if no intersection

source

STATIC intersectRect2D ( line, rect )

An easy way to get rectangle-line intersection points. For more optimized implementation, store the rectangle's sides separately (eg, Rectangle.sides()) and use Polygon.intersectPolygon2D().

parameters
line: GroupLike

a Group representing a line

rect: GroupLike

a Group representing a rectangle

returns
Group

a Group of intersecting Pts

source

STATIC magnitude ( line )

Get magnitude of a line segment.

parameters
line: PtIterable

a Group or an Iterable with at least 2 Pt

returns
number

source

STATIC magnitudeSq ( line )

Get squared magnitude of a line segment.

parameters
line: PtIterable

returns
number

source

STATIC marker ( line, size, graphic, atTail )

Create an marker arrow or line, placed at an end point of this line.

parameters
line: PtIterable

a Group or an Iterable representing a line to place marker

size: PtLike

size of the marker as Pt

graphic: string (default value: ("arrow"||"line"))

either "arrow" or "line"

atTail: boolean (default value: true)

a boolean, if true, the marker will be positioned at tail of the line (ie, index = 1). Default is true.

returns
Group

a Group that defines the marker's shape

source

STATIC perpendicularFromPt ( line, pt, asProjection )

Find a point on a line that is perpendicular (shortest distance) to a target point.

parameters
line: PtIterable

a Group or an Iterable that defines a line

pt: PtLike

a target Pt

asProjection: boolean (default value: false)

if true, this returns the projection vector instead. Default is false.

returns
Pt

a Pt on the line that is perpendicular to the target Pt, or a projection vector if asProjection is true.

source

STATIC sideOfPt2D ( line, pt )

Given a 2D path and a point, find whether the point is on left or right side of the line.

parameters
line: PtLikeIterable

a Group or an Iterable representing a line

pt: PtLike

a Pt or numeric array

returns
number

a negative value if on left and a positive value if on right. If collinear, then the return value is 0.

source

STATIC slope ( p1, p2 )

Calculate the slope of a line.

parameters
p1: PtLike

line's first end point

p2: PtLike

line's second end point

returns
number

source

STATIC subpoints ( line, num )

Get evenly distributed points on a line. Similar to Create.distributeLinear but excluding end points.

parameters
line: PtLikeIterable

a Group or an Iterable representing a line

num: number

number of points to get

returns
Group

source

STATIC toRect ( line )

Convert this line to a new rectangle representation.

parameters
line: GroupLike

a Group representing a line

returns
Group

source