Question: Area of a Convex Polygon This problem is a modification of Gilat 7 . 1 6 and 7 . 1 7 . The area of

Area of a Convex Polygon
This problem is a modification of Gilat 7.16 and 7.17.
The area of a convex polygon can be found by dividing the
polygon into triangles and summing the areas of the triangles.
The area of each triangle can be found by a cross product. For
example, for triangle ABC in the figure, its area is:
Area =0.5**|ABAC|
Assume that a file exists giving the coordinates of the points of
the polygon. There are two columns, with the first column
being the x coordinate and the second column the y coordinate.
The points are given in the order that they are connected to form the polygon (i.e., coordinates of A in
the first row, B in the second row, and so on).
Write a Matlab program to read the input file, calculate the area and write the area into an output file.
Prompt for the filenames.
Here is a sample input file. The columns have one or more spaces between them. There may be more
or fewer lines than shown here:
For the data above, the output file is just one number. The format is a field width of 12, with 6 digits
after the decimal:
116.500000
You must use fscanf to read the input and fprintf to write the output. You may not use a loop, as we
have not yet covered loops and they are also slow. Below are some suggestions on doing this using
Chapter 2 material.
Read the data into a matrix:
data =
11
72
105
Page 2 of 3
911
612
112
-38
-44
Subtract the first row from all the other rows to form:
vecs =
61
94
810
511
011
-47
-53
Add a 3rd column of zeros:
vecs =
610
940
8100
5110
0110
-470
-530
Tranpose vecs:
vecs =
69850-4-5
1410111173
0000000
Now create a matrix, first, that contains all the columns except for the last:
first =
69850-4
141011117
000000
Page 3 of 3
Create a matrix called second that contains all the columns of vec except for the first:
second =
9850-4-5
410111173
000000
Now take the cross product of first with second, then take the absolute value and divide by 2:
areas =
000000
000000
7.500029.000019.000027.500022.000011.5000
The 3rd row of matrix areas contains the areas of the triangles. Now just add them up and write the
output file.
One problem youll face is that you dont know how many vertices will be in the input file. There are a
couple of solutions:
1) Use the size function.
2) Use end. This is not in your textbook, but here is the relevant section from the Matlab help.
You can also find examples on end by typing doc end.
end can also serve as the last index in an indexing expression. In
that context, end = SIZE(X,k) when used as part of the k-th index.
Examples of this use are, X(3:end) and X(1,1:2:end-1). When using end
to grow an array, as in X(end+1)=5, make sure X exists first.
 Area of a Convex Polygon This problem is a modification of

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!