# Class: Point2D
The Point2D object represents a point in the two dimensional space. It is also used to represent two dimensional vector objects.
# Hierarchy
Point
↳ Point2D
# Constructors
# constructor
+ new Point2D(): Point2D
Defined in point.ts:30 (opens new window)
Creates an empty Point object.
example
// Create a point at x: 10, y: 5
const point = new Point();
console.log(point.x); // { x: 0, y: 0 }
Returns: Point2D
+ new Point2D(scalar
: number): Point2D
Defined in point.ts:42 (opens new window)
Creates a Point object with a x and y value of scalar
example
const point = new Point(5)
console.log(point) // { x: 5, y: 5 }
Parameters:
Name | Type | Description |
---|---|---|
scalar | number | the scalar value |
Returns: Point2D
+ new Point2D(x
: number, y
: number): Point2D
Defined in point.ts:55 (opens new window)
Creates a Point object with the given x and y coordinates.
example
// Create a point at x: 10, y: 5
const point = new Point(10, 5);
console.log(point.x); // 10
console.log(point.y); // 5
Parameters:
Name | Type | Description |
---|---|---|
x | number | the x coordinate |
y | number | the y coordinate |
Returns: Point2D
+ new Point2D(point
: AnyPoint2D): Point2D
Defined in point.ts:71 (opens new window)
Creates a Point object using the properties in the given object.
example
// Creating a point using an array literal:
var point = new Point([10, 20]);
console.log(point.x); // 10
console.log(point.y); // 20
example
// Creating a point at x: 10, y: 20 using an object literal:
var point = new Point({
x: 10,
y: 20
});
console.log(point.x); // 10
console.log(point.y); // 20
Parameters:
Name | Type | Description |
---|---|---|
point | AnyPoint2D | the object describing the point's properties |
Returns: Point2D
# Properties
# x
• x: number = 0
Defined in point.ts:25 (opens new window)
The x coordinate of the point
# y
• y: number = 0
Defined in point.ts:30 (opens new window)
The y coordinate of the point
# Accessors
# angle
• get angle(): number
Defined in point.ts:152 (opens new window)
The vector's angle in degrees, measured from the x-axis to the vector.
readonly
Returns: number
# angleRadians
• get angleRadians(): number
Defined in point.ts:143 (opens new window)
The vector's angle in radians, measured from the x-axis to the vector.
readonly
Returns: number
# array
• get array(): [number, number]
Defined in point.ts:166 (opens new window)
Returns: [number, number]
# length
• get length(): number
Defined in point.ts:162 (opens new window)
The length of the vector that is represented by this point's coordinates.
Each point can be interpreted as a vector that points from the origin (x = 0
, y = 0
) to the point's location. Setting the length changes the
location but keeps the vector's angle.
Returns: number
• set length(length
: number): void
Defined in point.ts:170 (opens new window)
The length of the vector that is represented by this point's coordinates.
Each point can be interpreted as a vector that points from the origin (x = 0
, y = 0
) to the point's location. Setting the length changes the
location but keeps the vector's angle.
Parameters:
Name | Type |
---|---|
length | number |
Returns: void
# quadrant
• get quadrant(): 1 | 2 | 3 | 4
Defined in point.ts:214 (opens new window)
The quadrant of the {@link #angle} of the point.
Angles between 0 and 90 degrees are in quadrant 1
. Angles between 90 and
180 degrees are in quadrant 2
, angles between 180 and 270 degrees are in
quadrant 3
and angles between 270 and 360 degrees are in quadrant 4
.
example
const point = new Point(1, 1);
console.log(point.quadrant); // 1
readonly
Returns: 1 | 2 | 3 | 4
# Methods
# add
▸ add(point
: AnyPoint2D | number): Point2D‹›
Defined in point.ts:254 (opens new window)
Returns the addition of the supplied point to the point as a new point. The object itself is not modified!
example
const point1 = new Point(5, 10);
const point2 = new Point(10, 20);
const result = point1.add(point2);
console.log(result); // {x: 15, y: 30}
Parameters:
Name | Type | Description |
---|---|---|
point | AnyPoint2D | number | the point to add |
Returns: Point2D‹›
the addition of the two points as a new point
# clone
▸ clone(): Point2D‹›
Defined in point.ts:195 (opens new window)
Returns a copy of the point.
example
var point1 = new Point();
var point2 = point1;
point2.x = 1; // also changes point1.x
var point2 = point1.clone();
point2.x = 1; // doesn't change point1.x
Returns: Point2D‹›
the cloned point
# cross
▸ cross(point
: AnyPoint2D | number): number
Defined in point.ts:324 (opens new window)
Returns the cross product of the point and another point.
Parameters:
Name | Type |
---|---|
point | AnyPoint2D | number |
Returns: number
the cross product of the two points
# divide
▸ divide(point
: AnyPoint2D | number): Point2D‹›
Defined in point.ts:302 (opens new window)
Returns the division of the supplied point to the point as a new point. The object itself is not modified!
example
const firstPoint = new Point(8, 10);
const secondPoint = new Point(2, 5);
const result = firstPoint.divide(secondPoint);
console.log(result); // {x: 4, y: 2}
Parameters:
Name | Type | Description |
---|---|---|
point | AnyPoint2D | number | the point to divide by |
Returns: Point2D‹›
the division of the two points as a new point
# dot
▸ dot(point
: AnyPoint2D | number): number
Defined in point.ts:313 (opens new window)
Returns the dot product of the point and another point.
Parameters:
Name | Type |
---|---|
point | AnyPoint2D | number |
Returns: number
the dot product of the two points
# equals
▸ equals(point
: AnyPoint2D): boolean
Defined in point.ts:367 (opens new window)
Checks whether the coordinates of the point are equal to that of the supplied point.
example
var point = new Point(5, 10);
console.log(point == new Point(5, 10)); // true
console.log(point == new Point(1, 1)); // false
console.log(point != new Point(1, 1)); // true
Parameters:
Name | Type | Description |
---|---|---|
point | AnyPoint2D | point to compare |
Returns: boolean
@true if the points are equal
# getDistance
▸ getDistance(point
: AnyPoint2D): Point2D
Defined in point.ts:232 (opens new window)
Returns the distance between the point and another point.
example
const point = new Point(1, 0)
const distance = point1.getDistance({ x: 2, y: 0})
console.log(distance.length) // 1
console.log(distance) // { x: 1, y: 0 }
Parameters:
Name | Type |
---|---|
point | AnyPoint2D |
Returns: Point2D
point representing the distance
# isClose
▸ isClose(point
: Point2D, tolerance
: number): Boolean
Defined in point.ts:379 (opens new window)
Checks if the point is within a given distance of another point.
Parameters:
Name | Type | Description |
---|---|---|
point | Point2D | the point to check against |
tolerance | number | the maximum distance allowed |
Returns: Boolean
@true if it is within the given distance
# modulo
▸ modulo(point
: AnyPoint2D | number): Point2D‹›
Defined in point.ts:282 (opens new window)
Parameters:
Name | Type |
---|---|
point | AnyPoint2D | number |
Returns: Point2D‹›
# multiply
▸ multiply(point
: AnyPoint2D | number): Point2D‹›
Defined in point.ts:277 (opens new window)
Parameters:
Name | Type |
---|---|
point | AnyPoint2D | number |
Returns: Point2D‹›
# rotate
▸ rotate(angle
: number, center?
: Point2D): Point2D
Defined in point.ts:337 (opens new window)
Rotates the point by the given angle around an optional center point. The object itself is not modified.
Parameters:
Name | Type | Description |
---|---|---|
angle | number | the rotation angle |
center? | Point2D | the center point of the rotation |
Returns: Point2D
the rotated point
# subtract
▸ subtract(point
: AnyPoint2D | number): Point2D‹›
Defined in point.ts:272 (opens new window)
Returns the subtraction of the supplied point to the point as a new point.
example
const point = new Point(10, 20);
const result = point.subtract({ x: 5, y: 10 });
console.log(result); // {x: 5, y: 10}
Parameters:
Name | Type | Description |
---|---|---|
point | AnyPoint2D | number | the number to subtract |
Returns: Point2D‹›
the subtraction of the point and the value as a new point
# toString
▸ toString(): string
Defined in point.ts:176 (opens new window)
Returns: string
# Static
max
▸ max(a
: Point2D, b
: Point2D): Point2D‹›
Defined in point.ts:453 (opens new window)
Returns a new point object with the largest {@link #x} and {@link #y} of the supplied points.
example
const point1 = new Point(10, 100);
var point2 = new Point(200, 5);
var maxPoint = Point.max(point1, point2);
console.log(maxPoint); // {x: 200, y: 100}
example
// Find the maximum of multiple points:
var point1 = new Point(60, 100);
var point2 = new Point(200, 5);
var point3 = new Point(250, 35);
[point1, point2, point3].reduce(Point.max) // {x: 250, y: 100}
Parameters:
Name | Type |
---|---|
a | Point2D |
b | Point2D |
Returns: Point2D‹›
the newly created point object
# Static
min
▸ min(a
: Point2D, b
: Point2D): Point2D‹›
Defined in point.ts:424 (opens new window)
Returns a new point object with the smallest {@link #x} and {@link #y} of the supplied points.
example
const point1 = new Point(10, 100);
const point2 = new Point(200, 5);
const minPoint = Point.min(point1, point2);
console.log(minPoint); // {x: 10, y: 5}
example
// Find the minimum of multiple points:
var point1 = new Point(60, 100);
var point2 = new Point(200, 5);
var point3 = new Point(250, 35);
[point1, point2, point3].reduce(Point.min) // {x: 60, y: 5}
Parameters:
Name | Type |
---|---|
a | Point2D |
b | Point2D |
Returns: Point2D‹›
the newly created point object
# Static
zero
▸ zero(): Point2D
Defined in point.ts:395 (opens new window)
Creates a zero point with both x and y values of zero.
example
const zero = Point.zero
console.log(zero) // { x: 0, y: 0 }
Returns: Point2D
an empty point with x and y of zero