Some idea to plot polygons
Polygon, in geometry, is any closed curve consisting of a
set of line segments (sides) connected such that no two segments cross. The simplest polygons
are triangles (three sides), quadrilaterals (four sides), and pentagons (five
sides).
Here is how you can enter polygon data in
Matlab.
Matlab obviously have a command to plot a polygon. The
purpose of this coding experience is to practice basic approaches to work with geometric
data.
clear all
close all
Polyg=[1 -1;
%vertex 1
7 -1; %vertex
2
7 5; %vertex
3
4 5; %vertex
4
1 -1]; %repeating vertex 1
xlim([0 8])
ylim([-3 7])
hold on
m=length(Polyg)-1; %getting the number of segments
%Now lets draw the polygon
for i=1:m
plot(Polyg(i,1), Polyg(i,2),'o');
hold on
for
t=0:0.01:1
x_segment=(1-t)*Polyg(i,1)+t*Polyg(i+1,1);
y_segment=(1-t)*Polyg(i,2)+t*Polyg(i+1,2);
plot(x_segment,y_segment,'.r');
end
end
In what follows as a case demonstation we are going to ask
a user to click on the screen in counterclockwise fashon to plot a pentegon.
%we will save the polygon data in an
array (first column is the x and second column is the y)
figure
plot(0,0);
axis([0 10 0 10]);
title('Enter the
vertices of the pentagon you want to plot in counter clockwise fashion')
hold on
clear all
for i=1:5
Poly(i,:)=ginput(1);
end
Poly(6,:)=Poly(1,:);
%Now lets draw the polygon
for i=1:5
plot(Poly(i,1), Poly(i,2),'o');
hold on
for
t=0:0.01:1
x_segment=(1-t)*Poly(i,1)+t*Poly(i+1,1);
y_segment=(1-t)*Poly(i,2)+t*Poly(i+1,2);
plot(x_segment,y_segment,'.r');
end
end
Can you reason why we added the first vertex as the last
element in the Poly array that keeps the vertices coordinates?
Check how we ploted the line segements, conistituting the
edges of the Polygon.
Appendix
Remember that the equation of the line segment between two
points with coordinates
and
is given by
You can also plot a polygon using Matlab's build-in
function `polyshape'. For more information see https://www.mathworks.com/help/matlab/ref/polyshape.html
Also for your information