A example of a issue I am having with precision is when generating 3d vectors.
The code below is supposed to generate coordinate points on a line between two 3d coordinates.
function curveType(vertices, type) {
let p1 = vertices[0]
let p2 = vertices[1]
let xDiff = matrixTools.differenceBetween(p1[0], p2[0]);
let yDiff = matrixTools.differenceBetween(p1[1], p2[1]);
let zDiff = matrixTools.differenceBetween(p1[2], p2[2]);
let curveCoords = []
let distance = matrixTools.distanceCalc(vertices[0], vertices[1])
console.log(`Generating coordinates between: ${JSON.stringify(p1)} and ${JSON.stringify(p2)}`)
let density = 1/distance;
for (let t = 0; t <= 1; t += density) {
let x = vertices[0][0] + xDiff * t
let y = vertices[0][1] + yDiff * t
let z = vertices[0][2] + zDiff * t
console.log(`t: ${t} \n Den: ${density}`)
if (t+density > 1) {
console.log(`t: ${t} \n Den: ${density} \n t+den: ${t+density} \n distance: ${distance}`)
}
console.log(`Generated: ${[x, y, z]}`)
curveCoords.push([x, y, z])
}
// if (!programTools.compare2d(curveCoords[curveCoords.length - 1], p2)) {
// curveCoords[curveCoords.length - 1] = [...p2]
// }
console.log(`returning: ${JSON.stringify(curveCoords)}`)
return curveCoords
}
Here are some of the helper functions that go along with it:
These two functions find the difference (rate of change), positive or negative between the points to use in the 3d vectors. The distance functions finds the direct distance between two points
let matrixTools = {
differenceBetween: function(n1, n2) {
if (n1 >= 0 && n2 >= 0) {
return n2 - n1
} else if (n1 < 0 && n2 >= 0) {
return (n2 + Math.abs(n1))
} else if (n1 >= 0 && n2 < 0) {
return -(n1 + Math.abs(n2))
} else {
return n1 < n2 ? Math.abs(n1) + n2 : (Math.abs(n1) - Math.abs(n2))
}
},
distanceCalc: function(p1, p2) {
return Math.sqrt(Math.pow((p2[0] - p1[0]), 2) + Math.pow((p2[1] - p1[1]), 2) + Math.pow((p2[2] - p1[2]), 2))
}
}
There is a infinite amount of coordinates on a line between any two points. I want to generate enough coordinates that, when each coordinate’s values are rounded to a whole number, all whole numbers coordinates between the two points were found. This is so I could then use these coordinates as index’s on a 3d matrix to capture my 3d object (can’t have matrix3d[1.5][2.5][3]
).
To generate the coordinates, I find the distance between the two points. I then divide one by that distance. I then count up with that decimal number from 0 to 1. This should make it so no matter the size of the line, I always generate enough coordinates to span that line.
If you use the program, you will notice that rounding errors seem to always cause a snowball affect. A rounding error on the second coordinate calculation will cause the next one to be off, and the next one’s rounding error adds to that.
Here is a example of this happening (modeled in Three.js using 0.25 sized cubes at the coordinate positions) :
The program generates coordinates along the edge of a triangle, but due to the snowball affect the corner cubes always seem have rouge cubes due to coordinates generating at 0.05025 instead of just zero. This create technically a new coordinate next to my target end coordinate of (0,0,0), causing a overlap
Any ideas on how to fix these rounding errors? Should I just cut off the calculations at two decimal points and just accept a margin of error in the calculations?