空間坐標(biāo)變換可看作坐標(biāo)點(diǎn)乘以一個(gè)齊次矩陣,其中,齊次矩陣可表示為:
其中:
①區(qū)域的3×3矩陣產(chǎn)生三維圖形的比例、對(duì)稱、旋轉(zhuǎn)、錯(cuò)切等基本變換;
②區(qū)域產(chǎn)生圖形的透視變換;
③區(qū)域產(chǎn)生沿X、Y、Z三個(gè)軸的平移變換;
④區(qū)域產(chǎn)生圖形的總比例變換。
平移變換
平移變換可表示為:
[
x
y
z
1
]
[
1
0
0
0
0
1
0
0
0
0
1
0
l
m
n
1
]
=
[
x
+
l
y
+
m
z
+
n
1
]
\begin{gathered} \quad \begin{bmatrix} x & y & z & 1 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ l & m & n & 1 \end{bmatrix} = \begin{bmatrix} x+l & y+m & z+n & 1 \end{bmatrix} \end{gathered}
[x?y?z?1?]
?100l?010m?001n?0001?
?=[x+l?y+m?z+n?1?]?
繞X軸旋轉(zhuǎn)
空間立體繞X軸旋轉(zhuǎn)某個(gè)角度,實(shí)體上個(gè)點(diǎn)的X坐標(biāo)不變,只有Y、Z坐標(biāo)改變,可表示為:
[
x
y
z
1
]
[
1
0
0
0
0
c
o
s
(
θ
)
s
i
n
(
θ
)
0
0
?
s
i
n
(
θ
)
c
o
s
(
θ
)
0
0
0
0
1
]
=
[
x
y
×
c
o
s
(
θ
)
?
z
×
s
i
n
(
θ
)
y
×
s
i
n
(
θ
)
+
z
×
c
o
s
(
θ
)
1
]
\begin{gathered} \quad \begin{bmatrix} x & y & z & 1 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & cos(\theta) & sin(\theta) & 0 \\ 0 & -sin(\theta) & cos(\theta) & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} = \begin{bmatrix} x & y \times cos(\theta) - z \times sin(\theta) & y \times sin(\theta) + z \times cos(\theta) & 1 \end{bmatrix} \end{gathered}
[x?y?z?1?]
?1000?0cos(θ)?sin(θ)0?0sin(θ)cos(θ)0?0001?
?=[x?y×cos(θ)?z×sin(θ)?y×sin(θ)+z×cos(θ)?1?]?
繞Y軸旋轉(zhuǎn)
空間立體繞Y軸旋轉(zhuǎn)某個(gè)角度,實(shí)體上個(gè)點(diǎn)的Y坐標(biāo)不變,只有X、Z坐標(biāo)改變,可表示為:
[
x
y
z
1
]
[
c
o
s
(
θ
)
0
?
s
i
n
(
θ
)
0
0
1
0
0
s
i
n
(
θ
)
0
c
o
s
(
θ
)
0
0
0
0
1
]
=
[
x
×
c
o
s
(
θ
)
+
z
×
s
i
n
(
θ
)
y
?
x
×
s
i
n
(
θ
)
+
z
×
c
o
s
(
θ
)
1
]
\begin{gathered} \quad \begin{bmatrix} x & y & z & 1 \end{bmatrix} \begin{bmatrix} cos(\theta) & 0 & -sin(\theta) & 0 \\ 0 & 1 & 0 & 0 \\ sin(\theta) & 0 & cos(\theta) & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} = \begin{bmatrix} x \times cos(\theta) + z \times sin(\theta) & y & -x \times sin(\theta) + z \times cos(\theta) & 1 \end{bmatrix} \end{gathered}
[x?y?z?1?]
?cos(θ)0sin(θ)0?0100??sin(θ)0cos(θ)0?0001?
?=[x×cos(θ)+z×sin(θ)?y??x×sin(θ)+z×cos(θ)?1?]?
繞Z軸旋轉(zhuǎn)
空間立體繞Z軸旋轉(zhuǎn)某個(gè)角度,實(shí)體上個(gè)點(diǎn)的Z坐標(biāo)不變,只有X、Y坐標(biāo)改變,可表示為:
[
x
y
z
1
]
[
c
o
s
(
θ
)
s
i
n
(
θ
)
0
0
?
s
i
n
(
θ
)
c
o
s
(
θ
)
0
0
0
0
1
0
0
0
0
1
]
=
[
x
×
c
o
s
(
θ
)
?
y
×
s
i
n
(
θ
)
x
×
s
i
n
(
θ
)
+
y
×
c
o
s
(
θ
)
z
1
]
\begin{gathered} \quad \begin{bmatrix} x & y & z & 1 \end{bmatrix} \begin{bmatrix} cos(\theta) & sin(\theta) & 0 & 0 \\ -sin(\theta) & cos(\theta) & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} = \begin{bmatrix} x \times cos(\theta) - y \times sin(\theta) & x \times sin(\theta) + y \times cos(\theta) & z & 1 \end{bmatrix} \end{gathered}
[x?y?z?1?]
?cos(θ)?sin(θ)00?sin(θ)cos(θ)00?0010?0001?
?=[x×cos(θ)?y×sin(θ)?x×sin(θ)+y×cos(θ)?z?1?]?
python代碼
繞X軸旋轉(zhuǎn)
def rotate_X(x, y, z, alpha):
alpha = alpha * (np.pi / 180)
x_r = x
y_r = np.cos(alpha) * y - np.sin(alpha) * z
z_r = np.sin(alpha) * y + np.cos(alpha) * z
return round(x_r, 4), round(y_r, 4), round(z_r, 4)
繞Y軸旋轉(zhuǎn)
def rotate_Y(x, y, z, beta):
beta = beta * (np.pi / 180)
x_r = np.cos(beta) * x + np.sin(beta) * z
y_r = y
z_r = -np.sin(beta) * x + np.cos(beta) * z
return round(x_r, 4), round(y_r, 4), round(z_r, 4)
繞Z軸旋轉(zhuǎn)
def rotate_Z(x, y, z, gamma):
gamma = gamma * (np.pi / 180)
x_r = np.cos(gamma) * x - np.sin(gamma) * y
y_r = np.sin(gamma) * x + np.cos(gamma) * y
z_r = z
return round(x_r, 4), round(y_r, 4), round(z_r, 4)
為了更直觀的顯示,使用matplotlib將旋轉(zhuǎn)前后的點(diǎn)顯示出來
def plot_3D(x, y, z, x1, y1, z1):
a, b, c = [0, x, x1], \
[0, y, y1], \
[0, z, z1]
d, e, f, g, h, i = [0], [0], [0], [0], [0], [0]
d.append(x)
e.append(y)
f.append(z)
g.append(x1)
h.append(y1)
i.append(z1)
dd, ee, ff = [x, 0, 0, x, x, x], \
[y, y, 0, 0, y, y], \
[z, z, z, z, z, 0]
dd1, ee1, ff1 = [0, x, x, 0, 0, 0], \
[0, 0, y, y, 0, 0], \
[0, 0, 0, 0, 0, z]
dd2, ee2, ff2 = [x, x], \
[0, 0], \
[0, z]
dd3, ee3, ff3 = [0, 0], \
[y, y], \
[0, z]
gg, hh, ii = [x1, 0, 0, x1, x1, x1], \
[y1, y1, 0, 0, y1, y1], \
[z1, z1, z1, z1, z1, 0]
gg1, hh1, ii1 = [0, x1, x1, 0, 0, 0], \
[0, 0, y1, y1, 0, 0], \
[0, 0, 0, 0, 0, z1]
gg2, hh2, ii2 = [x1, x1], \
[0, 0], \
[0, z1]
gg3, hh3, ii3 = [0, 0], \
[y1, y1], \
[0, z1]
ax = plt.axes(projection='3d') # 創(chuàng)建一個(gè)三維的繪圖工程
ax.scatter3D(a, b, c, c='r') # 繪制數(shù)據(jù)點(diǎn) c: 'r'紅色,'y'黃色,等顏色
ax.text(x, y, z, (x, y, z), c='r') # 顯示點(diǎn)坐標(biāo)
ax.text(x1, y1, z1, (x1, y1, z1), c='r')
ax.plot3D(d, e, f, c='b')
ax.plot3D(dd, ee, ff, c='b', linestyle='--')
ax.plot3D(dd1, ee1, ff1, c='b', linestyle='--')
ax.plot3D(dd2, ee2, ff2, c='b', linestyle='--')
ax.plot3D(dd3, ee3, ff3, c='b', linestyle='--')
ax.plot3D(g, h, i, c='y')
ax.plot3D(gg, hh, ii, c='y', linestyle='--')
ax.plot3D(gg1, hh1, ii1, c='y', linestyle='--')
ax.plot3D(gg2, hh2, ii2, c='y', linestyle='--')
ax.plot3D(gg3, hh3, ii3, c='y', linestyle='--')
ax.set_xlabel('X') # 設(shè)置x坐標(biāo)軸
ax.set_ylabel('Y') # 設(shè)置y坐標(biāo)軸
ax.set_zlabel('Z') # 設(shè)置z坐標(biāo)軸
ax.grid(False) # 關(guān)閉網(wǎng)格
plt.show()
現(xiàn)設(shè)初始點(diǎn)為P0(0,0,6),P0先繞X軸旋轉(zhuǎn)6°,再繞Y軸旋轉(zhuǎn)45°得到P2
完整代碼:文章來源:http://www.zghlxwxcb.cn/news/detail-645235.html
import numpy as np
import matplotlib.pyplot as plt
# 1、繞Z周旋轉(zhuǎn)gamma角
def rotate_Z(x, y, z, gamma):
gamma = gamma * (np.pi / 180)
x_r = np.cos(gamma) * x - np.sin(gamma) * y
y_r = np.sin(gamma) * x + np.cos(gamma) * y
z_r = z
return round(x_r, 4), round(y_r, 4), round(z_r, 4)
# 2、繞Y軸旋轉(zhuǎn)beta角
def rotate_Y(x, y, z, beta):
beta = beta * (np.pi / 180)
x_r = np.cos(beta) * x + np.sin(beta) * z
y_r = y
z_r = -np.sin(beta) * x + np.cos(beta) * z
return round(x_r, 4), round(y_r, 4), round(z_r, 4)
# 3、繞X軸旋轉(zhuǎn)alpha角
def rotate_X(x, y, z, alpha):
alpha = alpha * (np.pi / 180)
x_r = x
y_r = np.cos(alpha) * y - np.sin(alpha) * z
z_r = np.sin(alpha) * y + np.cos(alpha) * z
return round(x_r, 4), round(y_r, 4), round(z_r, 4)
def plot_3D(x, y, z, x1, y1, z1):
a, b, c = [0, x, x1], \
[0, y, y1], \
[0, z, z1]
d, e, f, g, h, i = [0], [0], [0], [0], [0], [0]
d.append(x)
e.append(y)
f.append(z)
g.append(x1)
h.append(y1)
i.append(z1)
dd, ee, ff = [x, 0, 0, x, x, x], \
[y, y, 0, 0, y, y], \
[z, z, z, z, z, 0]
dd1, ee1, ff1 = [0, x, x, 0, 0, 0], \
[0, 0, y, y, 0, 0], \
[0, 0, 0, 0, 0, z]
dd2, ee2, ff2 = [x, x], \
[0, 0], \
[0, z]
dd3, ee3, ff3 = [0, 0], \
[y, y], \
[0, z]
gg, hh, ii = [x1, 0, 0, x1, x1, x1], \
[y1, y1, 0, 0, y1, y1], \
[z1, z1, z1, z1, z1, 0]
gg1, hh1, ii1 = [0, x1, x1, 0, 0, 0], \
[0, 0, y1, y1, 0, 0], \
[0, 0, 0, 0, 0, z1]
gg2, hh2, ii2 = [x1, x1], \
[0, 0], \
[0, z1]
gg3, hh3, ii3 = [0, 0], \
[y1, y1], \
[0, z1]
ax = plt.axes(projection='3d') # 創(chuàng)建一個(gè)三維的繪圖工程
ax.scatter3D(a, b, c, c='r') # 繪制數(shù)據(jù)點(diǎn) c: 'r'紅色,'y'黃色,等顏色
ax.text(x, y, z, (x, y, z), c='r') # 顯示點(diǎn)坐標(biāo)
ax.text(x1, y1, z1, (x1, y1, z1), c='r')
ax.plot3D(d, e, f, c='b')
ax.plot3D(dd, ee, ff, c='b', linestyle='--')
ax.plot3D(dd1, ee1, ff1, c='b', linestyle='--')
ax.plot3D(dd2, ee2, ff2, c='b', linestyle='--')
ax.plot3D(dd3, ee3, ff3, c='b', linestyle='--')
ax.plot3D(g, h, i, c='y')
ax.plot3D(gg, hh, ii, c='y', linestyle='--')
ax.plot3D(gg1, hh1, ii1, c='y', linestyle='--')
ax.plot3D(gg2, hh2, ii2, c='y', linestyle='--')
ax.plot3D(gg3, hh3, ii3, c='y', linestyle='--')
ax.set_xlabel('X') # 設(shè)置x坐標(biāo)軸
ax.set_ylabel('Y') # 設(shè)置y坐標(biāo)軸
ax.set_zlabel('Z') # 設(shè)置z坐標(biāo)軸
ax.grid(False) # 關(guān)閉網(wǎng)格
plt.show()
if __name__ == '__main__':
x, y, z = 0, 0, 6
x1, y1, z1 = rotate_X(x, y, z, 6)
x2, y2, z2 = rotate_Y(x1, y1, z1, 45)
print(x2, y2, z2)
plot_3D(x, y, z, x2, y2, z2)
C++代碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-645235.html
#include<iostream>
#include <iomanip>
#include<math.h>
#define PAI acos(-1)
using namespace std;
void rotate_Z(double x, double y, double z, double theta);
void rotate_Y(double x, double y, double z, double theta);
void rotate_X(double x, double y, double z, double theta);
int main()
{
double x = 0;
double y = 0;
double z = 6;
rotate_Z(x,y,z,45);
return 0;
}
void rotate_Z(double x, double y, double z, double theta)
{
double theta_r = theta * (PAI/180);
double x_r = cos(theta_r) * x - sin(theta_r) * y;
double y_r = sin(theta_r) * x + sin(theta_r) * y;
double z_r = z;
cout.setf(ios::fixed);
cout << "X:" << setprecision(4) << x_r << endl;
cout << "Y:" << setprecision(4) << y_r << endl;
cout << "Z:" << setprecision(4) << z_r << endl;
}
void rotate_Y(double x, double y, double z, double theta)
{
double theta_r = theta * (PAI/180);
double x_r = cos(theta_r) * x + sin(theta_r) * z;
double y_r = y;
double z_r = -sin(theta_r) * x + cos(theta_r) * z;
cout.setf(ios::fixed);
cout << "X:" << setprecision(4) << x_r << endl;
cout << "Y:" << setprecision(4) << y_r << endl;
cout << "Z:" << setprecision(4) << z_r << endl;
}
void rotate_X(double x, double y, double z, double theta)
{
double theta_r = theta * (PAI/180);
double x_r = x;
double y_r = cos(theta_r) * y - sin(theta_r) * z;
double z_r = sin(theta_r) * y + cos(theta_r) * z;
cout.setf(ios::fixed);
cout << "X:" << setprecision(4) << x_r << endl;
cout << "Y:" << setprecision(4) << y_r << endl;
cout << "Z:" << setprecision(4) << z_r << endl;
}
到了這里,關(guān)于空間坐標(biāo)變換(Python&C++實(shí)現(xiàn))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!