Point
The Point
class represents a Point in 2D space.
Constructor
constructor(x: number, y: number, options: PointOptions = {})
Properties
x
- Type:
number
- The x coordinate.
- Type:
y
- Type:
number
- The y coordinate.
- Type:
options
(optional)- Type:
PointOptions
- Default value:
{}
- The options for the Point
- Type:
Methods
setOptions
setOptions(options: PointOptions): Point
Set the options of the point.
Parameters
options
- Type:
PointOptions
- The point options.
- Type:
Return
Point
The point with the new options.
Example
const point = new Point(10, 10);
point.setOptions({ color: 'red', weight: 2 });
toArray
toArray(): [number, number]
Converts a point to an array.
Return
[number, number]
Array with 2 numbers, the first is the x coordinate of the point and the second is the y coordinate of the point.
Example
const point = new Point(10, 10);
const arr = point.toArray();
add
add(point: Point): Point
Addition with another point.
Return
Point
The result of the addition.
Example
const point1 = new Point(10, 10);
const point2 = new Point(10, 10);
const result = point1.add(point2);
subtract
subtract(point: Point): Point
Subtraction with another point.
Return
Point
The result of the subtraction.
Example
const point1 = new Point(10, 10);
const point2 = new Point(10, 10);
const result = point1.subtract(point2);
multiply
multiply(point: Point): Point
Multiplication with another point.
Return
Point
The result of the multiplication.
Example
const point1 = new Point(10, 10);
const point2 = new Point(10, 10);
const result = point1.multiply(point2);
divide
divide(point: Point): Point
Division with another point.
Return
Point
The result of the division.
Example
const point1 = new Point(10, 10);
const point2 = new Point(10, 10);
const result = point1.divide(point2);
equals
equals(point: Point): boolean
Checks if the point is equal to another point.
Parameters
point
- Type:
Point
- The point to compare with
- Type:
Return
boolean
Whether the points are equal.
Example
const point1 = new Point(10, 10);
const point2 = new Point(10, 10);
const result = point1.equals(point2);
getDistanceWithPoint
getDistanceWithPoint(point: Point): number
Get the distance between two points.
Parameters
point
- Type:
Point
- The point to calculate the distance to.
- Type:
Return
number
The distance between the two points.
Example
const point1 = new Point(10, 10);
const point2 = new Point(20, 20);
const result = point1.getDistanceWithPoint(point2);
isOnLine
isOnLine(line: Line, tolerance: number = 0.1): boolean
Check if the point is on a line.
Parameters
line
- Type:
Line
- The line to check.
- Type:
tolerance
(optional)- Type:
number
- Default value:
0.1
- The tolerance of the check
- Type:
Return
boolean
Whether the point is on the line.
Example
const point = new Point(10, 10);
const line = new Line(new Point(0, 0), new Point(20, 20));
const result = point.isOnLine(line);
isOnRectangle
isOnRectangle(rectangle: Rectangle): boolean
Check if the point is on a rectangle.
Parameters
rectangle
- Type:
Rectangle
- The rectangle to check.
- Type:
Return
boolean
Whether the point is on the rectangle.
Example
const point = new Point(10, 10);
const rectangle = new Rectangle(new Point(0, 0), new Point(20, 20));
const result = point.isOnRectangle(rectangle);
isInRectangle
isInRectangle(rectangle: Rectangle): boolean
Check if the point is in a rectangle.
Parameters
rectangle
- Type:
Rectangle
- The rectangle to check.
- Type:
Return
boolean
Whether the point is in the rectangle.
Example
const point = new Point(10, 10);
const rectangle = new Rectangle(new Point(0, 0), new Point(20, 20));
const result = point.isInRectangle(rectangle);
isOnCircle
isOnCircle(circle: Circle, tolerance: number = 0.1): Point[]
Check if the point is on a circle.
Parameters
circle
- Type:
Circle
- The circle to check.
- Type:
tolerance
- Type:
number
- Default value:
0.1
- The tolerance of the check.
- Type:
Return
boolean
Whether the point is on the circle.
Example
const point = new Point(10, 10);
const circle = new Circle(new Point(0, 0), 10);
const result = point.isOnCircle(circle);
isInCircle
isInCircle(circle: Circle, tolerance: number = 0.1): boolean
Check if the point is in a circle.
Parameters
circle
- Type:
Circle
- The circle to check.
- Type:
tolerance
- Type:
number
- Default value:
0.1
- The tolerance of the check.
- Type:
Return
boolean
Whether the point is in the circle.
Example
const point = new Point(10, 10);
const circle = new Circle(new Point(0, 0), 10);
const result = point.isInCircle(circle);
clone
clone(): Point
Get a copy of point.
Return
Point
The copy.
Example
const point = new Point(10, 10);
const copy = point.clone();
draw
draw(ctx: CanvasRenderingContext2D): void
Draw the Point.
Parameters
ctx
- Type:
CanvasRenderingContext2D
- The canvas context.
- Type:
Return
void
Example
const point = new Point(10, 10);
point.draw(ctx);
Static methods
fromArray
static fromArray(array: [number, number]): Point
Creates a point from an array
Parameters
array
- Type:
[number number]
- Array with 2 numbers, the first is x coordinate of the point and the second is the y coordinate of the point.
- Type:
Return
Point
The point.
Example
const point = Point.fromArray([10, 10]);