Line
The Line
class represents a Line in 2D space.
Constructor
constructor(start: Point, end: Point, options: LineOptions = {})
Properties
start
- Type:
Point
- The start point of the line.
- Type:
end
- Type:
Point
- The end point of the line.
- Type:
options
(optional)- Type:
LineOptions
- Default value:
{}
- The options for the line
- Type:
Getters
length
- Type:
number
The length of the line.
center
- Type:
Point
The center of the line.
angle
- Type:
number
The angle of the line.
Methods
setOptions
setOptions(options: LineOptions): Circle
Set the options of the line.
Parameters
options
- Type:
LineOptions
- The circle options.
- Type:
Return
Line
The line with the new options.
Example
const line = new Line(/* ... */);
line.setOptions({ color: 'red', weight: 2 });
toArray
toArray(): [number, number, number, number]
Converts a line to an array.
Return
[number, number, number, number]
Array with 4 numbers, the two first are the start position of the line and the two remaining are the end position of the line.
Example
const line = new Line(new Point(10, 10), new Point(20, 20));
const array = line.toArray();
equals
equals(line: Line): boolean
Checks if the line is equal to another line.
Parameters
line
- Type:
Line
- The line to compare with
- Type:
Return
boolean
Whether the lines are equal.
Example
const line1 = new Line(new Point(10, 10), new Point(20, 20));
const line2 = new Line(new Point(10, 10), new Point(20, 20));
const line3 = new Line(new Point(10, 10), new Point(20, 30));
line1.equals(line2); // true
line1.equals(line3); // false
line2.equals(line3); // false
isContainsPoint
isContainsPoint(point: Point, tolerance: number = 0.1): boolean
Checks if the line contains a point.
Parameters
point
- Type:
Point
- The point to check
- Type:
tolerance
(optional)- Type:
number
- Default value:
0.1
- The tolerance to use
- Type:
Return
boolean
Whether the line contains the point.
Example
const line = new Line(new Point(10, 10), new Point(20, 20));
line.isContainsPoint(new Point(15, 15)); // true
isIntersectsWithLine
isIntersectsWithLine(line: Line, tolerance: number = 0.1): boolean
Checks if the line intersects with another line.
Parameters
line
- Type:
Line
- The line to check.
- Type:
tolerance
(optional)- Type:
number
- Default value:
0.1
- The tolerance to use
- Type:
Return
boolean
Whether the lines intersect.
Example
const line1 = new Line(new Point(10, 10), new Point(20, 20));
const line2 = new Line(new Point(10, 20), new Point(20, 10));
line1.isIntersectsWithLine(line2); // true
getIntersectionsWithLine
getIntersectionsWithLine(line: Line, tolerance: number = 0.1): Point[]
Get the intersections with another line
Parameters
line
- Type:
Line
- The line to check.
- Type:
tolerance
(optional)- Type:
number
- Default value:
0.1
- The tolerance to use.
- Type:
Return
Point[]
The intersections.
Example
const line1 = new Line(new Point(10, 10), new Point(20, 20));
const line2 = new Line(new Point(10, 20), new Point(20, 10));
const intersections = line1.getIntersectionsWithLine(line2);
isIntersectsWithRectangle
isIntersectsWithRectangle(rectangle: Rectangle): boolean
Check if the line intersects with a rectangle.
Parameters
rectangle
- Type:
Rectangle
- The rectangle to check.
- Type:
Return
boolean
Whether the line intersects with the rectangle.
Example
const line = new Line(new Point(10, 10), new Point(20, 20));
const rectangle = new Rectangle(new Point(10, 20), new Point(20, 10));
line.isIntersectsWithRectangle(rectangle); // true
getIntersectionsWithRectangle
getIntersectionsWithRectangle(rectangle: Rectangle): Point[]
Get the intersection point of the line with a rectangle.
Parameters
rectangle
- Type:
Rectangle
- The rectangle to check.
- Type:
Return
Point[]
The intersection points.
Example
const line = new Line(new Point(10, 10), new Point(20, 20));
const rectangle = new Rectangle(new Point(10, 20), new Point(20, 10));
const points = line.getIntersectionsWithRectangle(rectangle);
isInRectangle
isInRectangle(rectangle: Rectangle): boolean
Check if the line is in a rectangle.
Parameters
rectangle
- Type:
Rectangle
- The rectangle to check.
- Type:
Return
boolean
Whether the line is in the rectangle.
Example
const line = new Line(new Point(10, 10), new Point(20, 20));
const rectangle = new Rectangle(new Point(10, 20), new Point(20, 10));
line.isInRectangle(rectangle); // true
isIntersectsWithCircle
isIntersectsWithCircle(circle: Circle, tolerance: number = 0.1): boolean
Check if the line intersects with a circle.
Parameters
circle
- Type:
Circle
- The circle to check.
- Type:
tolerance
- Type:
number
- Default value:
0.1
- The tolerance to use.
- Type:
Return
boolean
Whether the line intersects with the circle.
Example
const line = new Line(new Point(10, 10), new Point(20, 20));
const circle = new Circle(new Point(15, 15), 5);
line.isIntersectsWithCircle(circle); // true
getIntersectionsWithCircle
getIntersectionsWithCircle(circle: Circle, tolerance: number = 0.1): Point | null
Get the intersection point of the line with a circle.
Parameters
circle
- Type:
Circle
- The circle to check.
- Type:
tolerance
- Type:
number
- Default value:
0.1
- The tolerance to use.
- Type:
Return
Point | null
The intersection point or null if the line doesn't intersect with the circle.
Example
const line = new Line(new Point(10, 10), new Point(20, 20));
const circle = new Circle(new Point(15, 15), 5);
const point = line.getPointFromIntersectsWithCircle(circle);
isInCircle
isInCircle(circle: Circle): boolean
Check if the line is in a circle
Parameters
circle
- Type:
Circle
- The circle to check.
- Type:
Return
Point | null
Whether the line is in the circle.
Example
const line = new Line(new Point(10, 10), new Point(20, 20));
const circle = new Circle(new Point(15, 15), 5);
line.isInCircle(circle); // true
clone
clone(): Line
Get a copy of line.
Return
Line
The copy.
Example
const line = new Line(new Point(10, 10), new Point(20, 20));
const line2 = line.clone();
draw
draw(ctx: CanvasRenderingContext2D): void
Draw the Line.
Parameters
ctx
- Type:
CanvasRenderingContext2D
- The canvas context.
- Type:
Return
void
Example
const line = new Line(new Point(10, 10), new Point(20, 20));
line.draw(ctx);
Static methods
fromArray
static fromArray(array: [number, number, number, number]): Line
Creates a line from an array
Parameters
array
- Type:
[number, number, number number]
- Array with 4 numbers, the two first are the start position of the line and the two remaining are the end position of the line.
- Type:
Return
Line
The line.
Example
const line = Line.fromArray([10, 10, 20, 20]);