如果我有一個角度theta(弧度)、一個小角度delta(弧度)和一個目標角度theta prime(弧度),我怎樣才能增加/減少它theta以delta使其更接近角度theta prime?目標是把它推得更近,最終達到和平等theta prime(但不要超越它)。它應該適用于負弧度或高于 Math.PI 或低于 -Math.PI 的弧度。
像這樣的東西
function MoveTheta(theta, delta, theta_prime) {
// magic to move theta by at most delta closer to theta_prime
return new_theta_value;
}
我會一直呼叫 MoveTheta,直到 MoveTheta 等于 theta_prime。這怎么可能寫?
uj5u.com熱心網友回復:
function MoveTheta(theta, delta, theta_prime) {
// find the distance between theta and theta_prime
var diff = theta_prime - theta;
// find the number of times you need to add/subtract delta to theta in order
// to get to theta_prime (without passing theta_prime)
var deltas = Math.floor(diff / delta);
var new_theta_value = theta deltas * delta;
return new_theta_value;
}
這是你要找的嗎?
uj5u.com熱心網友回復:
除非你不關心三角函式的使用。功能,嘗試下一種方法(應該解決零過渡問題,選擇最短方向等):
rot = atan2(cos(th)*sin(th_pr)-cos(th_pr)*sin(th),
cos(th)*cos(th_pr) sin(th_pr)*sin(th))
if rot >= 0
new_th = th min(delta, rot)
else
new_th = th max(-delta, rot)
uj5u.com熱心網友回復:
您可以嘗試以下解決方案:
首先評估與目標角度的距離。然后,如果距離小于步長,則回傳目標角度或原始角度加上步長乘以差的符號以考慮旋轉方向。
function move(
theta, // the original angle
delta, // the step
theta_prime // the target angle
)
{
const diff = theta_prime - theta;
return Math.abs(diff) > delta ? tetha Math.sign(diff) * delta : tetha_prime;
}
如果此方法的結果等于目標角度,則程式完成。
function move(theta, delta, theta_prime) {
const diff = theta_prime - theta;
return Math.abs(diff) > delta ? theta Math.sign(diff) * delta : tetha_prime;
}
let theta = 75;
const delta = 4;
const theta_prime = 32;
while (theta != theta_prime) {
theta = move(theta, delta, theta_prime);
console.log(theta);
}
console.log("done");
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/412191.html
標籤:
