Initial Commit
commit
a6694cb3f4
|
@ -0,0 +1,50 @@
|
|||
# bresenham3d
|
||||
|
||||
The Bresenham line algorithm is an algorithm which determines which order to form a close approximation to a straight line between two given points. This module is for points in a 3d space.
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
`npm install bresenham3d`
|
||||
|
||||
```
|
||||
var bresenham3d = require('bresenham3d'),
|
||||
p1 = {x:23, y:43, z: -32},
|
||||
p2 = {x: -3, y: 30.323, z: 0.32}
|
||||
density = 1.2,
|
||||
memo = {totalDensity: 0},
|
||||
pointDensity = function(pos, memo) {
|
||||
if (isBlock(pos)) memo.totalDensity += density;
|
||||
},
|
||||
xrayDamage = function(memo) {
|
||||
if (memo.totalDensity < 10) player.takeDamage(100);
|
||||
};
|
||||
|
||||
bresenham3d(p1,p2,memo, pointDensity, xrayDamage);
|
||||
|
||||
```
|
||||
|
||||
|
||||
Reference
|
||||
---------
|
||||
|
||||
- [On Wikipedia](https://en.wikipedia.org/wiki/Bresenham's_line_algorithm)
|
||||
- [Shamlessly Borrowed From Here](http://cobrabytes.squeakyduck.co.uk/forum/index.php?topic=1150.0)
|
||||
|
||||
Use positional audio in your [voxeljs.com](http://voxeljs.com) world.
|
||||
|
||||
demo [http://ryanramage.github.com/voxel-audio/](http://ryanramage.github.com/voxel-audio/)
|
||||
|
||||
**not done: work in progress**
|
||||
|
||||
|
||||
|
||||
## License - MIT
|
||||
|
||||
Copyright (c) 2013 Ryan Ramage
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
arguments
|
||||
---------
|
||||
|
||||
- `from_position` - an object with x,y,z properties, eg {x: 23, y: -21, z: 49}
|
||||
- `to_position` - an object with x,y,z properties, eg {x: -32, y: 40, z: 49}
|
||||
- 'memo' - an object used to store values (see onPoint below). eg {count: 0}
|
||||
- `onPoint` - a function that is called for each integer point found on the path. function(pos, memo) {}
|
||||
- 'onComplete' - [optional] a function that is called when complete. function(memo) {}
|
||||
|
||||
*/
|
||||
module.exports = function(from_position, to_position, memo, onPoint, onComplete) {
|
||||
var temp;
|
||||
|
||||
// safty first kids
|
||||
var x0 = Math.floor(from_position.x);
|
||||
var y0 = Math.floor(from_position.y);
|
||||
var z0 = Math.floor(from_position.z);
|
||||
var x1 = Math.floor(to_position.x);
|
||||
var y1 = Math.floor(to_position.y);
|
||||
var z1 = Math.floor(to_position.z);
|
||||
|
||||
//'steep' xy Line, make longest delta x plane
|
||||
var swap_xy = Math.abs(y1 - y0) > Math.abs(x1 - x0);
|
||||
if (swap_xy) {
|
||||
temp = x0; x0 = y0; y0 = temp; //swap(x0, y0);
|
||||
temp = x1; x1 = y1; y1 = temp; //swap(x1, y1);
|
||||
}
|
||||
//do same for xz
|
||||
var swap_xz = Math.abs(z1 - z0) > Math.abs(x1 - x0);
|
||||
if (swap_xz) {
|
||||
temp = x0; x0 = z0; z0 = temp; //swap(x0, z0);
|
||||
temp = x1; x1 = z1; z1 = temp; //swap(x1, z1);
|
||||
}
|
||||
|
||||
//delta is Length in each plane
|
||||
var delta_x = Math.abs(x1 - x0);
|
||||
var delta_y = Math.abs(y1 - y0);
|
||||
var delta_z = Math.abs(z1 - z0);
|
||||
|
||||
//drift controls when to step in 'shallow' planes
|
||||
//starting value keeps Line centred
|
||||
var drift_xy = (delta_x / 2);
|
||||
var drift_xz = (delta_x / 2);
|
||||
|
||||
//direction of line
|
||||
var step_x = 1;
|
||||
if (x0 > x1) step_x = -1;
|
||||
var step_y = 1;
|
||||
if (y0 > y1) step_y = -1;
|
||||
var step_z = 1;
|
||||
if (z0 > z1) step_z = -1;
|
||||
|
||||
|
||||
//starting point
|
||||
var y = y0;
|
||||
var z = z0;
|
||||
|
||||
var cx, cy, cz;
|
||||
|
||||
//step through longest delta (which we have swapped to x)
|
||||
for (var x = x0; x !== x1; x += step_x) {
|
||||
|
||||
//copy position
|
||||
cx = x; cy = y; cz = z;
|
||||
|
||||
//unswap (in reverse)
|
||||
if (swap_xz) {
|
||||
temp = cx; cx = cz; cz = temp; //swap(cx, cz);
|
||||
}
|
||||
if (swap_xy) {
|
||||
temp = cx; cx = cy; cy = temp; //swap(cx, cy);
|
||||
}
|
||||
//passes through this point
|
||||
if (onPoint) onPoint({x: cx, y: cy, z: cz}, memo);
|
||||
|
||||
|
||||
//update progress in other planes
|
||||
drift_xy = drift_xy - delta_y;
|
||||
drift_xz = drift_xz - delta_z;
|
||||
|
||||
//step in y plane
|
||||
if (drift_xy < 0) {
|
||||
y = y + step_y;
|
||||
drift_xy = drift_xy + delta_x;
|
||||
}
|
||||
|
||||
//same in z
|
||||
if (drift_xz < 0) {
|
||||
z = z + step_z;
|
||||
drift_xz = drift_xz + delta_x;
|
||||
}
|
||||
}
|
||||
if (onComplete) onComplete(memo);
|
||||
|
||||
|
||||
};
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"name" : "bresenham3d",
|
||||
"version" : "0.0.1",
|
||||
"description" : "Calculate all integer points along a relativly smooth line segment in 3d space.",
|
||||
"repository" : {
|
||||
"type" : "git",
|
||||
"url" : "git://github.com/ryanramage/bresenham3d.git"
|
||||
},
|
||||
"homepage" : "https://github.com/ryanramage/bresenham3d",
|
||||
"keywords" : [
|
||||
"math",
|
||||
"3d"
|
||||
],
|
||||
"author" : {
|
||||
"name" : "Ryan Ramage",
|
||||
"email" : "ryan@eckoit.com",
|
||||
"url" : "http://github.com/ryanramage"
|
||||
},
|
||||
"license" : "MIT"
|
||||
}
|
Loading…
Reference in New Issue