python绘制圆柱体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import random
import numpy as np
import matplotlib.pyplot as plt
 
def plot_cylinder(center, radius, height, num_points=100):
    # 生成圆柱体的侧面点坐标
    theta = np.linspace(0, 2*np.pi, num_points)
    intervalZ = np.floor(height/0.05)
    indx2 = [ix for ix in range(int(intervalZ))]
    random_int2 = random.sample(indx2, int(intervalZ - 10))
    xx = []
    yy = []
    zz = []
    print(random_int2)
    for j in random_int2:
        indx = [i for i in range(num_points)]
        random_int = random.sample(indx, 50)
        theta2 = theta[random_int]
        print(random_int)
        x = center[0] + radius * np.cos(theta2)
        y = center[1] + radius * np.sin(theta2)
 
        z = (center[2]+0.05*j)*np.ones_like(x)
        xx = np.hstack((xx, x))
        yy = np.hstack((yy, y))
        zz = np.hstack((zz, z))
        print(j)
 
    #np.linspace(center[2], center[2]+height, num_points)
    return xx,yy,zz
 
x,y,z=plot_cylinder((0, 0, 0), 1, 2)
print(x.shape,y.shape,z.shape)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z)
plt.show()

  

随机生成一个圆柱体点集合:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import random
import numpy as np
import matplotlib.pyplot as plt
 
def plot_cylinder(center, radius, height, num_points=100):
    # 生成圆柱体的侧面点坐标
    theta = np.linspace(0, 2*np.pi, num_points)
    intervalZ = height/num_points
    indx2 = [ix for ix in range(num_points)]
    random_int2 = random.sample(indx2, 90)
    xx = []
    yy = []
    zz = []
    print(random_int2)
    for j in random_int2:
        indx = [i for i in range(num_points)]
        random_int = random.sample(indx, 50)
        theta2 = theta[random_int]
        print(random_int)
        x = center[0] + radius * np.cos(theta2)
        y = center[1] + radius * np.sin(theta2)
 
        z = (center[2]+intervalZ*j)*np.ones_like(x)
        xx = np.hstack((xx, x))
        yy = np.hstack((yy, y))
        zz = np.hstack((zz, z))
        print(j)
 
    #np.linspace(center[2], center[2]+height, num_points)
    return xx,yy,zz
 
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 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)
 
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)
 
x,y,z = plot_cylinder((0, 0, 0), 0.2, 1)
x_new=[]
y_new=[]
z_new=[]
theta = np.linspace(0,360, 360)
random_int_angle = random.randint(1,360)
random_int_angle2 = random.randint(1,360)
for x_t, y_t, z_t in zip(x, y, z):
    x1, y1, z1 = rotate_X(x_t, y_t, z_t, theta[random_int_angle])
    x2, y2, z2 = rotate_Y(x1, y1, z1, theta[random_int_angle2])
    x_new = np.hstack((x_new,x2))
    y_new = np.hstack((y_new,y2))
    z_new = np.hstack((z_new,z2))
 
print(x.shape,y.shape,z.shape)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x_new, y_new, z_new)
ax.set_xlim([-1.5 * 1, 1.5 * 1])
ax.set_ylim([-1.5 * 1, 1.5 * 1])
ax.set_zlim([-1, 2])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

 

随机生成一个圆柱体

  

 

posted @   太一吾鱼水  阅读(93)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
历史上的今天:
2015-08-06 SLAM学习笔记(1)基本概念
2013-08-06 判断点在多边形内
点击右上角即可分享
微信分享提示