我已經搜索了stackoverflow並找到了有用的和了解到2D形狀旋轉的this question。旋轉2D多邊形形狀算法
我在這樣的格式
int x1=-30, x2=-15, x3=20, x4=30;
int my1=-30,y2=-15,y3=0,y4=15,y5=20,y6=30;
的座標,並有一些中心和樞轉點這樣
int xc=320, yc=240;//Center of the figure
int xp=0, yp=0;//Pivot point for this figure
我使用該功能以繪製形狀
void draw_chair()
{
int loc_xc = xc+xp;
int loc_yc = yc+yp;
line(x2+loc_xc,my1+loc_yc,x2+loc_xc,y5+loc_yc);
line(x3+loc_xc,my1+loc_yc,x3+loc_xc,y5+loc_yc);
line(x2+loc_xc,my1+loc_yc,x3+loc_xc,my1+loc_yc);
line(x2+loc_xc,y2+loc_yc,x3+loc_xc,y2+loc_yc);
line(x2+loc_xc,y3+loc_yc,x3+loc_xc,y3+loc_yc);
line(x1+loc_xc,y4+loc_yc,x4+loc_xc,y4+loc_yc);
line(x2+loc_xc,y3+loc_yc,x1+loc_xc,y4+loc_yc);
line(x3+loc_xc,y3+loc_yc,x4+loc_xc,y4+loc_yc);
line(x1+loc_xc,y4+loc_yc,x1+loc_xc,y6+loc_yc);
line(x4+loc_xc,y4+loc_yc,x4+loc_xc,y6+loc_yc);
}
的問題是,現在我很困惑如何計算旋轉後的x和y值
我試圖谷歌,發現這段代碼旋轉
int tempx=x1;
x1=tempx*cos(angle)-my1*sin(angle);
my1=tempx*sin(angle)+my1*cos(angle);
tempx=x2;
x2=tempx*cos(angle)-y2*sin(angle);
y2=tempx*sin(angle)+y2*cos(angle);
tempx=x3;
x3=tempx*cos(angle)-y3*sin(angle);
y3=tempx*sin(angle)+y3*cos(angle);
tempx=x4;
x4=tempx*cos(angle)-y4*sin(angle);
y4=tempx*sin(angle)+y4*cos(angle);
我試過,但它沒有旋轉的形狀正常,而是這個代碼轉換捏成其他一些奇怪的形狀。另外我有4個點和6個點,那麼如何計算每個點的新值?
任何想法?或提示? 謝謝
而不是從Google複製/粘貼代碼,瞭解旋轉矩陣如何工作。 – Blender