Methods
(static) calcAngleBetweenRectangles(rect1, rect2) → {number}
Calculates the angle between the centers of two rectangles, given as instances of Rectangle
, in radians.
Name | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
rect1 | Object | The first rectangle, represented as an instance of Properties
| |||||||||||||||
rect2 | Object | The second rectangle, represented as an instance of Properties
|
The angle between the centers of the two rectangles, in radians.
- Type:
- number
const rect1 = { x: 0, y: 0, width: 2, height: 2 };
const rect2 = { x: 2, y: 2, width: 2, height: 2 };
calcAngleBetweenRectangles(rect1, rect2); // output: 0.7853981633974483
const rect1 = { x: 0, y: 0, width: 2, height: 2 };
const rect2 = { x: 1, y: 2, width: 2, height: 2 };
calcAngleBetweenRectangles(rect1, rect2); // output: 1.1071487177940904
(static) calcAngleBetweenRectanglesByCoordinates(x1, y1, width1, height1, x2, y2, width2, height2) → {number}
Gets the angle between two rectangles defined by their coordinates.
Name | Type | Description |
---|---|---|
x1 | number | The x-coordinate of the first rectangle. |
y1 | number | The y-coordinate of the first rectangle. |
width1 | number | The width of the first rectangle. |
height1 | number | The height of the first rectangle. |
x2 | number | The x-coordinate of the second rectangle. |
y2 | number | The y-coordinate of the second rectangle. |
width2 | number | The width of the second rectangle. |
height2 | number | The height of the second rectangle. |
The angle between the two rectangles.
- Type:
- number
calcAngleBetweenRectanglesByCoordinates(0, 0, 10, 10, 20, 20, 20, 20); // Output: 45
calcAngleBetweenRectanglesByCoordinates(10, 10, 10, 10, 30, 30, 20, 20); // Output: 30
(static) calcAngleBetweenTwoPointsDegrees(x1, y1, x2, y2) → {number}
Calculates the angle between two points in a two-dimensional plane, in degrees.
Name | Type | Description |
---|---|---|
x1 | number | The x-coordinate of the first point. |
y1 | number | The y-coordinate of the first point. |
x2 | number | The x-coordinate of the second point. |
y2 | number | The y-coordinate of the second point. |
The angle between the two points, in degrees.
- Type:
- number
calcAngleBetweenTwoPointsDegrees(0, 0, 1, 1); // output: 45
calcAngleBetweenTwoPointsDegrees(0, 0, 0, 1); // output: 90
(static) calcAngleBetweenTwoPointsVector2(point1, point2) → {number}
Calculates the angle between two points in a two-dimensional plane, given as instances of Vector2
.
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
point1 | Object | The first point, represented as an instance of Properties
| |||||||||
point2 | Object | The second point, represented as an instance of Properties
|
The angle between the two points, in radians.
- Type:
- number
const point1 = { x: 0, y: 0 };
const point2 = { x: 1, y: 1 };
calcAngleBetweenTwoPointsVector2(point1, point2); // output: 0.7853981633974483 (approx. 45°)
const point1 = { x: 0, y: 0 };
const point2 = { x: 0, y: 1 };
calcAngleBetweenTwoPointsVector2(point1, point2); // output: 1.5707963267948966 (approx. 90°)
(static) calcAngleBetweenTwoPointsVector2Degrees(point1, point2) → {number}
Calculates the angle between two points in a two-dimensional plane, given as instances of Vector2
, in degrees.
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
point1 | Object | The first point, represented as an instance of Properties
| |||||||||
point2 | Object | The second point, represented as an instance of Properties
|
The angle between the two points, in degrees.
- Type:
- number
const point1 = { x: 0, y: 0 };
const point2 = { x: 1, y: 1 };
calcAngleBetweenTwoPointsVector2Degrees(point1, point2); // output: 45
const point1 = { x: 0, y: 0 };
const point2 = { x: 0, y: 1 };
calcAngleBetweenTwoPointsVector2Degrees(point1, point2); // output: 90
(static) calcCircleArea(radius) → {number}
Gets the area of a circle.
Name | Type | Description |
---|---|---|
radius | number | The radius of the circle. |
The area of the circle.
- Type:
- number
calcCircleArea(5); // Output: 78.53981633974483
calcCircleArea(10); // Output: 314.1592653589793
(static) calcCirclePerimeter(radius) → {number}
Gets the perimeter of a circle.
Name | Type | Description |
---|---|---|
radius | number | The radius of the circle. |
The perimeter of the circle.
- Type:
- number
calcCirclePerimeter(5); // Output: 31.41592653589793
calcCirclePerimeter(10); // Output: 62.83185307179586
(static) calcRectangleArea(width, height) → {number}
Gets the area of a rectangle.
Name | Type | Description |
---|---|---|
width | number | The width of the rectangle. |
height | number | The height of the rectangle. |
The area of the rectangle.
- Type:
- number
calcRectangleArea(5, 10); // Output: 50
calcRectangleArea(10, 20);
(static) calcRectangleCenter(x, y, width, height) → {object}
Calculates the center coordinates of a rectangle given its x-coordinate, y-coordinate, width, and height.
Name | Type | Description |
---|---|---|
x | number | The x-coordinate of the rectangle |
y | number | The y-coordinate of the rectangle |
width | number | The width of the rectangle |
height | number | The height of the rectangle |
An object with x and y properties representing the center coordinates of the rectangle
- Type:
- object
calcRectangleCenter(10, 10, 20, 20); // returns { x: 15, y: 15 }
(static) calcRectangleCenterFromBounds(rectangle) → {object}
Returns the center of a rectangle, given the rectangle's bounds.
Name | Type | Description |
---|---|---|
rectangle | object | The rectangle's bounds, represented as an object with |
The center of the rectangle, represented as an object with x
and y
properties.
- Type:
- object
const rect = { x: 10, y: 10, width: 20, height: 20 };
calcRectangleCenterFromBounds(rect); // { x: 20, y: 20 }
(static) calcRectangleCenterX(x, width) → {number}
Calculates the center x-coordinate of a rectangle given its x-coordinate and width.
Name | Type | Description |
---|---|---|
x | number | The x-coordinate of the rectangle |
width | number | The width of the rectangle |
The center x-coordinate of the rectangle
- Type:
- number
calcRectangleCenterX(10, 20); // returns 15
(static) calcRectangleCenterXFromBounds(rectangle) → {number}
Calculates the center x-coordinate of a rectangle given its bounds.
Name | Type | Description |
---|---|---|
rectangle | object | An object with x, y, width, and height properties representing the bounds of the rectangle |
The center x-coordinate of the rectangle
- Type:
- number
calcRectangleCenterXFromBounds({ x: 10, y: 10, width: 20, height: 20 }); // returns 15
(static) calcRectangleCenterY(y, height) → {number}
Calculates the center y-coordinate of a rectangle given its y-coordinate and height.
Name | Type | Description |
---|---|---|
y | number | The y-coordinate of the rectangle |
height | number | The height of the rectangle |
The center y-coordinate of the rectangle
- Type:
- number
calcRectangleCenterY(10, 20); // returns 15
(static) calcRectangleCenterYFromBounds(rectangle) → {number}
Returns the Y-coordinate of the center of a rectangle, given the rectangle's bounds.
Name | Type | Description |
---|---|---|
rectangle | object | The rectangle's bounds, represented as an object with |
The Y-coordinate of the center of the rectangle.
- Type:
- number
calcRectangleCenterYFromBounds({ x: 10, y: 10, y: 10, height: 20 }); // 20
calcRectangleCenterYFromBounds({ x: 10, y: 10, width: 20, height: 20 }); // returns 15
(static) calcRectanglePerimeter(rectangle) → {number}
Gets the perimeter of a rectangle defined by its bounds.
Name | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
rectangle | Object | The rectangle bounds object. Properties
|
The perimeter of the rectangle.
- Type:
- number
const rectangle = { x: 0, y: 0, width: 5, height: 10 };
calcRectanglePerimeter(rectangle); // Output: 30
const rectangle = { x: 10, y: 20, width: 10, height: 20 };
calcRectanglePerimeter(rectangle); // Output: 60
(static) calcRectanglePerimeterByDimensions(width, height) → {number}
Gets the perimeter of a rectangle.
Name | Type | Description |
---|---|---|
width | number | The width of the rectangle. |
height | number | The height of the rectangle. |
The perimeter of the rectangle.
- Type:
- number
calcRectanglePerimeterByDimensions(5, 10); // Output: 30
calcRectanglePerimeterByDimensions(10, 20); // Output: 60
(static) calcRectangleVertices(x, y, width, height) → {Array}
Returns an array of objects that represent the vertices of a rectangle.
Name | Type | Description |
---|---|---|
x | number | The x-coordinate of the top-left corner of the rectangle. |
y | number | The y-coordinate of the top-left corner of the rectangle. |
width | number | The width of the rectangle. |
height | number | The height of the rectangle. |
An array of objects that represent the vertices of the rectangle, where each object has properties 'x' and 'y'.
- Type:
- Array
calcRectangleVertices(0, 0, 2, 3); // [{x: 0, y: 0}, {x: 2, y: 0}, {x: 2, y: 3}, {x: 0, y: 3}]
(static) calcVerticesFromRectangleBounds(rectangle) → {Array}
Returns an array of objects that represent the vertices of a rectangle, given its bounds.
Name | Type | Description |
---|---|---|
rectangle | Object | An object that represents the bounds of the rectangle, with properties 'x', 'y', 'width', and 'height'. |
An array of objects that represent the vertices of the rectangle, where each object has properties 'x' and 'y'.
- Type:
- Array
calcVerticesFromRectangleBounds({ x: 0, y: 0, width: 2, height: 3 }); // [{x: 0, y: 0}, {x: 2, y: 0}, {x: 2, y: 3}, {x: 0, y: 3}]
(static) calculateDistanceBetweenCircles(circle1, circle2) → {number}
Gets the distance between two circles defined by their centers.
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
circle1 | Object | The first circle object. Properties
| ||||||||||||
circle2 | Object | The second circle object. Properties
|
The distance between the two circles.
- Type:
- number
const circle1 = { x: 0, y: 0, radius: 5 };
const circle2 = { x: 10, y: 0, radius: 5 };
calculateDistanceBetweenCircles(circle1, circle2); // Output: 0
const circle1 = { x: 0, y: 0, radius: 10 };
const circle2 = { x: 20, y: 0, radius: 5 };
calculateDistanceBetweenCircles(circle1, circle2); // Output: 5
(static) calculateDistanceBetweenCirclesByCoordinates(x1, y1, radius1, x2, y2, radius2) → {number}
Gets the distance between two circles defined by their centers and radii.
Name | Type | Description |
---|---|---|
x1 | number | The x-coordinate of the center of the first circle. |
y1 | number | The y-coordinate of the center of the first circle. |
radius1 | number | The radius of the first circle. |
x2 | number | The x-coordinate of the center of the second circle. |
y2 | number | The y-coordinate of the center of the second circle. |
radius2 | number | The radius of the second circle. |
The distance between the two circles.
- Type:
- number
calculateDistanceBetweenCirclesByCoordinates(0, 0, 5, 10, 0, 5); // Output: 0
calculateDistanceBetweenCirclesByCoordinates(0, 0, 10, 20, 0, 5); // Output: 5
(static) calculateOverlapBetweenRectangles(rect1, rect2) → {number}
Calculates the area of overlap between two rectangles defined by their bounds.
Name | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
rect1 | Object | The first rectangle. Properties
| |||||||||||||||
rect2 | Object | The second rectangle. Properties
|
The area of overlap between the two rectangles.
- Type:
- number
const rect1 = { x: 0, y: 0, width: 10, height: 10 };
const rect2 = { x: 5, y: 5, width: 10, height: 10 };
calculateOverlapBetweenRectangles(rect1, rect2); // Returns 25
(static) calculateOverlapBetweenRectanglesByCoordinates(x1, y1, width1, height1, x2, y2, width2, height2) → {number}
Calculates the area of overlap between two rectangles defined by their x, y, width, and height.
Name | Type | Description |
---|---|---|
x1 | number | The x-coordinate of the top-left corner of the first rectangle. |
y1 | number | The y-coordinate of the top-left corner of the first rectangle. |
width1 | number | The width of the first rectangle. |
height1 | number | The height of the first rectangle. |
x2 | number | The x-coordinate of the top-left corner of the second rectangle. |
y2 | number | The y-coordinate of the top-left corner of the second rectangle. |
width2 | number | The width of the second rectangle. |
height2 | number | The height of the second rectangle. |
The area of overlap between the two rectangles.
- Type:
- number
calculateOverlapBetweenRectanglesByCoordinates(0, 0, 10, 10, 5, 5, 10, 10); // Returns 25
(static) getAngleBetweenTwoPoints(x1, y1, x2, y2) → {number}
Calculates the angle between two points in a two-dimensional plane.
Name | Type | Description |
---|---|---|
x1 | number | The x-coordinate of the first point. |
y1 | number | The y-coordinate of the first point. |
x2 | number | The x-coordinate of the second point. |
y2 | number | The y-coordinate of the second point. |
- Source
The angle between the two points, in radians.
- Type:
- number
calcAngleBetweenTwoPoints(0, 0, 1, 1); // output: 0.7853981633974483 (approx. 45°)
calcAngleBetweenTwoPoints(0, 0, 0, 1); // output: 1.5707963267948966 (approx. 90°)
(static) topDownCarMovimentation(state) → {Object}
Updates the position, rotation, and speed of a car in a top-down view.
The algorithm is based on https://www.youtube.com/watch?v=Rs_rAxEsAvI.
Name | Type | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
state | Object | the current state of the car Properties
|
- Type:
- Object