2d-utils v1.2.9
Simplify point manipulation and interactions in your 2D projects with this versatile toolkit.Effortlessly calculate distances, areas, collision detection, and more with this collection of handy functions.
How to use
- Installation
npm install 2d-utils
- Usage
import 2d from "2d-utils";
Functions
This section explains the usage of the utility functions.
Actions with points in XY coordinate system
distance(x1, y1, x2, y2)
Returns the distance beetween two points.
area(points)
Returns the area enclosed by the given points. Takes an array of points as argument, where each point is an object with x and y properties.
collision(x1, y1, x2, y2,collisionDistance, callback)
Returns a boolean indicating if the two points are closer than the given distance.
collisionInArray(x1, y1, radius, points, callback)
Returns the point that is closer than the radius to the given point, or false if there's no collision.
positionInCircle(centerX, centerY, radius, angleInRadians)
Returns the XY coordinates for the current point in the circle, given its center, radius, and angle.
angle(x1, y1, x2, y2)
Returns the angle formed by the connection of two points.
middlePoint(x1, y1, x2, y2)
Returns the midpoint between two points.
nearest(x, y, points)
Returns the nearest point to the given point from the array.
perimeter(points)
Returns the perimeter of the figure formed by the given points.
pointWithoutCollision(minX, maxX, minY, maxY, distance, points)
Returns a point that doesn't collide with any of the given points within the specified distance, if such a point exists, otherwise returns false.
randomPoint(xMin, xMax, yMin, yMax)
Returns a random point within the given dimensions, if provided.
randomPointInDistance(x, y, distance)
Returns a random point within the given distance from the specified point.
randomPoints(xMin, xMax, yMin, yMax, quantity)
Returns a specified quantity of random points within the given dimensions, if dimensions are provided.
Math
degreesToRadians(degrees)
Converts degrees to radians.
radiansToDegrees(radians)
Converts radians to degrees.
inRange(number, min, max)
Returns true if the given number is within the specified range.
roundToPrecision(number, precision)
Rounds the number to the given precision.
Randomization
randomNumber(min, max)
Returns a random number within the given range.
randomBoolean()
Returns a random boolean value.
uniqueId(other ids)
Returns a unique ID that's different from the provided IDs, or a random ID if no other IDs are given.