Clear Filters
Clear Filters

How can I get meshgrid points inside the complex polygon?

10 views (last 30 days)
I have a polygon combined with multiple sub-polygons.
I want to get meshgrid points inside of the polygon.
% cal_polymask
clc
clear
% define computation domain
n_grid = 200;
x_range = 10;
y_range = 10;
x = linspace(-x_range, x_range, n_grid);
y = linspace(-y_range, y_range, n_grid);
[X, Y] = meshgrid(x, y);
% Note: Define polygon vertices in CW direction
xvertice_1 = [-8 -8 8 8]; % line1
yvertice_1 = [6.5 7.5 7.5 6.5];
xvertice_2 = [-8 -8 8 8]; % line2
yvertice_2 = yvertice_1 - 2;
xvertice_3 = [-8 -8 8 8]; % line3
yvertice_3 = yvertice_2 - 2;
xvertice_4 = [-8 -8 8 8]; % line4
yvertice_4 = yvertice_3 - 2;
xvertice_5 = [-8 -8 8 8]; % line5
yvertice_5 = yvertice_4 - 2;
xvertice_6 = [-8 -8 8 8]; % line6
yvertice_6 = yvertice_5 - 2;
xvertice_7 = [-8 -8 8 8]; % line7
yvertice_7 = yvertice_6 - 2;
xvertice_8 = [-8 -8 8 8]; % line8
yvertice_8 = yvertice_7 - 2;
poly1 = polyshape(xvertice_1, yvertice_1);
polyout = addboundary(poly1, {xvertice_2, xvertice_3, xvertice_4, xvertice_5, xvertice_6, xvertice_7, xvertice_8}, ...
{yvertice_2, yvertice_3, yvertice_4, yvertice_5, yvertice_6, yvertice_7, yvertice_8});
figure()
plot(polyout);
axis([-10 10 -10 10])
xlabel('$x (\mu m)$', 'Interpreter','latex');
ylabel('$y (\mu m)$', 'Interpreter','latex');
What should I do to get the meshgrid points [X, Y] and their indice inside of blue-colored area?

Answers (1)

Moksh
Moksh on 13 Sep 2023
Hello Yunhyeok,
I understand that you are plotting a polygon and are now looking to obtain the meshgrid coordinates of the sub-polygons present in it.
To achieve this, you can utilize the "meshgrid" function in MATLAB. This function generates a 2D grid based on the input coordinate vectors "x" and "y". Additionally, you have the flexibility to specify and adjust the limits and spacing between the meshgrid coordinates based on your desired outcome.
Below is an example code for the first sub-polygon:
% Taking the first subpolygon as an example
xvertice_1 = [-8 -8 8 8];
yvertice_1 = [6.5 7.5 7.5 6.5];
% Defining edge points for meshgrid function
xMin = -8;
xMax = 8;
yMin = 6.5;
yMax = 7.5;
% Plotting the rectangle
poly = polyshape(xvertice_1, yvertice_1);
plot(poly)
axis([0 10 0 10])
hold on
% Specifying the spacing between meshgrid coordinates
spacing = 0.3;
% Generating and plotting the meshgrid coordinates
[X, Y] = meshgrid(xMin:spacing:xMax, yMin:spacing:yMax);
n = size(X);
plot(X, Y, ".", "Color", "red")
The above steps can be repeated for each sub-polygon.
Please refer to the below documentation for more information about the “meshgrid” function:
Hope this helps!
Best Regards,
Moksh Aggarwal

Categories

Find more on Elementary Polygons in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!