Implement Matlab function that draws a polygon defined by it
Solution
Answer :
Now we need to loop through all vertices that make up the polygon, initializing the x,y values to create the polygon.
As you saw in the octagon created using this code, the polygon will always be convex and equally spaced, or a regular polygon. In order to generate non-regular polygons that may or not be concave or convex, you can add in some variability to the radius and angle separation measures. We will utilize the rand function to and this variability. Let’s start by initializing two new variables radVar and angVar to specify that we want variance in the radius and angle for each vertex.
And now to calculate the x and y coordinates of each vertex on the polygon you can utilize new equations of x = (r+rVar) * cos(theta + thetaVar), y = (r + rVar) * sin(theta + thetaVar). In MATLAB code here is how these new x,y points will be calculated and plotted.
