C

LineSegment

This class represents a line segment in the plane.
ImplementsInheritance Hierarchy

Remarks

A line segment is defined by its two endpoints.

Members

No filters for this type

Constructors

Returns a new LineSegment.

Parameters

p1: Point
The first end point of the line segment.
p2: Point
The second end point of the line segment.

Properties

Gets the smallest rectangle that contains the object.
readonlyfinal
Gets the distance from start to end point in x-coordinates.
readonlyfinal

Property Value

the distance from start to end point in x-coordinates.
Gets the distance from start to end point in y-coordinates.
readonlyfinal

Property Value

the distance from start to end point in y-coordinates.
Gets the first endpoint of the line segment.
readonlyfinal
Gets the second endpoint of the line segment.
readonlyfinal
Gets the slope of the line segment.
readonlyfinal

Property Value

the slope of the line segment.
Gets the y-value of the line for x-coordinate 0.
readonlyfinal

Methods

Determines whether a specified point lies on this line segment, within an optional tolerance.

This method checks whether the specified point lies within the bounds of this line segment, taking into account an optional tolerance eps.

This method is particularly useful for determining point containment on line segments in the presence of floating-point inaccuracies or when a precise match is not feasible.

final

Parameters

point: Point
The point to check.
eps?: number
The non-negative tolerance to apply when checking if the point lies on the line segment. Defaults to 0. A small tolerance, such as 1e-8, can be used to account for floating-point precision errors.

Return Value

boolean
true if the point lies on the line segment within the specified tolerance; otherwise, false.
Checks whether the line segment intersects a box.
final

Parameters

box: Rect
A rectangle.

Return Value

boolean
true if the line segment intersects the box, false otherwise.
Determines whether this segment is considered horizontal, meaning that the y-coordinate of its endpoints differ by at most the specified tolerance.

A segment is considered horizontal if the absolute difference between the y-coordinates of its endpoints is less than the specified tolerance eps. If eps is not provided, the method checks for an exact match (eps = 0).

This method is particularly useful when working with geometric segments where small inaccuracies in floating-point arithmetic might otherwise cause a segment to be incorrectly classified as non-horizontal.

final

Parameters

eps?: number
The allowable difference in y-coordinates to consider the segment horizontal. Defaults to 0. A small tolerance such as 1e-8 can be used to account for floating-point precision errors.

Return Value

boolean
true if the y-coordinates of the endpoints of this segment differ by less than eps, indicating that the segment is considered horizontal; otherwise, false.
Determines whether a specified x-coordinate lies within the projection of the line segment on the x-axis, considering an optional tolerance.

The method checks if the given x-coordinate x falls within the x-axis interval defined by the endpoints of the line segment, optionally reducing the interval by the tolerance eps at both ends of the interval.

This method is useful in scenarios where you need to verify that a point falls within the horizontal range of a line segment, particularly when accounting for minor inaccuracies in floating-point calculations.

final

Parameters

x: number
The x-coordinate to check.
eps?: number
The tolerance to apply when checking if the x-coordinate is within the interval. Defaults to 0. A small tolerance, such as 1e-8, can be used to account for floating-point precision errors.

Return Value

boolean
true if the x-coordinate lies within the x-axis projection of the line segment, considering the specified tolerance; otherwise, false.
Determines whether a specified y-coordinate lies within the projection of the line segment on the y-axis, considering an optional tolerance.

The method checks if the given y-coordinate y falls within the y-axis interval defined by the endpoints of the line segment, optionally reducing the interval by the tolerance eps at both ends of the interval.

This method is useful in scenarios where you need to verify that a point falls within the vertical range of a line segment, particularly when accounting for minor inaccuracies in floating-point calculations.

final

Parameters

y: number
The y-coordinate to check.
eps?: number
The tolerance to apply when checking if the y-coordinate is within the interval. Defaults to 0. A small tolerance, such as 1e-8, can be used to account for floating-point precision errors.

Return Value

boolean
true if the y-coordinate lies within the y-axis projection of the line segment, considering the specified tolerance; otherwise, false.
Determines whether this segment is considered vertical, meaning that the x-coordinate of its endpoints differ by at most the specified tolerance.

A segment is considered vertical if the absolute difference between the x-coordinates of its endpoints is less than the specified tolerance eps. If eps is not provided, the method checks for an exact match (eps = 0).

This method is particularly useful when working with geometric segments where small inaccuracies in floating-point arithmetic might otherwise cause a segment to be incorrectly classified as non-vertical.

final

Parameters

