how to draw a meshed geometry?

2 次查看(过去 30 天)
Hi
I aim to write a code which draws a geometry and then mesh it! Could you please tell me how can I edit this code so it gives out the attached figure? (please ignore the colorful pattern, I only need a meshed geometry)
% Define the vertices of the triangle
v1 = [0, 0];
v2 = [20, 0];
v3 = [30, -10];
v4 = [50, -10];
v5 = [50, -20];
v6 = [0, -20];
% Create a matrix with the vertices as rows
vertices = [v1; v2; v3; v4; v5; v6];
% Define the faces of the triangle as a 1-by-3 matrix
faces = [1, 2, 3, 4, 5, 6];
% Create a patch object with the specified vertices and faces
patch('Vertices', vertices, 'Faces', faces, 'FaceColor', 'white');
% Set the axis limits and labels
axis([0, 50, -20, 0]);
xlabel('x-axis');
ylabel('y-axis');
hold on
x1=0;
x2=50;
y1=0;
y2=-20;
npoints_x=51;
npoints_y=21;
% Create a grid of points using the meshgrid function
x_grid = linspace(x1, x2, npoints_x);
y_grid = linspace(y1, y2, npoints_y);
hold on
[X, Y] = meshgrid(x_grid, y_grid);
hold on
mesh (X,Y,zeros(size(X)))

采纳的回答

KSSV
KSSV 2023-4-17
clc; clear all ;
% Define the vertices of the triangle
v1 = [0, 0];
v2 = [20, 0];
v3 = [30, -10];
v4 = [50, -10];
v5 = [50, -20];
v6 = [0, -20];
% Create a matrix with the vertices as rows
vertices = [v1; v2; v3; v4; v5; v6; v1];
% % Define the faces of the triangle as a 1-by-3 matrix
% faces = [1, 2, 3, 4, 5, 6];
%
% % Create a patch object with the specified vertices and faces
% patch('Vertices', vertices, 'Faces', faces, 'FaceColor', 'white');
%
% % Set the axis limits and labels
% axis([0, 50, -20, 0]);
% xlabel('x-axis');
% ylabel('y-axis');
%
% hold on
x1=0;
x2=50;
y1=0;
y2=-20;
npoints_x=51;
npoints_y=21;
% Create a grid of points using the meshgrid function
x_grid = linspace(x1, x2, npoints_x);
y_grid = linspace(y1, y2, npoints_y);
[X, Y] = meshgrid(x_grid, y_grid);
Z = rand(size(X)) ;
idx = inpolygon(X,Y,vertices(:,1),vertices(:,2)) ;
Z(~idx) = NaN ;
h = pcolor(X,Y,Z) ;
h.EdgeColor = 'none' ;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by