how to draw a meshed geometry?

2 ビュー (過去 30 日間)
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor 2023 年 4 月 17 日
回答済み: KSSV 2023 年 4 月 17 日
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 Center および File ExchangeSurface and Mesh Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by