필터 지우기
필터 지우기

how to draw a meshed geometry?

조회 수: 1 (최근 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 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