Options
All
• Public
• Public/Protected
• All

# Class 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 function as-is, or apply the `op` method in Group or Pt to many of these functions. See Op guide for details.

• Line

## Methods

### Static collinear

• collinear(p1: PtLike | number[], p2: PtLike | number[], p3: PtLike | number[], threshold?: number): boolean
• Check if three Pts are collinear, ie, on the same straight path.

#### Parameters

first Pt

second Pt

third Pt

• ##### Default value threshold: number = 0.01

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

### Static crop

• Crop this line by a circle or rectangle at end point.

#### Parameters

• ##### line: GroupLike

line to crop

• ##### size: PtLike

size of circle or rectangle as Pt

• ##### Default value index: number = 0

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

• ##### Default value cropAsCircle: boolean = 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.

### Static distanceFromPt

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

see

`Line.perpendicularFromPt`

#### Parameters

• ##### line: GroupLike

a Group of 2 Pts

a Pt

### Static fromAngle

• fromAngle(anchor: PtLike, angle: number, magnitude: number): Group
• Create a line by "drawing" 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

### Static intercept

• intercept(p1: PtLike | number[], p2: PtLike | number[]): object
• Calculate the slope and xy intercepts of a line

#### Parameters

• ##### p1: PtLike | number[]

line's first end point

• ##### p2: PtLike | number[]

line's second end point

#### Returns object

an object with `slope`, `xi`, `yi` properties

### Static intersectGridWithLine2D

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

#### Parameters

• ##### 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.

### Static intersectGridWithRay2D

• Get two intersection Pts of a ray with a 2D grid point

#### Parameters

• ##### ray: 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.

### Static intersectLine2D

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

#### Parameters

• ##### la: GroupLike

a Group of 2 Pts representing a line segment

• ##### lb: GroupLike

a Group of 2 Pts representing a line segment

#### Returns Pt

an intersection Pt or undefined if no intersection

### Static intersectLineWithRay2D

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

#### Parameters

• ##### line: GroupLike

a Group of 2 Pts representing a line segment

• ##### ray: GroupLike

a Group of 2 Pts representing a ray

#### Returns Pt

an intersection Pt or undefined if no intersection

### Static intersectLines2D

• Find intersection points of 2 polygons. This checks all line segments in the two lists. Consider using a bounding-box check before calling this.

#### Parameters

• ##### lines1: GroupLike[]

an array of line segments

• ##### lines2: GroupLike[]

an array of line segments

• ##### Default value isRay: boolean = false

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

### Static intersectPolygon2D

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

#### Parameters

• ##### lineOrRay: GroupLike

a Group of 2 Pts representing a line or ray

• ##### poly: GroupLike

a Group of Pts representing a polygon

• ##### Default value sourceIsRay: boolean = false

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

### Static intersectRay2D

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

#### Parameters

• ##### la: GroupLike

a Group of 2 Pts representing a ray

• ##### lb: GroupLike

a Group of 2 Pts representing a ray

#### Returns Pt

an intersection Pt or undefined if no intersection

### Static intersectRect2D

• Quick way to check rectangle intersection. 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

### Static magnitude

• Get magnitude of a line segment

#### Parameters

• ##### line: GroupLike

a Group of at least 2 Pts

### Static magnitudeSq

• Get squared magnitude of a line segment

#### Parameters

• ##### line: GroupLike

a Group of at least 2 Pts

### Static marker

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

#### Parameters

• ##### line: GroupLike

line to place marker

• ##### size: PtLike

size of the marker as Pt

• ##### Default value graphic: string = ("arrow"||"line")

either "arrow" or "line"

• ##### Default value atTail: boolean = 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

### Static perpendicularFromPt

• perpendicularFromPt(line: GroupLike, pt: PtLike | number[], asProjection?: boolean): Pt
• Find a point on a line that is perpendicular (shortest distance) to a target point

#### Parameters

• ##### pt: PtLike | number[]

a target Pt

• ##### Default value asProjection: boolean = 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.

### Static sideOfPt2D

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

#### Parameters

• ##### line: GroupLike

a Group of at least 2 Pts

a Pt

#### Returns number

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

### Static slope

• slope(p1: PtLike | number[], p2: PtLike | number[]): number
• Calculate the slope of a line

#### Parameters

• ##### p1: PtLike | number[]

line's first end point

• ##### p2: PtLike | number[]

line's second end point

### Static subpoints

• Get evenly distributed points on a line

#### Parameters

• ##### line: GroupLike | number[][]

a Group representing a line

• ##### num: number

number of points to get

### Static toRect

• Convert this line to a rectangle representation

#### Parameters

• ##### line: GroupLike

a Group representing a line

#### Returns Group

Generated using TypeDoc