获得给定角度的 x 和 y 增量值
get a value of x and y increment with a given angle
注意:您的解决方案可以在浏览器js或python中。 angleX 将以弧度为单位
我想要一个带有三个参数的方法:-
- aCord -(一个 object/dictionary 与点 A 的坐标)
- bCord - (一个 object/dictionary 与点 B 的坐标)
- angleX - (旋转角度)
假设我有一个点A和一个点B(屏幕上的任何位置。不一定如图所示平行)
B点旋转X度(以B点为圆心)形成C点
但是,我想增加 b.x 和 b.y,所以它与 c.x 和 c.y
相同
您的函数应该 return 一个具有两个值的 object/dictionary。一个 'x-increment' 和一个 'y-increment' (这是我应该增加 B 点的 x 和 y 的多少)
这是我现有的代码(在 js 中)
function getIncrement(aCord, bCord, angleX) {
let r = Math.sqrt(Math.pow(aCord.x - bCord.x, 2) + Math.pow(aCord.x - bCord.x, 2));
let angleY = Math.atan(Math.abs(aCord.y - bCord.y) / Math.abs(aCord.x - bCord.x));
let cCord = {
x: Math.cos(angleY + angleX) * r,
y: Math.sin(angleY + angleX) * r
};
return {
xIncrement: cCord.x - aCord.x,
yIncrement: cCord.y - aCord.y
};
}
抱歉,如果我的解释不够好。有不懂的可以在评论里解释
在一般情况下,假设您有一个圆心为 A(x,y) 且半径为 r 的圆。初始点的位置是从 (a+r,b) 沿圆的 θ 弧度。
圆的参数方程是(x,y)=(a + r cosθ,b + r sinθ)。
在你的例子中,你想增加 ϕ 弧度。那么新的点是
(a+ r cos(θ+φ),b+ r sin(θ+φ))
也许,如果你能对你的问题提供更多解释。它将更多地帮助解决问题。同时我将挖掘没有半径的答案,即 A 和 B 之间的距离
这是在 JavaScript 片段中使用的函数演示。在方框内移动鼠标移动B点(A固定),A-C直线会动态添加。在输入框中更改角度:
// The function to calculate C:
function getC(a, b, angle) {
let sign = -1; // change to 1 if Y axis goes upward
let radius = Math.sqrt((a.x-b.x)**2 + (a.y-b.y)**2);
angle += Math.atan2(sign*(b.y-a.y), b.x-a.x);
return {
x: a.x + Math.cos(angle)*radius,
y: a.y + sign*Math.sin(angle)*radius
};
}
// I/O handling
function drawLine(ctx, a, b, color="black") {
ctx.beginPath();
ctx.moveTo(a.x, a.y);
ctx.lineTo(b.x, b.y);
ctx.strokeStyle = color;
ctx.stroke();
}
function text(ctx, a, txt, color) {
ctx.fillStyle = color;
ctx.fillText(txt, a.x+2, a.y-2);
}
function refresh(ctx, a, b, c) {
outputB.textContent = Math.round(b.x) + "," + Math.round(b.y);
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
drawLine(ctx, a, b, "black");
drawLine(ctx, a, c, "red");
text(ctx, a, "A", "black");
text(ctx, b, "B", "black");
text(ctx, c, "C", "red");
}
let ctx = document.querySelector("canvas").getContext("2d");
let inputDegrees = document.querySelector("#angle");
let outputB = document.querySelector("#b");
let a = { x: 200, y: 75 };
ctx.canvas.onmousemove = function(e) {
let b = {
x: e.clientX-e.target.offsetLeft,
y: e.clientY-e.target.offsetTop,
};
let c = getC(a, b, inputDegrees.value*Math.PI/180);
refresh(ctx, a, b, c);
}
canvas { border: 1px solid }
Angle(degrees): <input id="angle" type="number" value="15"><br>
A=(200,75) B=(<span id="b"></span>)<br>
<canvas width="400" height="150"></canvas>
注意:您的解决方案可以在浏览器js或python中。 angleX 将以弧度为单位
我想要一个带有三个参数的方法:-
- aCord -(一个 object/dictionary 与点 A 的坐标)
- bCord - (一个 object/dictionary 与点 B 的坐标)
- angleX - (旋转角度)
假设我有一个点A和一个点B(屏幕上的任何位置。不一定如图所示平行)
B点旋转X度(以B点为圆心)形成C点
但是,我想增加 b.x 和 b.y,所以它与 c.x 和 c.y
相同您的函数应该 return 一个具有两个值的 object/dictionary。一个 'x-increment' 和一个 'y-increment' (这是我应该增加 B 点的 x 和 y 的多少)
这是我现有的代码(在 js 中)
function getIncrement(aCord, bCord, angleX) {
let r = Math.sqrt(Math.pow(aCord.x - bCord.x, 2) + Math.pow(aCord.x - bCord.x, 2));
let angleY = Math.atan(Math.abs(aCord.y - bCord.y) / Math.abs(aCord.x - bCord.x));
let cCord = {
x: Math.cos(angleY + angleX) * r,
y: Math.sin(angleY + angleX) * r
};
return {
xIncrement: cCord.x - aCord.x,
yIncrement: cCord.y - aCord.y
};
}
抱歉,如果我的解释不够好。有不懂的可以在评论里解释
在一般情况下,假设您有一个圆心为 A(x,y) 且半径为 r 的圆。初始点的位置是从 (a+r,b) 沿圆的 θ 弧度。
圆的参数方程是(x,y)=(a + r cosθ,b + r sinθ)。
在你的例子中,你想增加 ϕ 弧度。那么新的点是 (a+ r cos(θ+φ),b+ r sin(θ+φ))
也许,如果你能对你的问题提供更多解释。它将更多地帮助解决问题。同时我将挖掘没有半径的答案,即 A 和 B 之间的距离
这是在 JavaScript 片段中使用的函数演示。在方框内移动鼠标移动B点(A固定),A-C直线会动态添加。在输入框中更改角度:
// The function to calculate C:
function getC(a, b, angle) {
let sign = -1; // change to 1 if Y axis goes upward
let radius = Math.sqrt((a.x-b.x)**2 + (a.y-b.y)**2);
angle += Math.atan2(sign*(b.y-a.y), b.x-a.x);
return {
x: a.x + Math.cos(angle)*radius,
y: a.y + sign*Math.sin(angle)*radius
};
}
// I/O handling
function drawLine(ctx, a, b, color="black") {
ctx.beginPath();
ctx.moveTo(a.x, a.y);
ctx.lineTo(b.x, b.y);
ctx.strokeStyle = color;
ctx.stroke();
}
function text(ctx, a, txt, color) {
ctx.fillStyle = color;
ctx.fillText(txt, a.x+2, a.y-2);
}
function refresh(ctx, a, b, c) {
outputB.textContent = Math.round(b.x) + "," + Math.round(b.y);
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
drawLine(ctx, a, b, "black");
drawLine(ctx, a, c, "red");
text(ctx, a, "A", "black");
text(ctx, b, "B", "black");
text(ctx, c, "C", "red");
}
let ctx = document.querySelector("canvas").getContext("2d");
let inputDegrees = document.querySelector("#angle");
let outputB = document.querySelector("#b");
let a = { x: 200, y: 75 };
ctx.canvas.onmousemove = function(e) {
let b = {
x: e.clientX-e.target.offsetLeft,
y: e.clientY-e.target.offsetTop,
};
let c = getC(a, b, inputDegrees.value*Math.PI/180);
refresh(ctx, a, b, c);
}
canvas { border: 1px solid }
Angle(degrees): <input id="angle" type="number" value="15"><br>
A=(200,75) B=(<span id="b"></span>)<br>
<canvas width="400" height="150"></canvas>