HDU 2202 Max triangle convex hull Template

Source: Internet
Author: User

// The following is my template ;;
// I won't talk about the question. It's a naked convex bag ..
// Note: n values must be assigned first, and the number of points must be greater than two. Place the subscript of the Convex point in sta [] instead of the Convex point in point [].
# Include <iostream>
# Include <algorithm>
# Include <cstdio>
# Include <cmath>
# Define pi acos (-1.0)
Using namespace std;

Typedef double pointper; // coordinate type

# Define POINTNUM 50005 // maximum number of vertices
# Define PREX 1e-11 // used when the coordinate of a vertex is of the real number type

Struct node
{
Pointper x, y;
} Point [2, 10001];

Class Polygon
{
Public:
Int sta [POINTNUM]; // coordinate of the point on the convex hull
Node point [POINTNUM];
Bool flag [POINTNUM];
Int top, n, stab; // n indicates the number of reading points, top-1 indicates the number of points on the convex hull, (0 ~~ Top-2) is the coordinates of the vertices on the convex hull. top-1 and 0 are the first vertices;
Pointper x1, y1, x2, y2;
Polygon ()
{
Top = n = 0;
}
Static bool cmp (const node & A, const node & B)
{
Return A. x <B. x | A. x = B. x & A. y <B. y; // sort by X first, and then by Y.
}
Bool X (int x1, int y1, int x2, int y2, bool f) // if the value of f is true, the vertex on the edge is included.
{
If (f)
Return x1 * y2-x2 * y1> = 0;
Return x1 x y2-x2 * y1> 0;
}
Bool X (double x1, double y1, double x2, double y2, bool f) // if the value is true, the vertex on the edge is included.
{
If (f)
Return x1 * y2-x2 * y1> = 0.0 | fabs (x1 * y2-x2 * y1) <PREX;
Return x1 * y2-x2 * y1> 0.0;
}
Double dis (node a, node B)
{
Return sqrt (double) (a. x-b.x) * (a. x-b.x) + (a. y-b.y) * (a. y-b.y )));
}
Void pointselect (bool f); // calculate the vertex on the convex hull. If f is true, the vertex on the edge is included;
Void getpoint (int I, bool f );
Void XY (int I); // auxiliary X () for cross Multiplication
Double length (); // calculates the perimeter of the convex hull.
Double area (); // calculates the area of the convex hull;
Bool IsInPoly (int x, int y, bool f );
Bool IsInPoly (double x, double y, bool f );
};

Polygon Tubao;

Void Polygon: XY (int I)
{
X1 = point [I]. x-point [sta [top-2]. x;
Y1 = point [I]. y-point [sta [top-2]. y;
X2 = point [sta [top-1]. x-point [sta [top-2]. x;
Y2 = point [sta [top-1]. y-point [sta [top-2]. y;
}

Void Polygon: getpoint (int I, bool f)
{
XY (I );
If (top = stab | X (x1, y1, x2, y2, f ))
{
Sta [top ++] = I;
Flag [I] = false;
}
Else
{
Top --;
Flag [sta [top] = true;
XY (I );
While (top> stab &&! X (x1, y1, x2, y2, f ))
{
Top --;
Flag [sta [top] = true;
XY (I );
}
Sta [top ++] = I;
Flag [I] = false;
}
}
Void Polygon: pointselect (bool f)
{
Int I;
Memset (flag, true, n + 1 );
Sort (point, point + n, cmp );
Sta [0] = 0;
Sta [1] = 1;
Top = 2;
Flag [1] = false;
Stab = 1;
For (I = 2; I <n; I ++)
Getpoint (I, f );
Stab = top;
For (I = n-2; I> = 0; I --)
If (flag [I])
Getpoint (I, f );
}

Double Polygon: length ()
{
Double s = 0.0;
Int I;
For (I = 1; I <top; I ++)
S + = sqrt (1.0 * (point [sta [I]. x-point [sta [I-1]. x) * (point [sta [I]. x-point [sta [I-1]. x) + (point [sta [I]. y-point [sta [I-1]. y) * (point [sta [I]. y-point [sta [I-1]. y ));
Return s;
}
Double Polygon: area ()
{
Double s = 0.0;
Int I;
For (I = 1; I <top; I ++)
S + = point [sta [I-1]. x * point [sta [I]. y-point [sta [I]. x * point [sta [I-1]. y;
Return fabs (s/2 );
}

Bool Polygon: IsInPoly (int x, int y, bool f) // int type
{
Int I;
For (I = 1; I <top; I ++)
If (! X (x-point [sta [I-1]. x, y-point [sta [I-1]. y, (double) point [sta [I]. x-point [sta [I-1]. x, (double) point [sta [I]. y-point [sta [I-1]. y, f ))
Return false;
Return true;
}

Bool Polygon: IsInPoly (double x, double y, bool f) // double Type
{
Int I;
For (I = 1; I <top; I ++)
If (! X (x-point [sta [I-1]. x, y-point [sta [I-1]. y, point [sta [I]. x-point [sta [I-1]. x, point [sta [I]. y-point [sta [I-1]. y, f ))
Return false;
Return true;
}

Int main () // HDU 2202 calculates the maximum triangle ..
{
While (scanf ("% d", & Tubao. n )! = EOF & Tubao. n)
{

For (int I = 0; I <Tubao. n; I ++)
{
Scanf ("% lf", & Tubao. point [I]. x, & Tubao. point [I]. y );
}
Tubao. pointselect (1 );
Double a, B, c, p, f, sum = 0;
Double max = 999999;
For (I = 1; I <Tubao. top; I ++) // enumerate the area of a triangle at each of the three points ..
For (int j = I + 1; j <Tubao. top; j ++)
For (int k = j + 1; k <Tubao. top; k ++)
{
A = Tubao. dis (Tubao. point [Tubao. sta [I], Tubao. point [Tubao. sta [j]);
B = Tubao. dis (Tubao. point [Tubao. sta [I], Tubao. point [Tubao. sta [k]);
C = Tubao. dis (Tubao. point [Tubao. sta [j], Tubao. point [Tubao. sta [k]);
P = (a + B + c)/2.0;
F = sqrt (p * (p-a) * (p-B) * (p-c ));
If (f> sum)
Sum = f;
}
Printf ("%. 2lf \ n", sum );
// For (I = 1; I <Tubao. top; I ++) // Tubao. top stores the number of convex packets ..
// Cout <Tubao. point [Tubao. sta [I]. x <"" <Tubao. point [Tubao. sta [I]. y <endl;
// The above output is the point in the convex bag ..
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.