This question uses two computational Geometric algorithms. Solve convex hull and simple polygon area. For more information about convex hull algorithms, see Introduction to algorithms. The polygon area is divided into triangles.
Shape. It is generally assumed that two consecutive points on the polygon are combined with the origin point in order to form a triangle. Then, use the cross product to calculate the sum of the directed area and take the absolute value. Note:
Points must be given clockwise or clockwise on the polygon, And the convex hull just gives such a series of points.
The convex hull code first finds a vertex with the smallest y coordinate, and then sorts all the remaining vertices by the polar angle. Then, perform a two-dimensional loop on the sorted points. Two-dimensional loops are interpreted
When a new vertex is added to the convex hull set, if it is different from the deflection direction formed by the previously added vertex, the preceding vertices need to pop up the set.
The Code is as follows:
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
# Include <math. h>
Using namespace std;
# Define MAX_N (10000 + 10)
Struct Point
{
Double x, y;
Bool operator <(const Point & p) const
{
Return y <p. y | y = p. y & x <p. x;
}
};
Point pts [MAX_N];
Int nN;
Point ans [MAX_N];
Int nM;
Double Det (double fX1, double fY1, double fX2, double fY2)
{
Return fX1 * fY2-fX2 * fY1;
}
Double Cross (Point a, Point B, Point c)
{
Return Det (B. x-a. x, B. y-a. y, c. x-a. x, c. y-a. y );
}
Bool CrossCmp (const Point & a, const Point & B)
{
Double cs = Cross (pts [0], a, B );
Return cs> 0 | cs = 0 & a. x <B. x;
}
Void Scan ()
{
NM = 0;
Sort (pts + 1, pts + nN, CrossCmp); // sorts all vertices by the Polar Angle and deflection counterclockwise.
// Point 0-2 does not actually enter the second cycle
// Check the deflection direction of the edges formed with the front points in the convex hull from the first 3rd points.
// If it is not counterclockwise deflection, the point is displayed.
For (int I = 0; I <nN; ++ I)
{
While (nM> = 2 & Cross (ans [nM-2], ans [nM-1], pts [I]) <= 0)
{
NM --;
} Www.2cto.com
Ans [nM ++] = pts [I];
}
}
Double GetArea ()
{
Double fAns = 0.0;
Point ori = {0.0, 0.0 };
For (int I = 0; I <nM; ++ I)
{
FAns + = Cross (ori, ans [I], ans [(I + 1) % nM]);
}
Returned fabs (fAns) * 0.5;
}
Int main ()
{
While (scanf ("% d", & nN) = 1)
{
For (int I = 0; I <nN; ++ I)
{
Scanf ("% lf", & pts [I]. x, & pts [I]. y );
If (pts [I] <pts [0])
{
Swap (pts [I], pts [0]); // pts [0] is the smallest point of y coordinate
}
}
Scan (); // Scan the convex hull
Double fArea = GetArea ();
Printf ("% d \ n", (int) (fArea/50 ));
}
Return 0;
}