Convex Hull of Lattice Points (from HDU 3285)

Source: Internet
Author: User

There are several changes to the naked convex hull: The base point is the top left point, and the convex hull vertex is output clockwise (the most important point in this question: No output on the convex hull side, output vertices clockwise only) and shaping coordinates

Train of Thought: the key point is to bring up the point on the side of the convex hull, so there are several key steps: when the polar coordinates are sorted, selecting the shortest path for colons takes precedence; top starts from 1, so of course, while top needs to be verified as a positive number.

Here is an example of nine points:

Note: After the vertices in the graph are sorted, I use the lexicographically sorted order.

Here we will focus on the following beginning and end: (suitable for basic beginners, please PASS ~ -) Start: Lima at point B has been shot.

Finally, point C rounds E and D, and then comes to the key gpoint. Note that at this time, point C first finds H instead of J, because we used to select the shortest priority during the update process, although H is pushed to the stack because HG is the right turn of GC, however, it was quickly caused by the turn of IH to the left of HG. I point was immediately caused by the turn of IG to the left of the stack, but it was quickly caused by the turn of JI to the left of the IC, then, determine the relationship between the JG and GC. At this time, the gpoint is also popped up because the JG is collocated with the GC. The final judgment is that JC is the left turn of the AC, which meets the conditions and is pushed to the stack. Now, the loop ends.

So the order of the three HIJ points is: the order of blue, red, and yellow.

Program: // The following program is changed directly from my template, so there will be several changes

 

# Include <iostream>
# Include <stdio. h>
# Include <algorithm>
# Include <string. h>
# Include <math. h>
 
Using namespace std;

Struct point
 
{

Int x, y;
 
};
 
Point p [105], res [105];
 
Double Dist (const point & arg1, const point & arg2)
 
{
 
Return sqrt (double) (arg1.x-arg2.x) * (arg1.x-arg2.x) + (arg1.y-arg2.y) * (arg1.y-arg2.y )));
 
}
 
Int multi (int I, int top, point p2, point p1, point p0) // transfers I and top to facilitate debugging.
 
{

Return (p1.x-Snapshot X) * (p2.y-Snapshot y)-(p2.x-Snapshot X) * (p1.y-Snapshot y );

 
}
 
Int mysort1 (point a, point B)
 
{// If (a. y! = B. y) return a. y <B. y; major modification: Change the lower left to the upper left.
If (a. y! = B. y) return a. y> B. y;
If (a. y = B. y & a. x! = B. x) return a. x <B. x;
 
}
 
Bool cmp (const point & a, const point & B)
 
{
 
Point temp = p [0];
 
Double xmt = (a. x-temp.x) * (B. y-temp.y)-(B. x-temp.x) * (a. y-temp.y );
 
If (xmt)
Return xmt <0;
// Return xmt> 0; major modification: clockwise
// Return Dist (a, temp)> Dist (B, temp); // change the longest sum of vectors to the shortest.
Return Dist (a, temp) <Dist (B, temp );
 
}
 
 
 
Int main ()
 
{Int n, k, len;
 
Cin> k;
 

Int cas;
While (k --)
 
{

Cin> cas> n;
 
For (int I = 0; I <n; I ++)
 
Cin> p [I]. x> p [I]. y;
 
Sort (p, p + n, mysort1); // sort and locate the point in the lower left corner.

Res [0] = p [0];
 
 
Sort (p + 1, p + n, cmp); // sort by Polar Angle

Res [1] = p [1];
 
// Res [2] = p [2]; major modification: Bring out the collinearity point
 
Int top = 1; // major modification: Bring out the collocated points.
 
For (int I = 2; I <n; I ++) // I = 3 major modification: Bring out the collocated point
 
{

While (top & multi (I, top, p [I], res [top], res [top-1])> = 0)
// Major modification: Click left to change to right.
Top --;
 
Res [++ top] = p [I];
// In fact, the above cmp has a very good advantage in selecting a short distance first, because it can be started
// You can bring up all the records that are collocated with the origin.
// The pop-up principle is: every time the source is returned, the stack is stopped, and the next vertex is directly selected to go to the stack.
// Because the current vertex + the preceding vertex + origin is not collocated or left
// You can discard the current point directly, so there are several points that are collocated with the origin at the beginning.
// You will be forced to select this option every time you return the origin point, that is, you will return it once.
// Enter and return once until you find the right point.
// As for the final collinearity, the advantage of selecting a short distance priority is that
// The last point is changed because you turn right.
// Cout <"I p [I]. x p [I]. y "<I <" "<p [I]. x <"" <p [I]. y <endl;
 
}


Cout <cas <''<top + 1 <endl;
For (int I = 0; I <= top; I ++)
 
Cout <res [I]. x <"" <res [I]. y <endl;
 
}

System ("pause ");
 
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.