from mpltoolkits.mplot3d.art3d import Poly3DCollection import matplotlib.pyplot as plt…
- User
from mpl_toolkits.mplot3d.art3d import Poly3DCollection import matplotlib.pyplot as plt import numpy as np
def midpoint_displacement(vertices, depth, roughness, triangles=None, displacement_cache=None): if triangles is None: triangles = [] if displacement_cache is None: displacement_cache = {}
codeif depth == 0: triangles.append(vertices) return triangles def get_displacement(point): key = tuple(point[:2]) if key not in displacement_cache: displacement_cache[key] = point[2] + np.random.uniform(-roughness, roughness) return displacement_cache[key] mid1 = [(vertices[0][i] + vertices[1][i]) / 2 for i in range(3)] mid2 = [(vertices[1][i] + vertices[2][i]) / 2 for i in range(3)] mid3 = [(vertices[2][i] + vertices[0][i]) / 2 for i in range(3)] mid1[2] = get_displacement(mid1) mid2[2] = get_displacement(mid2) mid3[2] = get_displacement(mid3) t1 = [vertices[0], mid1, mid3] t2 = [vertices[1], mid2, mid1] t3 = [vertices[2], mid3, mid2] t4 = [mid1, mid2, mid3] roughness /= 2 midpoint_displacement(t1, depth - 1, roughness, triangles, displacement_cache) midpoint_displacement(t2, depth - 1, roughness, triangles, displacement_cache) midpoint_displacement(t3, depth - 1, roughness, triangles, displacement_cache) midpoint_displacement(t4, depth - 1, roughness, triangles, displacement_cache) return trianglesdef plot_landscape(triangles): fig = plt.figure(figsize=(12, 15)) ax = fig.add_subplot(111, projection='3d') #ax.axis('off')
codefor triangle in triangles: verts = [[(v[0], v[1], v[2]) for v in triangle]] # Формируем вершины poly = Poly3DCollection(verts, alpha=0.6, facecolor='saddlebrown', edgecolor='brown') ax.add_collection3d(poly) ax.set_xlim(-1, 5) ax.set_ylim(-1, 5) ax.set_zlim(-2, 2) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plt.show() - Assistant
- User
import matplotlib.pyplot as plt import numpy as np
def draw_branch(x, y, angle, length, thickness, color, depth, max_depth, angle_variation): if depth > max_depth: return
code - Assistant