我在 cpp 的駕駛模擬器中有一個電路,我想知道如何圍繞原點旋轉電路,我試圖深入研究一些三角函式,因為我知道這對我有幫助,但我正處于我的地步米卡住了。
供您參考,電路存盤在一對向量中(std::vector<pair<double,double> 道路)。
這是我的示例電路的樣子(紅色一個),我想做的是另外兩個迭代(黃色和綠色)。
電路分三步:

這是存盤在道路內的示例
x : 170, y : 0
x : -170, y : 0`
x : 170, y : 0
x : -170, y : 0
x : 170, y : -0.69632
x : -170, y : -0.69632
x : 170, y : -5.57056
x : -170, y : -5.57056
x : 170, y : -18.8006
x : -170, y : -18.8006
x : 170, y : -44.5645
x : -170, y : -44.5645
x : 170, y : -0.69632
x : -170, y : -0.69632
x : 170, y : -5.57056
x : -170, y : -5.57056
x : 170, y : -18.8006
x : -170, y : -18.8006
x : 170, y : -44.5645
x : -170, y : -44.5645
...
For going foward, backward, straft to the left or the right, it's quite easy as i only need to decrease/ increase all x/y as i go in one direction, but the problem comes when i want to rotate. I know that, in theory I need to make sqrt(x2 y2) to find the distance from the origin (aka the driver) and apply a rotation in consideration of that distance (as a ratio, the lesser the distance is the lesser it needs to rotate).
But for now, I'm stuck and I don't know how to do that, can anyone provide my some sort of explanation on how to do this ?
uj5u.com熱心網友回復:
您只需應用 2d 旋轉矩陣 ( https://en.wikipedia.org/wiki/Rotation_matrix )。這應該可行,您只需在每個點進行迭代:
#include <math.h>
void rotate(float &x,float &y,float teta)
{
float newX = x*cos(teta) y*sin(teta);
float newY = x*sin(teta)-y*cos(teta);
x = newX;
y = newY;
}
請記住,這個teta角的單位是弧度而不是度數。
uj5u.com熱心網友回復:
我為“圍繞原點旋轉點的數學”做了一個谷歌。這是我得到的第一個打擊:
https://academo.org/demos/rotation-about-point/
它很好地解釋了數學。您沒有顯示您的代碼,但如果您只是圍繞原點旋轉一定數量的度數(或弧度),那么每個點的數學運算非常簡單。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/441483.html
標籤:c math trigonometry