eps?: number
The allowable difference in x-coordinates to consider the segment vertical. Defaults to 0. A small tolerance such as 1e-8 can be used to account for floating-point precision errors.

Return Value

boolean
true if the x-coordinates of the endpoints of this segment differ by less than eps, indicating that the segment is considered vertical; otherwise, false.
Returns the length of the line segment, this is the value of the Euclidean norm.
final

Return Value

number
a value > 0.
String representation of the line.
final

Return Value

string
A string representation of the line.

Static Methods

Checks whether a line segment intersects a box.
static

Parameters

box: Rect
A rectangle.
s: Point
First end point of the line segment.
t: Point
Second end point of the line segment.

Return Value

boolean
true if the line segment intersects the box, false otherwise.
Checks whether a line segment intersects a box.
Implemented using the Cohen-Sutherland algorithm.
static

Parameters

box: Rect
A rectangle
x1: number
x-coordinate of the first end point of the line segment
y1: number
y-coordinate of the first end point of the line segment
x2: number
x-coordinate of the second end point of the line segment
y2: number
y-coordinate of the second end point of the line segment

Return Value

boolean
true if the line segment intersects the box, false otherwise.
Checks whether a line segment intersects a paraxial box.
Implemented using the Cohen-Sutherland algorithm.
static

Parameters

boxX1: number
x-coordinate of the upper left corner of the box
boxY1: number
y-coordinate of the upper left corner of the box
boxX2: number
x-coordinate of the lower right corner of the box
boxY2: number
y-coordinate of the lower right corner of the box
sX1: number
x-coordinate of the first end point of the line segment
sY1: number
y-coordinate of the first end point of the line segment
sX2: number
x-coordinate of the second end point of the line segment
sY2: number
y-coordinate of the second end point of the line segment

Return Value

boolean
true if the line segment intersects the box, false otherwise.
Determines the intersection point between two line segments, if they intersect.

This method computes the intersection point of the line segments s1 and s2. If the line segments intersect within the given tolerance eps, the method returns the intersection point as a Point. If there is no intersection or if the two line segments overlap, i.e., there is no unique intersection point, the method returns null.

The method takes into account a small tolerance to handle potential floating-point inaccuracies. The line segments are treated as if they were shortened by the tolerance on both ends. This is useful to prevent intersections from being returned due to precision errors in cases where two line segments are close, but do not intersect. Moreover, a small tolerance can be used to model intersections of open line segments, i.e., if only the endpoint of a line segment lies on the other line segment, this shall not be treated as an intersection.

static

Parameters

s1: LineSegment
The first line segment.
s2: LineSegment
The second line segment.
eps?: number
The non-negative tolerance to apply when checking for intersection. Defaults to 0. A small tolerance, such as 1e-8, can be used to account for floating-point precision errors.

Return Value

Point
A Point representing the intersection point of the two line segments, or null if the segments do not intersect within the specified tolerance or if they overlap.
Determines whether the specified points define a horizontal line segment within a given tolerance.

A horizontal line segment is characterized by having the same y-coordinate for both points. This method checks if the absolute difference between the y-coordinates of p1 and p2 is less than the specified tolerance eps. If eps is not provided, the method defaults to checking for an exact match (eps = 0).

This method is useful in geometric computations where exact precision is challenging due to floating-point arithmetic limitations. By allowing a small tolerance, it accommodates minor computational inaccuracies.

static

Parameters

p1: Point
The first point.
p2: Point
The second point.
eps?: number
The allowable difference in y-coordinates to consider the points as forming a horizontal line segment. Defaults to 0. A typical value for a small tolerance is 1e-8.

Return Value

boolean
true if the x-coordinate of p1 and p2 differ by less than eps, indicating that the points define a horizontal line segment; otherwise, false.
Determines whether the specified points define a vertical line segment within a given tolerance.

A vertical line segment is characterized by having the same x-coordinate for both points. This method checks if the absolute difference between the x-coordinates of p1 and p2 is less than the specified tolerance eps. If eps is not provided, the method defaults to checking for an exact match (eps = 0).

This method is useful in geometric computations where exact precision is challenging due to floating-point arithmetic limitations. By allowing a small tolerance, it accommodates minor computational inaccuracies.

static

Parameters

p1: Point
The first point.
p2: Point
The second point.
eps?: number
The allowable difference in x-coordinates to consider the points as forming a vertical line segment. Defaults to 0. A typical value for a small tolerance is 1e-8.

Return Value

boolean
true if the x-coordinates of p1 and p2 differ by less than eps, indicating that the points define a vertical line segment; otherwise, false .