diff --git a/src/geo/base.js b/src/geo/base.js index 89ba5eb4..019afacf 100644 --- a/src/geo/base.js +++ b/src/geo/base.js @@ -357,23 +357,32 @@ * from https://stackoverflow.com/questions/4103405/what-is-the-algorithm-for-finding-the-center-of-a-circle-from-three-points */ function center2d(A,B,C,rad) { - let yDelta_a = B.y - A.y, - xDelta_a = B.x - A.x, - yDelta_b = C.y - B.y, - xDelta_b = C.x - B.x, - aSlope = yDelta_a/xDelta_a, - bSlope = yDelta_b/xDelta_b, - center = {x:0, y:0, z:0}; - center.x = (aSlope*bSlope*(A.y - C.y) + bSlope*(A.x + B.x) - aSlope*(B.x+C.x) )/(2* (bSlope-aSlope) ); - center.y = -1*(center.x - (A.x+B.x)/2)/aSlope + (A.y+B.y)/2; + // let yDelta_a = B.y - A.y, + // xDelta_a = B.x - A.x, + // yDelta_b = C.y - B.y, + // xDelta_b = C.x - B.x, + // aSlope = yDelta_a/xDelta_a, + // bSlope = yDelta_b/xDelta_b, + // center = {x:0, y:0, z:0}; + // center.x = (aSlope*bSlope*(A.y - C.y) + bSlope*(A.x + B.x) - aSlope*(B.x+C.x) )/(2* (bSlope-aSlope) ); + // center.y = -1*(center.x - (A.x+B.x)/2)/aSlope + (A.y+B.y)/2; + let center = center3d(A,B,C); if (rad) { let dx = center.x - A.x; let dy = center.y - A.y; center.r = Math.sqrt(dx*dx + dy*dy) + // if (isNaN(center.r)) console.log('NaN',{dx,dy,A,B,C,center,xDelta_a,xDelta_b,yDelta_a,yDelta_b,aSlope,bSlope}); } return center; } + function thetaDiff(n1, n2, sign) { + let diff = n2 - n1; + while (diff < -Math.PI) diff += Math.PI * 2; + while (diff > Math.PI) diff -= Math.PI * 2; + return sign ? diff : Math.abs(diff); + } + /** ****************************************************************** * Connect to base ******************************************************************* */ @@ -459,6 +468,7 @@ area2, center2d, center3d, + thetaDiff, distSq : dist2, distSqv2 : dist2v2, inRange, diff --git a/src/kiri/print.js b/src/kiri/print.js index 92ddf247..494e05cf 100644 --- a/src/kiri/print.js +++ b/src/kiri/print.js @@ -139,6 +139,10 @@ const beltaxis = { X: "X", Y: "Z", Z: "Y", E: "E", F: "F" }; const beltfact = Math.cos(Math.PI/4); + function LOG() { + console.log(...[...arguments].map(o => Object.clone(o))); + } + function G2G3(g2, line) { const rec = {}; @@ -158,7 +162,7 @@ dx: rec.X - pos.X, dy: rec.Y - pos.Y }; - let hml = Math.sqrt(mid.dx * mid.dx + mid.dy * mid.dy) / 2; + let hml = Math.sqrt(mid.dx * mid.dx + mid.dy * mid.dy); let s2 = BASE.newSlope({ x: pos.X, y: pos.Y @@ -166,38 +170,45 @@ x: rec.X, y: rec.Y }).normal().toUnit(); - let ln = Math.sqrt(rec.R*rec.R - hml*hml); - center.x = pos.X + ln * mid.dx; - center.y = pos.Y + ln * mid.dy; + let ln = Math.sqrt(Math.abs(rec.R*rec.R - hml*hml)); + LOG({hml,s2,ln,mid}); + center.x = mid.x - ln * s2.dx; + center.y = mid.y - ln * s2.dy; } else { console.log({malfomed_arc: line}); } // line angles - let a1 = Math.atan2(center.y - pos.Y, center.x - pos.X); - let a2 = Math.atan2(center.y - rec.Y, center.x - rec.X); - let ad = a2 - a1; - let steps = 20; - let step = (Math.abs(ad) > 0.001? ad : Math.PI * 2) / steps; - let rot = step; + let a1 = Math.atan2(pos.Y - center.y, pos.X - center.x); + let a2 = Math.atan2(rec.Y - center.y, rec.X - center.x); + // let a1 = Math.atan2(center.y - pos.Y, center.x - pos.X); + // let a2 = Math.atan2(center.y - rec.Y, center.x - rec.X); + let ad = BASE.util.thetaDiff(a1, a2); + let steps = 5; + let step = (ad > 0.001 ? ad : Math.PI * 2) / steps; + let rot = a1; + let d1 = a1 * 180/Math.PI; + let d2 = a2 * 180/Math.PI; - console.log({pos, rec, center, a1, a2, step, rot}); + LOG({pos, rec, center, a1, a2, ad, d1, d2, step, rot}); + G0G1(false, [`X${center.x}`, `Y${center.y}`, `E1`]); + G0G1(false, [`X${rec.X}`, `Y${rec.Y}`, `E1`]); // rotate p around o // p'x = cos(theta) * (px-ox) - sin(theta) * (py-oy) + ox // p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy let pc = { X: pos.X, Y: pos.Y }; - for (let i=0; i