牙叔教程 简单易懂
直线两点式转一般式
function getLineGeneralEquationFromTwoPoint(point1, point2) { let x1 = point1.x; let y1 = point1.y; let x2 = point2.x; let y2 = point2.y; let a = y2 - y1; let b = x1 - x2; let c = x2 * y1 - x1 * y2; return { a: a, b: b, c: c, }; }
两条直线的交点
function getFocusCoordinatesOfTwoLines(line1, line2) { var x1 = line1.x1; var y1 = line1.y1; var x2 = line1.x2; var y2 = line1.y2; var x3 = line2.x1; var y3 = line2.y1; var x4 = line2.x2; var y4 = line2.y2; var x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)); var y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)); return { x: x, y: y }; }
线段延长后的坐标计算
function extendEndPoint(startPoint, endPoint, distance) { var A = [startPoint.x, startPoint.y]; var B = [endPoint.x, endPoint.y]; // 横坐标差, 纵坐标差 var aToB = [B[0] - A[0], B[1] - A[1]]; // A点到B点的线段长度 var aToBDistance = Math.sqrt(aToB[0] * aToB[0] + aToB[1] * aToB[1]); // 一份有多长 // 横坐标一份有多长 // 纵坐标一份有多长 var atoBNormalization = [aToB[0] / aToBDistance, aToB[1] / aToBDistance]; // 延长 distance 个像素就乘以 distance, 加上终点 var newEndPoint = { x: atoBNormalization[0] * distance + B[0], y: atoBNormalization[1] * distance + B[1], }; return newEndPoint; }
已知三个点, 求夹角
下面是求角A, 返回值是角度
function getAngle(pointA, pointB, pointC) { var lengthAB = Math.sqrt(Math.pow(pointA.x - pointB.x, 2) + Math.pow(pointA.y - pointB.y, 2)), lengthAC = Math.sqrt(Math.pow(pointA.x - pointC.x, 2) + Math.pow(pointA.y - pointC.y, 2)), lengthBC = Math.sqrt(Math.pow(pointB.x - pointC.x, 2) + Math.pow(pointB.y - pointC.y, 2)); var cosA = (Math.pow(lengthAB, 2) + Math.pow(lengthAC, 2) - Math.pow(lengthBC, 2)) / (2 * lengthAB * lengthAC); var angleA = Math.round((Math.acos(cosA) * 180) / Math.PI); return angleA; } let arr = [ { x: 1, y: 0 }, { x: 0.5, y: 1 }, { x: 0, y: 0 }, ]; let r = getAngle(arr[0], arr[1], arr[2]); console.log(r);
已知点和直线, 求对称点坐标
// 求: 点P(x0,y0)关于直线 Ax + By + C = 0 对称的点P'(x,y)
// 计算公式
// x=x0-2Ad, y=y0-2Bd
// d=(Ax0+By0+C)/(A^2+B^2)
function calculateSymmetryPoint(P, A, B, C) { let d = (A * P.x + B * P.y + C) / (Math.pow(A, 2) + Math.pow(B, 2)); let x = P.x - 2 * A * d; let y = P.y - 2 * B * d; return { x: x, y: y }; } let P = { x: 1, y: 3, }; let A = 2; let B = 1; let C = 5; let r = calculateSymmetryPoint(P, A, B, C); console.log(r);
已知两点, 求直线点斜式公式
let A = { x: 1, y: 3, }; let B = { x: 2, y: 4, }; // 点斜式直线 // 斜率 let k = (B.y - A.y) / (B.x - A.x); // 偏移量 let b = A.y - k * A.x; console.log("k = " + k + " b = " + b);
两点距离
function getDistance(pointA, pointB) { return Math.sqrt(Math.pow(pointA.x - pointB.x, 2) + Math.pow(pointA.y - pointB.y, 2)); }
已知两点, 求直线角度
顺时针,角度为正;
逆时针, 角度为负
function calcAngle(x1, y1, x2, y2) { var angle = Math.atan2(y1 - y2, x2 - x1); //弧度 var theta = angle * (180 / Math.PI); // 角度 theta < 0 && (theta = 360 + theta); return theta; } let x1 = 0; let y1 = 0; let x2 = 1; let y2 = -1; log(calcAngle(x1, y1, x2, y2));
名人名言
思路是最重要的, 其他的百度, bing, stackoverflow, github, 安卓文档, autojs文档, 最后才是群里问问
--- 牙叔教程
声明
部分内容来自网络
本教程仅用于学习, 禁止用于其他用途