Monday, August 18, 2014

Calculate the Area


 Problem Link : http://www.spoj.com/problems/CALCAREA/

 Idea: Given N points and we have to deduce the area of polygon made of given  points. First triangulate the polygon taking 3 points at a time.If the polygon is convex,  the summation of area of those triangles is the answer.Otherwise the answer is  the summation of area of those triangles whose points are convex in respect of the polygon minus summation of area of those triangles whose points are concave in respect of the polygon.
Take a vector of int int pair. Store the co-ordinate of given points in pair and push the pair in vector.Now,assume fixed the first point and make vector from first point to other point.Calculate the cross product taking couple of vector from first to other with the formula-
                  A*B=x1*y2-x2*y1.
So area of the triangle is A*B/2.
and add them.
You oppose that,all triangle are not part of the polygon,that means we should subtruct the area of concave triangles.See more specifically,if the triangle is concave to the polygon,the area given by the formula is positive else it is negative.Our convex area are negative and concave are positive.If we add them,we should get the required area.Of course take its absolute value.

My Solution:  http://pastebin.com/L9rgPBHB

No comments:

Post a Comment