Package Exports
- pointscape
Readme
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 pointscape- Usage
import pointscape from "pointscape";Examples
import {
distance,
triangle,
center,
inRange,
chunk,
randomBoolean } from "pointscape";
const distanceBetweenPoints = distance({x: 0, y: 0}, {x: 10, y: 10});
console.log(distanceBetweenPoints);
// output: 14.142135623730951
const pointsForTriangle = triangle({x: 0, y: 0}, 10);
console.log(pointsForTriangle);
// output: [ { x: 0, y: 0 }, { x: -10, y: 0 }, { x: -5, y: 8.660254037844386 } ]
const centralPoint = center(
[{x:0, y: 0}, {x:0, y:10}, {x:10, y:10}, {x:10, y:0}]
);
console.log(centralPoint);
// output: { x: 5, y: 5 }
const isInTheRange = inRange(1, 0, 10);
console.log(isInTheRange);
// output: true
const chunks = chunk([1, 1, 1, 1], 2);
console.log(chunks);
// output: [[1, 1], [1, 1]]
const randomBool = randomBoolean();
console.log(randomBool)
// output: true or false
Functions
Actions with points in XY coordinate system
Math
Arrays
Randomization
Actions with points in XY coordinate system
- distance
(point1: Point, point2: Point) => numberReturns the distance beetween two points, each point is an object with x and y properties.
- area
(points: Point[]) => numberReturns 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
(point1: Point, point2: Point, collisionDistance: number, [callback]: Function) => booleanReturns a boolean indicating if the two points are closer than the given distance.
- collisionInArray
(point: Point, radius: number, points: Point[], [callback]: Function) => Point[]Returns the points that are closer than the radius to the given point.
- positionInCircle
(point: Point, radius: number, angleInRadians: number) => PointReturns the x and y coordinates for the current point in the circle, given its center point, radius, and angle.
- angle
(point1: Point, point2: Point) => numberReturns the angle formed by the connection of two points.
- middle
(point1: Point, point2: Point) => PointReturns the midpoint between two points.
- nearest
(point: Point, points: Point[]) => PointReturns the nearest point to the given point from the array.
- perimeter
(points: Point[]) => numberReturns the perimeter of the figure formed by the given points.
- pointWithoutCollision(minX, maxX, minY, maxY, distance, points)
(minX: number, maxX: number, minY: number, maxY: number, distance: number, points: Point[]) => Point | falseReturns 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]: number, [xMax]: number, [yMin]: number, [yMax]: number) => PointReturns a random point within the given dimensions, if provided, otherwise in 100 units on both axes.
- randomPointInDistance
(point: Point, distance: number) => PointReturns a random point within the given distance from the specified point.
- randomPoints
(quantity: number, [xMin]: number, [xMax]: number, [yMin]: number, [yMax]: number) => Point[]Returns a specified quantity of random points within the given dimensions, if dimensions are provided, otherwise in the range of 100.
- possibleConnections
(pointsCount: number) => numberReturns the quantity of possible connections among given quantity of points.
- circleArea
(radius): number => numberReturns the area of the circle.
- center
(points: Point[]) => PointReturns the center of given points.
- farest
(point: Point, points: Point[]) => PointReturns the farest point to the given point from the array.
- rotate
(point: Point, points: Point[], angleInRadians: number) => Point[]Returns the points rotated around the given point.
- sort
(points: Point, [coordinate]: "x" | "y") => Point[]Returns sorted array of the points.The coordinate parameter can be "x", "y", or none for sorting both for "x" and "y".
- scale
(scaleFactorX: number, scaleFactorY: number, points: Point[]) => Point[]Returns the scaled points.
- inLine
([point1, point2, point3]: Point[]) => booleanReturns boolean value indicating whether or not the given coordinates are on line defined by two other points.
- cross
(line1Start: Point, line1End: Point, line2Start: Point, line2End: Point) => booleanReturns boolean value indicating if two lines each defined by two points intersect.
- move
(point: Point, xStep: number, yStep: number, count: number) => Point[]Returns an array of points representing a moving point over time. The number of elements in the array is equal to "count". Each element contains coordinates of the point.
- square
(point: Point, size: number, [direction]: "left" | "right" | "down" | "up" ) => Point[]Returns an array of points representing a shape of square.Takes four parameters: starting coordinates (x and y), size of square side, and direction which should be one of the values "left", "right", "up", "down".
- rectangle(point, size, [direction])
(point: Point, size: number, [direction]: "left" | "right" | "down" | "up" ) => Point[]Returns an array of points representing a shape of rectangle.Takes same parameters as square function.
- triangle(point, size, [direction])
(point: Point, size: number, [direction]: "left" | "right" | "down" | "up" ) => Point[]Returns an array of points representing a shape of triangle.Takes same parameters as square function.
- pentagon(point, size, [direction])
(point: Point, size: number, [direction]: "left" | "right" | "down" | "up" ) => Point[]Returns an array of points representing a shape of pentagon.Takes four parameters: starting coordinates (x and y), size of pentagon side, and the angle of pentagon's rotation.
Math
- degreesToRadians
(degrees: number) => numberConverts degrees to radians.
- radiansToDegrees
(radians: number) => numberConverts radians to degrees.
- inRange
(number: number, min: number, max: number) => booleanReturns true if the given number is within the specified range.
- roundToPrecision)
(number: number, precision: -100 | -10 | 0 | 10 | 100 | number) => numberRounds the number to the given precision.
- average
(numbers: number[]) => numberReturns the average of all numbers in an array.
Arrays
- intersection(
(arr1: any[], arr2: any[]) => any[]Returns the array of intersection of two arrays.
- difference
(arr1: any[], arr2: any[]) => any[]Returns the array of difference of two arrays.
- chunk
(arr: any[], perArr: number) => any[][]Returns an array splited into chunks based on elements count per chunk.
- removeDuplicates(arr)
(arr: any[]) => any[]Returns the array without duplicates.
- sample
(arr: any[], [size]: number[]) => any[]Returns a random sample from an array with optional size argument for sampling length. If not specified, it returns only one element.
Randomization
- randomNumber
(min: number, max: number) => numberReturns a random number within the given range.
- randomBoolean
() => booleanReturns a random boolean value.
- uniqueId
([other ids]: string[]) => string Returns a unique ID that's different from the provided IDs, or a random ID if no other IDs are given.