I've been given an assignment:
Create a console application (using C and Obj C) that will calculate area of a random polygon. The application should process input data as a .txt file with a list of Cartesian coordinates pairs in the following format:
(12;34) (23;35) (85;23) ...
From those pairs a polygon should be formed. Sides of the polygon (line segments) should not intersect. If they do - return an error. The application should divide the polygon into triangles, calculate their area, sum and then return the area of the polygon. Input processor, triangles and the sum operation - all should be implemented as a separate classes. The application should support command "help" and return info on how to use the app.
So far I've build the following algorithm. It lacks some features (that I need help with) and have several programmatical mistakes.
My questions are:
- How to check that the sides of a polygon (line segments) - don't intersect? Each line segment should be checked to another. At which point of the algorithm this checking should take place?
- If the polygon has concave sides, my algorithm of building triangles out of the first point fails. What would be the solution for that? (the app should work for the concave/complex polygons too)
- The app processes the entry data. Form 2 NSMutableArrays one for X, the other for Y coordinates.
- Count number of values in any array. If less then 3 - display an error "Entry at least 3 pairs to form a polygon".
- Create class "Corner". Properties of it will be coordinates X and Y. An object of this class will be one corner of our polygon.
- Create a
forloop that should be repeated less than number of values in one of our NSMutableArrays.
- At the start of the loop, we create one object from our "Corner" class. We take out from our NSMutableArrays coordinates and give it as properties of our "corner" object. This way, at the end of the loop cycle we will have all corners/vertexes created.
- Every created
Cornerwe will be adding to another array.
- If the for loop already made at least 3 cycles - we already have 3 vertexes from which we can build the first triangle(from item 10 will learn why we care about triangles)
- Therefore, we create a new class
Triangle. We give it properties:
"side1", "side2", "side3", "area".
- Back to our "for" loop. We create an instance of the
Because we will be calculating area of our ultimate polygon, we can split it on triangles, calculate their area and then add them up. We will be drawing triangles from the first vertex: 1v->2v->3v; 1v->3v->4v; 1v->4v->5v... Because we know coordinate points - we can calculate length between points. Therefore, we know length of all sides of a triangle. To find an area of such triangle we can use Heron's formula (more about it in item 13). To calculate length between points we will create a new method inside the
sideLengthWithVert:corner1 vert2:corner2 vert3:corner3. That method takes corners/vertexes of the triangle - objects of "Corner" class and one after another calculates length between corners using formula: d=sqrt((x2-x1)^2+(y2-y1)^2). The length will be assigned to properties of a "Triangle" object -
side1 side2 side3
(here I have an error in my code, I suspect it is a logical mistake) Since this method takes
cornersas arguments - we create those 3 corners out of our previously created arrays of
corners. An error says:
[__NSArrayM objectAtIndex:]:index 567 beyond bounds [0 .. 2]Where is index 567 comes from - I have no clue. In the method of angle creation I pass variable
tthat equals to number of cycles of the
- We use newly created method:
sideLengthWithVert:corner1 vert2:corner2 vert3:corner3and give it our corners. Now our object
side1 side2 side3equaled length between corners.
- Now we have a complete triangle we can calculate its area.
For that we will write a new method inside class
calcArea. Inside it, we use Heron's formula: p=(a+b+c)/2 then A=sqt(p(p-a)(p-b)(p-c)) Once calculated, we give that to
- We create a new NSMutableArray and add there our triangles that will be created with each cycle of our big
- All what is last is to add up areas of triangles inside that last array. For that we create a condition "if we have at least one
Triangleobject created - perform following code: (inside next items 16, 17, 18) "
- Calculate number of triangles inside the array. Create a cycle that will loop less or equal number of triangles inside the array. With each step add up triangle's areas.
- If user enters
calcin console - show the result of our computations.
- If a user enters
helpin the console - show examples of usage of the application.