Dimension 0 in n-Dimensional World
This package has created to use in fields that you need to use physics rules without any real world limitation and AI fields
You can install this package by the following ways :
- Clone this repository directly from Github then get and use the main js file from
lib/
- Install package using npm :
npm install --save D0
After importing D0 to your code like this:
import D0 from 'd0'
You can create a multi-dimensional point with desired dimensions
let point = new D0(3, 4, 5, 6, 7); // => x = 3, y = 4, z = 5, w = 6, dimension-5 = 7
Also you can access to the origin point in your application using:
D0.ZERO // => x = 0, y = 0, z = 0, ... [dimension-n = 0]
With this method you can either get
or set
the dimension-n value
console.log(point.d(1)); // => 3
// returns the value of dimension-1 (x)
point.d(4, 6.5);
// sets the dimension-4's value to 6.5
returns count of point's dimensions
console.log(point.count); // => 5
D0 has getters
for most useful dimensions:
console.log(point.x); // => 3
console.log(point.y); // => 4
console.log(point.z); // => 5
console.log(point.w); // => 6.5
Also there are setters
for them which you can use like:
point.z = 8;
console.log(point.z); // => 8
Executes a provided function once for each dimension.
Syntax:
D0.prototype.each(function callback(dimensionIndex, dimensionValue) {
// your iterator
});
Example:
point.each(function(index, value){
console.log(`#${index} ~> ${value}`);
})
// logs
// #1 ~> 3
// #2 ~> 4
// #3 ~> 8
// #4 ~> 6.5
// #5 ~> 7
Executes a provided function one by one for each dimension of two points.
Syntax:
D0.prototype.eachWith(secondPoint, function callback(dimensionIndex, dimensionValueOfPoint1, dimensionValueOfPoint2) {
// your iterator
});
Example:
let point2 = new D0(10, 4, 5)
point.eachWith(point2, function(index, valueOfPoint1, valueOfPoint2){
console.log(`${valueOfPoint1}, ${valueOfPoint2}, ${valueOfPoint1 === valueOfPoint2}`);
})
// logs
// 3, 10, false
// 4, 4, true
// 8, 5, false
// 6.5, 0, false
// 7, 0, false
Add two points.
Example:
point.add({D0})
console.log(point.toString()) // => (13, 8, 13, 6.5, 7)
Calculate average of points.
Example:
point.midPoint({D0})
console.log(point.midPoint(point2).toString())
// => (6.5, 4, 6.5, 3.25, 3.5)
Subtract two Points
Example:
point.subtract({D0})
console.log(point.toString()) // => (-3, 0, 3, 6.5, 7)
Mutiply two Points or a point to a number.
Syntax:
D0.prototype.mutiply({D0|Number})
Example with {D0}:
console.log(point.toSting()) // => (-3, 0, 3, 6.5, 7)
console.log(point2.toSting()) // => (10, 4, 5, 0, 0)
point.mutiply(point2)
console.log(point.toString()) // => (-30, 0, 15, 0, 0)
Example with {Number}:
console.log(point.toString())
// => (-3, 0, 3, 6.5, 7)
point.mutiply(2)
console.log(point.toString())
// => (-6, 0, 6, 13, 14)
Divide the point by anthor point or a number
Syntax:
D0.prototype.divide({D0|Number})
Example with {D0}:
console.log(point.toSting()) // => (-30, 0, 15, 0, 0)
console.log(point2.toSting()) // => (10, 4, 5, 0, 0)
point.divide(point2)
console.log(point.toString()) // => (-3, 0, 3, 6.5, 7)
Example with {Number}:
console.log(point.toString())
// => (-6, 0, 6, 13, 14)
point.divide(3)
console.log(point.toString())
// => (-2, 0, 2, 4.3, 4.6)
Compare two points.
Example:
console.log(point.equals(point2)) // => false
This method return absolute value of every dimensions.
Example:
point.d(1, -60)
console.log(point.abs().toString()) // => (60, 32, 80, 0, 0)
Calculate euclidean distance between this point and the origin.
Syntax:
D0.prototype.distanceToOrigin()
Example:
console.log(point.distanceToOrigin())
// log => 104.9952379872535
Calculate euclidean distance between two points.
Syntax:
D0.prototype.euclideanDistanceTo({D0})
Example:
console.log(point.euclideanDistanceTo(point2))
// log => 94.38749917229505
Calculate manhattan distance between two points.
Syntax:
D0.prototype.manhattanDistanceTo({D0})
Example:
console.log(point.manhattanDistanceTo(point)) // log => 153
Calculate square distance between two points.
Syntax:
D0.prototype.squareDistanceTo({D0})
Example:
console.log(point.squareDistanceTo(point)) // log => 75
Returns euclidean distance between two points.
Syntax:
D0.prototype.distanceTo({D0})
Example:
console.log(point.distanceTo(point))
// log => 94.38749917229505
Simply create a copy of the point.
console.log(point.toString()) // => (60, 32, 80)
let pointCopy = point.clone()
console.log(pointCopy.toString()) // => (60, 32, 80)
console.log(pointCopy === point) // => false
console.log(pointCopy.equals(point)) // => true
Converts point to string
console.log(point.toString()) // => (60, 32, 80)