Write a program that prompts the user to enter the points of a convex polygon an
ID: 3548558 • Letter: W
Question
Write a program that prompts the user to enter the points of a convex polygon and display its area. ASuume that the polygon has six end points and the points are entered clockwise. For the definition of a convex polygon, see www.mathopenref.com/polygonconvex.html. Hint: the total area of a polygon is the sum of the areas of the small triangles.
Write a function called areaPolygon that solves the problem.
(2) Write a main function that:
? enters the coordinates of the points of a polygon;
? calls the areaPolygon function;
? outputs the area of the polygon.
Important note: No global variables and arrays should be declared.
(3) Test your program with appropriate input data (at least three set of input data)
Explanation / Answer
function [xm ym] = midpt(x,y)
% purpose: calculate midpoints of a list of points
% input: x, y - lists of x and y coordinates
% output: xm, ym - lists of x and y coordinates of the midpoints of the input points
% method: Loop through the input points. For each adjacent set of input points
% calculate the midpoint and store it in the results.
% no input checking done to keep this example short
nm = length(x) - 1; % number of midpoints we'll have
for i=1:nm % loop through sets of points to create midpoints
xm(i)=mid1( x(i),x(i+1) );
ym(i)=mid1( y(i),y(i+1) );
end
return
function m=mid1( v1, v2)
% purpose: determine the value that is halfway between v1 and v2
% input: v1,v2
% output: m - midpoint value
% method: the midpoint is the average of the two values
m=(v1+v2)/2;
return