d0 v1.0.1
d0
Dimension 0 in n-Dimensional World
Motivation
This package has created to use in fields that you need to use physics rules without any real world limitation and AI fields
Installation
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
Preparation for use
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 = 7Also you can access to the origin point in your application using:
D0.ZERO // => x = 0, y = 0, z = 0, ... [dimension-n = 0]Methods and Properties
D0.prototype.d(n, setTo)
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.5D0.prototype.count
returns count of point's dimensions
console.log(point.count); // => 5D0.prototype.x, D0.prototype.y, D0.prototype.z, D0.prototype.w
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.5Also there are setters for them which you can use like:
point.z = 8;
console.log(point.z); // => 8D0.prototype.each(callback)
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 ~> 7D0.prototype.eachWith(secondPoint, callback)
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, falseD0.prototype.add({D0})
Add two points.
Example:
point.add({D0})
console.log(point.toString()) // => (13, 8, 13, 6.5, 7)D0.prototype.midPoint({D0})
Calculate average of points.
Example:
point.midPoint({D0})
console.log(point.midPoint(point2).toString())
// => (6.5, 4, 6.5, 3.25, 3.5)D0.prototype.subtract({D0})
Subtract two Points
Example:
point.subtract({D0})
console.log(point.toString()) // => (-3, 0, 3, 6.5, 7)D0.prototype.multiply({D0|Number})
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)D0.prototype.divide({D0|Number})
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)D0.prototype.equals({D0})
Compare two points.
Example:
console.log(point.equals(point2)) // => falseD0.prototype.abs()
This method return absolute value of every dimensions.
Example:
point.d(1, -60)
console.log(point.abs().toString()) // => (60, 32, 80, 0, 0)D0.prototype.distanceToOrigin()
Calculate euclidean distance between this point and the origin.
Syntax:
D0.prototype.distanceToOrigin()Example:
console.log(point.distanceToOrigin())
// log => 104.9952379872535D0.prototype.euclideanDistanceTo({D0})
Calculate euclidean distance between two points.
Syntax:
D0.prototype.euclideanDistanceTo({D0})Example:
console.log(point.euclideanDistanceTo(point2))
// log => 94.38749917229505D0.prototype.manhattanDistanceTo({D0})
Calculate manhattan distance between two points.
Syntax:
D0.prototype.manhattanDistanceTo({D0})Example:
console.log(point.manhattanDistanceTo(point)) // log => 153D0.prototype.manhattanDistanceTo({D0})
Calculate square distance between two points.
Syntax:
D0.prototype.squareDistanceTo({D0})Example:
console.log(point.squareDistanceTo(point)) // log => 75D0.prototype.distanceTo({D0})
Returns euclidean distance between two points.
Syntax:
D0.prototype.distanceTo({D0})Example:
console.log(point.distanceTo(point))
// log => 94.38749917229505D0.prototype.clone()
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)) // => trueD0.prototype.toString()
Converts point to string
console.log(point.toString()) // => (60, 32, 80)