Convex Hull (Graham scanning)

Source: Internet
Author: User
/** Convex hull problem-Graham scanning method: Find the lowest vertex in Point Set P [] (with multiple vertices on the left), and use the vertex as the pole, obtain the polar angles of all other points. Obviously, the polar angle range is [0,180) degrees, and these points are sorted in ascending order of the polar angles, that is to say, we sort them in descending order by the cotangent value of the polar angle, first putting the first vertex and the second vertex after the pole and sorting into the stack, in another loop (I = 3-> N-1), if the two points at the top of the stack and the current point P [I] are in a clockwise direction, then, the top element of the stack goes out of the stack until it is redirected to a counter-clockwise direction or there are only two elements in the stack. Then, the current point P [I] is put into the stack. The element in the final stack is the point on the required convex hull, which refers to the extreme of the convex hull (the non-endpoint on the straight line is not the pole of the convex hull) **/struct point {Double X, y, dx, Dy; point (): x (0), y (0), dx (0), Dy (0) {} void setdxdy (INT x0, int y0) {dx = x-x0; DY = Y-y0;}/* set the current vertex to P. If the vector <p, P1> to the vector <P1, p2> returns true if the rotation angle is counter-clockwise or the angle is 0. The vector cross product is used to determine. If the cross product of a vector is greater than 0, true is returned. */bool mult (point P1, point P2) {return (p1.x-x) * (p2.y-p1.y)> (p2.x-p1.x) * (p1.y-y );} // a small value with a large cotangent value. If the cotangent value is equal, a small bool operator near the pole <(const point & P) const {return (dx * P. dy> dy * P. DX | (dx * P. DY = Dy * P. DX & dy <p. dy) ;}};/* Point Set P [0... n-1], find the pole on the convex hull (the non-endpoint on the straight line is not the pole) stored in res [], the function returns the number of midpoint of the convex hull */INT Graham (point P [], int N, point res []) {// first locate the bottom point (take the left when there are multiple points) and put it at the position of P [0] int K = 0; For (INT I = 1; I <n; I ++) {If (P [I]. Y <p [K]. Y | (P [I]. y = P [K]. Y & P [I]. x <p [I]. y) k = I;} If (k> 0) {point T = P [0]; P [0] = P [k]; P [k] = T;} // calculate the Dx and dy of each vertex relative to P [0] and calculate the cotangent value of the Polar Angle for (INT I = 0; I <n; I ++) {P [I]. setdxdy (P [0]. x, p [0]. y);} // sort by Polar Angle in ascending order. In order to sort (p + 1, P + n) in descending order of the polar cotangent value ); // first put P [0, 1, 2] into the stack if (n = 0) return 0; Res [0] = P [0]; If (n = 1) return 1; Res [1] = P [1]; if (N = 2) return 2; Res [2] = P [2]; int Top = 2; // perform inbound and outbound operations in sequence for (INT I = 3; I <n; I ++) {While (top> = 1 &&! Res [Top-1]. mult (RES [Top], p [I]) Top --; Res [++ top] = P [I];} return top + 1 ;}

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.