Tell us what’s happening:
Describe your issue in detail here.
Floating point rounding issue… Wrong floating-point data being appended to the final array. Where specifically do I need to append the statement internally in order to accommodate the final appending process…?
I would do this via
[details]
Math.floor(expr*10000)/10000;
[/details] … Could someone please help?
**Your code so far**
const hDist = (p1,p2) => Math.hypot(...p1.map((e, i) => e-p2[i]))/2;
const pAng = (p1,p2) => Math.atan(p1.map((e,i) => e-p2[i]).reduce(
(p, c) => c/p, 1));
const solveF = (p,r) => t => [r*Math.cos(t)+p[0], r*Math.sin(t)+p[1]];
const diamPts = (p1,p2) => p1.map((e,i) => e+(p2[i]-e)/2);
const getCircles = (...args) => {
const [p1,p2,s] = args;
const solve = solveF(p1,s), halfDist = hDist(p1,p2);
let debugMsg = `p1: ${p1}, p2: ${p2}, r:${s}`;
console.log(`${debugMsg}`);
let msg = `Result: `, dCheck = Math.sign(s-halfDist), newArr = [];
switch (dCheck) {
case 0:
s ? newArr.push(diamPts(p1,p2)) : (msg='Radius Zero');
break;
case 1:
if (!halfDist) {
msg = 'Coincident point. Infinite solutions';
} else {
let theta = pAng(p1,p2), theta2 = Math.acos(halfDist/s);
[1,-1].map(e => solve(theta + e*theta2)).forEach(e =>
newArr.push(e));
}
break;
case -1:
msg ='No intersection. Points further apart than circle diameter';
break;
}
console.log(`BEFORE: newArr: ${newArr}`);
newArr.forEach((el) => el = [Math.floor(el[0]*10000)/10000,
Math.floor(el[1]*10000)/10000]);
console.log(`AFTER: newArr: ${newArr}`);
return (msg !=`Result: `) ? (msg): newArr;
};
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
Challenge: Circles of given radius through two points
Link to the challenge: