Filter löschen
Filter löschen

How to use MATLAB to simulate and obtain farmland plots?

1 Ansicht (letzte 30 Tage)
marvellous
marvellous am 16 Jan. 2024
Kommentiert: William Rose am 17 Jan. 2024
I want to use MATLAB to simulate and obtain farmland plots, several coordinates around the farmland plots which can determine the shape of the farmland are also required. What can I do? I'm writing to ask a favor, thank you!
  2 Kommentare
William Rose
William Rose am 16 Jan. 2024
@marvellous, Please provide more information about your problem. If you haven't done the Matlab Onramp, please do. It includes a section on plotting. Good luck.
marvellous
marvellous am 17 Jan. 2024
@William Rose, I'm so sorry I didn't make myself clear. What I really want is to use MATLAB to randomly generate a farmland plot, better with specified size(e.g., an area of 100), the farmland plot can be of any polygon shape(e.g., rectangles, concave hexagons, irregular polygons), and finally, I can obtain the coordinates for each corner of the farmland plot.
Can I use MATLAB to do this? Or should I use some module inside MATLAB like SIMULINK? I have no idea about this. Could you please give me a hint? Thank you.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

William Rose
William Rose am 17 Jan. 2024
Bearbeitet: William Rose am 17 Jan. 2024
@marvellous, Yes you can.
You'll need to specify something about the shape, for example, the number of vertices.
I'm sure there are a million ways to do it . Here's one. Choose N vertices at random in the unit square. Connect them to make a boundary. Most of the time, we will get a border that self-intersects if we do this. If the polygon self-intersects, try again, and again, until you find one that does not self-intersect.
Once you have a shape, find its area with polyarea(), then rescale the points to make the area=100.
Demonstrate by making four polygons with 4 vertices each.
M=4; % number of polys
N=4; % number of vertices
figure;
warning off
for i=1:M
x=rand(N,1); % x-coordinates of the vertices
y=rand(N,1); % y-coordinates of the vertices
pgon=polyshape([x,y]); % create a polygon with vertices x,y
a=polyarea(x,y); % area of pgon
x1=x*sqrt(100/a); % scale coordinates so the area will=100
y1=y*sqrt(100/a); % scale coordinates so the area will=100
pgon1=polyshape([x1,y1]); % create polygon with vertices x1,y1
a1=polyarea(x1,y1); % area of pgon1
fprintf('i=%d: polygon area=%.1f.\n',i,a1); % display pgon1 area
subplot(2,2,i);
if pgon.NumRegions>1
plot(pgon1,'FaceColor','r','EdgeColor','r')
else
plot(pgon1,'FaceColor','g','EdgeColor','g')
end
end
i=1: polygon area=100.0. i=2: polygon area=100.0. i=3: polygon area=100.0. i=4: polygon area=100.0.
The code above makes four polygons. It finds the area of each polygon, then makes a scaled polygon with area=100. The code plots the scaled polygons. If the number of regions is >1, then the polygon is self-intersecting, and it is plotted in red. Otherwise, it is not self-intersecting, and it is plotted in green.
With N=4 vertices, about 50% of polygons generated as above are self-intersecting. With N=5, about 71% are self intersecting; with N=6, 85% are self-intersecting.
The next bit of code finds a single non-self-intersecting polygon with 6 vertices and area=100. The while loop runs as long as NumRegions exceeds 1. In other words, the while loop stops running after a non-self-intersecting polygon is found. I make a self-intersecting poly before the while loop, in order to force the while loop to run at least once.
N=6; % number of vertices
warning off; % turn off warning about self-intersecting polys
pgon=polyshape([1,0;-1,0;0,1;0,-1]); % self-intersecting poly
while pgon.NumRegions>1 % loop until NumRegions=1
x=rand(N,1); % x-coordinates of vertices
y=rand(N,1); % y-coordinates of vertices
pgon=polyshape([x,y]); % create polygon with vertices x,y
end
warning on; % turn warnings on again
a=polyarea(x,y); % area of pgon
x1=x*sqrt(100/a); % scale coordinates so area(pgon1)=100
y1=y*sqrt(100/a); % scale coordinates so area(pgon1)=100
pgon1=polyshape([x1,y1]); % create pgon1, with vertices x1,y1
figure;
plot(pgon1,'FaceColor','g','EdgeColor','g');
axis equal; grid on; title('Area=100')
I used "axis equal" in the code above, to make 1 unit on the x-axis appear as long as 1 unit on the y-axis. This ensures a realistic aspect ratio when plotting polygons.
  2 Kommentare
marvellous
marvellous am 17 Jan. 2024
Seems very easy, I didn't know the functions polyarea() and pgon.NumRegions. I guess I need to read more information about the use of MATLAB. Thank you very much for your help.
William Rose
William Rose am 17 Jan. 2024
@marvellous, you're welcome. Good luck with your work.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Hassaan
Hassaan am 16 Jan. 2024
Bearbeitet: Hassaan am 16 Jan. 2024
  1. Define Coordinates: Define the coordinates for each corner of the farmland plots. These coordinates will determine the shape and size of each plot.
  2. Create Plots: Use the coordinates to create the plots. This can be done by connecting the dots in a specific order to form a shape (e.g., rectangles, irregular polygons).
  3. Visualization: Use MATLAB's plotting functions like plot, fill, or patch to visualize the farmland plots.
  4. Additional Features: You can add more features like different colors for different plots, labels, grid lines, etc., to enhance the visualization.
% Example: Define coordinates for 3 farmland plots
% Each row in the matrix represents a plot with coordinates [x1, y1; x2, y2; ...; xn, yn]
plots = {
[1, 1; 4, 1; 4, 3; 1, 3]; % Plot 1 (Rectangle)
[5, 1; 7, 2; 6, 4; 4, 3]; % Plot 2 (Irregular shape)
[8, 1; 10, 1; 10, 3; 8, 4] % Plot 3 (Trapezoid)
};
% Visualization
figure;
hold on;
colors = ['r', 'g', 'b']; % Colors for each plot
for i = 1:length(plots)
plotCoordinates = plots{i};
fill(plotCoordinates(:,1), plotCoordinates(:,2), colors(i));
% Label the plots
text(mean(plotCoordinates(:,1)), mean(plotCoordinates(:,2)), ['Plot ' num2str(i)], ...
'HorizontalAlignment', 'center');
end
hold off;
xlabel('X Coordinate');
ylabel('Y Coordinate');
title('Farmland Plots');
axis equal;
grid on;
  • Each farmland plot is defined by a set of coordinates in a 2D plane.
  • The fill function is used to create colored shapes for each plot.
  • Labels are added to each plot for identification.
You can modify the plots variable to represent your specific farmland shapes and sizes. This is a basic framework, and you can expand it further based on your specific needs, such as adding more plots, customizing colors, or even adding functionality to calculate the area of each plot.
Reference Course:
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  1 Kommentar
marvellous
marvellous am 17 Jan. 2024
Thank you for your answer, but I'm afraid I didn't make myself clear, sorry for that.
What I really want is to use MATLAB to randomly generate a farmland plot(not to plot one), better with specified size(e.g., an area of 100), the farmland plot can be of any polygon shape(e.g., rectangles, concave hexagons, irregular polygons), and finally, I can obtain the coordinates for each corner of the farmland plot.
Can I use MATLAB to do this? Or should I use some module inside MATLAB like SIMULINK? I have no idea about this. Could you please give me a hint? Thank you.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Elementary Polygons finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by