C++实现坐标的平移和旋转
C++實(shí)現(xiàn)二維坐標(biāo)系的平移和旋轉(zhuǎn)
有這樣一個(gè)問(wèn)題,平面內(nèi)有一個(gè)點(diǎn)集,需要將坐標(biāo)原點(diǎn)平移,到點(diǎn)集的某一點(diǎn),然后在將坐標(biāo)系旋轉(zhuǎn)一定角度使得和點(diǎn)集中地另一個(gè)點(diǎn)重合。
坐標(biāo)變換
坐標(biāo)變換是空間實(shí)體的位置描述,是從一種坐標(biāo)系統(tǒng)變換到另一種坐標(biāo)系統(tǒng)的過(guò)程。通過(guò)建立兩個(gè)坐標(biāo)系統(tǒng)之間一一對(duì)應(yīng)關(guān)系來(lái)實(shí)現(xiàn)。是各種比例尺地圖測(cè)量和編繪中建立地圖數(shù)學(xué)基礎(chǔ)必不可少的步驟。兩個(gè)及以上的坐標(biāo)轉(zhuǎn)換時(shí)由極坐標(biāo)相對(duì)參照確定維數(shù)空間。
坐標(biāo)變換共有五種分別是平移、倍變、旋轉(zhuǎn)、切變、反射,除平移外均以坐標(biāo)原點(diǎn)為基準(zhǔn)點(diǎn),即變換前后坐標(biāo)原點(diǎn)不變。
平移:
當(dāng)把原點(diǎn) 移到 時(shí),平面上任一點(diǎn) 的舊坐標(biāo) 及其新坐標(biāo) 之間有下列關(guān)系:
其中(1)以新坐標(biāo)表示舊坐標(biāo),(2)以舊坐標(biāo)表示新坐標(biāo),叫做“平移公式”或“移軸公式”。
旋轉(zhuǎn)
如下圖, 在2維坐標(biāo)上,有一點(diǎn)p(x, y) , 直線(xiàn)opの長(zhǎng)度為r, 直線(xiàn)op和x軸的正向的夾角為a。 直線(xiàn)op圍繞原點(diǎn)做逆時(shí)針?lè)较騜度的旋轉(zhuǎn),到達(dá)p’ (s,t)
s = r cos(a + b) = r cos(a)cos(b) – r sin(a)sin(b) (1.1)
t = r sin(a + b) = r sin(a)cos(b) + r cos(a) sin(b) (1.2)
其中 x = r cos(a) , y = r sin(a)
代入(1.1), (1.2) ,
s = x cos(b) – y sin(b) (1.3)
t = x sin(b) + y cos(b) (1.4)
代碼實(shí)現(xiàn):
#include <iostream> #include <math.h> #include<fstream> #include<cstdlib> using namespace std;class Transform { public:Transform(int x, double d[]) {pointNums = x;if (x<0)cout << "Not enough" << endl;int c = 0;for (int i = 0; i<x; i++) {for (int j = 0; j<2; j++) {data[i][j] = d[c];c++;}}}void print();int matPoints();void translation(int x,double n,double m) ;void rotation(int x,double ax,double ay);double data[200][200]; // 點(diǎn)容量private:int pointNums; //點(diǎn)的個(gè)數(shù)};//輸出點(diǎn)坐標(biāo) void Transform::print() {for (int i = 0; i<pointNums; i++) {for (int j = 0; j<2; j++) {cout.width(4);cout << data[i][j] << " ";}cout << endl;}cout << endl; }//統(tǒng)計(jì)點(diǎn)的個(gè)數(shù) int Transform::matPoints() {return pointNums; }//二維平移(例如:從p1(x1,y1),平移到p2(x2,y2),n = x2-x1;m=y2-y1) //輸入?yún)?shù):x為點(diǎn)的個(gè)數(shù),n為X軸平移量,m為Y軸平移量。 void Transform::translation(int x,double n,double m) {for (int i = 0; i<x; i++) { data[i][0] = data[i][0] + n;data[i][1] = data[i][1] + m;} }//旋轉(zhuǎn) 將坐標(biāo)軸的X軸旋轉(zhuǎn)到以原點(diǎn)和某一點(diǎn)的連線(xiàn)上 void Transform::rotation(int x,double ax,double ay) {double r = sqrt(ax*ax + ay*ay);double s_a = -ay/r;double c_a = ax/r;for (int i = 0; i<x; i++) {double temp = data[i][0]* c_a - data[i][1]* s_a;data[i][1] = data[i][0]* s_a + data[i][1]* c_a;data[i][0] = temp;} }int main(int argc, char** argv) {double v[100000] = {};int n;int cc; ifstream infile("2.txt",ios::in);int count =0;infile>>n;while(infile){infile>>cc;v[count] = cc;count++;}Transform a(n, v);a.print();//將坐標(biāo)原點(diǎn)移到某一點(diǎn) a.translation(n,-a.data[0][0],-a.data[0][1]);a.print();//將x軸旋轉(zhuǎn),使得x軸與某點(diǎn)重合 double ax = a.data[1][0];double bx = a.data[1][1];a.rotation(n,ax,bx);a.print();return 0; }```cpp 在這里插入代碼片2.txt
10
2 4
6 8
11 14
15 7
22 13
56 7
34 5
13 15
-10 15
10 -19
輸出:
總結(jié)
以上是生活随笔為你收集整理的C++实现坐标的平移和旋转的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Gauss 消元法求解线性方程组
- 下一篇: QT 定时器与动画实现