python
import numpy as np
import matplotlib.pyplot as plt
# 生成10000个随机点
np.random.seed(42)
n_points = 10000
x = np.random.random(n_points)
y = np.random.random(n_points)
z = np.random.random(n_points)
# 计算映射后的坐标
mapped_x = (2*y + z)/(2*(x + y + z))
mapped_y = (np.sqrt(3)*z)/(2*(x + y + z))
# 创建散点图
plt.figure(figsize=(12, 12))
plt.scatter(mapped_x, mapped_y, alpha=0.1, s=5)
# 绘制等边三角形的边界
triangle_x = [0, 1, 0.5, 0]
triangle_y = [0, 0, np.sqrt(3)/2, 0]
plt.plot(triangle_x, triangle_y, 'r-', linewidth=2, label='三角形边界')
plt.grid(True)
plt.title('单位立方体到等边三角形的映射 (10000点)')
plt.xlabel('x = (2y+z)/(2(x+y+z))')
plt.ylabel('y = (√3*z)/(2(x+y+z))')
plt.axis('equal')
plt.legend()
# 添加参考线
plt.axhline(y=0, color='k', linestyle='--', alpha=0.3)
plt.axvline(x=0, color='k', linestyle='--', alpha=0.3)
plt.show()