标签:fun a+b 方法 计算方法 stc and abs color 就是
this.Dsct = function (x1, y1, x2, y2) { var r, dx, dy; dx = x1 - x2; dy = y1 - y2; r = Math.sqrt(dx * dx + dy * dy); return r; } /* 直线到原点距离 d=abs(c)/sqrt(a*a+b*b) 直线公式 a*x+b*y+c=0; */ this.LineToZeroDstc = function (x1, y1, x2, y2) { var a, b, c, r; // 模拟方式 a = y2 - y1; b = x1 - x2; c = x2 * y1 - x1 * y2; r = Math.abs(c) / Math.sqrt(a * a + b * b); return r; } /* C1 D C2 A ------------------------------B */ this.Pt2LineDstc = function (px, py, x1, y1, x2, y2) { var rbd, rac, rbc, r, rd2, rab; rbd = LineToZeroDstc(x1 - px, y1 - py, x2 - px, y2 - py); // 平移后到原点距离 rac = Dsct(px, py, x1, y1); rbc = Dsct(px, py, x2, y2); rab = Dsct(x1, y1, x2, y2); rd2 = rbd * rbd + rab * rab; if (rac * rac > rd2 || rbc * rbc > rd2) r = Math.min(rac, rbc); else r = rbd; return r; } // 用面积公式 // 2面积除长度就是高 this.Pt2LineDstc2 = function (px, py, x1, y1, x2, y2) { var s2, x3, y3, rab; x3 = px; y3 = py; s2 = Math.abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)); rab = Dsct(x1, y1, x2, y2); r = s / rab; return r; } this.test_Pt2LineDstc = function () { var pt, i, n, k, r1, r2; n = 1000; pt = []; for (i = 0; i < n; i++) { for (k = 0; k < 10; k++) pt[k] = Math.floor(Math.random() * 100); r1 = Pt2LineDstc(pt[0], pt[1], pt[2], pt[3], pt[4], pt[5]); r2 = Pt2LineDstc2(pt[0], pt[1], pt[2], pt[3], pt[4], pt[5]); if (Math.abs(r1 - r2) > 0.01) { console.log(r1, r2, "-", pt[0], pt[1], pt[2], pt[3], pt[4], pt[5]); } } }
标签:fun a+b 方法 计算方法 stc and abs color 就是
原文地址:https://www.cnblogs.com/zzz3265/p/14686422.html