MATLAB绘制正四面体、正六面体

时间:2024-11-19 15:28:32

MATLAB绘制正四面体、正六面体

clc;close all;clear all;warning off;% clear all
rand('seed', 100);
randn('seed', 100);
format long g;

% 正四面体(Tetrahedron)
% 顶点坐标(正四面体的顶点位于一个正方体的对角线上,并经过适当缩放)
vertices = [
    1 1 1;
    -1 -1 1;
    1 -1 -1;
    -1 1 -1
    ] / sqrt(3); % 缩放以确保边长为1

% 边(由顶点索引组成的对)
edges = [
    1 2;
    1 3;
    1 4;
    2 3;
    2 4;
    3 4  % 这条边是完整的正四面体结构中的一部分,之前可能由于误解而认为缺失
    ];

% 绘制多面体的边
figure;
hold on;
for i = 1:size(edges, 1)
    % 使用循环遍历每条边,并绘制它
    plot3(vertices(edges(i, :), 1), vertices(edges(i, :), 2), vertices(edges(i, :), 3), 'k-');
end

% 绘制顶点(可选,因为边已经连接了顶点)
plot3(vertices(:, 1), vertices(:, 2), vertices(:, 3), 'ro');

% 设置图形属性
axis equal;
grid on;
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Tetrahedron');
view(3); % 设置视角以便更好地查看正四面体
hold off;


% 正六面体(Cube)
% 顶点坐标
vertices = [
    1 1 1; -1 1 1; -1 -1 1; 1 -1 1;
    1 1 -1; -1 1 -1; -1 -1 -1; 1 -1 -1
    ] / sqrt(3);

% 面(由顶点索引组成)
faces = [
    1 2 3 4;
    5 6 7 8;
    1 2 6 5;
    2 3 7 6;
    3 4 8 7;
    4 1 5 8
    ];

% 绘制多面体
figure;
hold on;
for i = 1:size(faces, 1)
    plot3(vertices(faces(i, :), 1), vertices(faces(i, :), 2), vertices(faces(i, :), 3), 'k-');
end
plot3(vertices(:, 1), vertices(:, 2), vertices(:, 3), 'ro'); % 顶点
axis equal;
grid on;
title('Cube');
hold off;