How to use MATLAB to simulate and obtain farmland plots?
4 views (last 30 days)
Show older comments
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 Comments
William Rose
on 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.
Accepted Answer
William Rose
on 17 Jan 2024
Edited: William Rose
on 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
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 Comments
More Answers (1)
Hassaan
on 16 Jan 2024
Edited: Hassaan
on 16 Jan 2024
- Define Coordinates: Define the coordinates for each corner of the farmland plots. These coordinates will determine the shape and size of each plot.
- 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).
- Visualization: Use MATLAB's plotting functions like plot, fill, or patch to visualize the farmland plots.
- 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.
See Also
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!