Convex Hull problem -- Graham Scanning Method

Source: Internet
Author: User

Convex bag problem-Graham scanning method:

(1) Find the leftmost Point P1 in Point Set P [], connect other points in the same point set P1 with line segments, and calculate the angle between these line segments and the horizontal line, then sort by angle from small to large and by the distance to P1 from near to far (angle range is [0,180) degrees, and can delete points with the same angle and close to P1, keep the farthest point, this reduces the amount of computing. Because the non-endpoint on the straight line is not the pole of the convex hull, that is, if P1, P2, and P3 are on a straight line, only the convex points P1 and P3 are obtained. P2 is not at the endpoint, so it can be removed) to obtain the new node sequence P1, P2 ,... pn. connect these points in sequence to get a polygon (the point has been made counterclockwise, but must be removed from the convex hull ). At this time, P1 is the starting point of the convex hull boundary, P2 and Pn are also the vertices of the final convex hull, P [n + 1] = p1 (as a loop)

(2) Delete a vertex that is not on the convex hull in P3, P4,... P [n-1:

First, put P1, P2, and P3 into Stack s, and then cyclically (I = 3-> N-1 ), if the two points at the top of the stack and the current point P [I] are skewed clockwise, it indicates that the link is concave and should be deleted, the top element of the stack goes out of the stack (the previous element may be concave and the stack needs to be output again, for example ), the current point P [I] is added to the stack until it is deflected to a counter-clockwise direction or there are only two elements in the stack (P1P2.

The elements in the final Stack are the points on the final convex hull.

Analysis: Generally, the next convex packet point Pa is obtained from the lowest point P1 in the left, and then the next convex packet point Pb is obtained based on the minimum slope of all points in the PA, this is a triplicate loop (P1, Pa, Pb are one, P1, Pa, and Pb are sorted twice in a total of three cycles ), this is not efficient (in fact, this is the volume package method, the complexity is O (NH), where N is all points
H is the number of points on the convex hull ). The Graham scan method only obtains the slope of all vertices P1. The following vertices make full use of the information of the slope and perform some processing to Improve the efficiency. This is a method from many to few, from multiple to one, and fully utilizes known conditions.

The following figure also illustrates the stack scan process (from http://www.cnblogs.com/Booble/archive/2011/03/10/1980089.html#2065991 ):

----------------

---------------

--------------------

The following code is taken from ACM program design training tutorial Wu Hao China Railway Publishing House:

―――――――――――

Reposted on the Internet as follows:

For a set of Q with three or more vertices, the process of Graham scanning is as follows:

Make P0 the smallest point in the order of Y-X coordinates in Q
Set <P1, P2 ,... PM> This is the point set that is obtained by sorting the rest of the points alphabetically by the Polar Angle centered on P0 (if multiple points have the same polar angle, all except the Farthest Points from P0 are removed
Press P0 into Stack s
Press P1 into Stack s
Press P2 into Stack s
For I need 3 to m
Do While consists of the next element of S's stack top element, S's stack top element, and PI line segment without turning to the left
Stack s
Press PI into Stack s
Return S;
If the perimeter is calculated. Add an edge to zoj1453
If the calculated area is used. A triangle composed of three points .. Maximum zoj2419 found

# Include <stdio. h>
# Include <stdlib. h>
# Include <math. h>
# Define Max node 100015
Int stack [maxnode];
Int top;
Typedef struct point
{
Int X;
Int y;
} Point;
Point point [maxnode];
Void swap (point [], int I, Int J)
{
Point TMP;
TMP = point [I];
Point [I] = point [J];
Point [J] = TMP;
}
Double Multi (point P1, point P2, point P0) // cross Multiplication
{
Return (p1.x-Snapshot X) * (p2.y-Snapshot y)-(p1.y-Snapshot y) * (p2.x-Snapshot X ));
}
Double distence (point P1, point P2) // The distance between P1 and P2
{
Return SQRT (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y ));
}
Int CMP (const void * a, const void * B)
{
Point * c = (point *);
Point * D = (point *) B;
Double K = multi (* C, * D, point [0]);
If (k <0) return 1;
Else if (k = 0 & distence (* C, point [0])> = distence (* D, point [0]) return 1; // same polar angle, Distance Ratio
Else return-1;
}
Void grahamscan (int n)
{
Int I, U;
U = 0;
For (I = 1; I <= n-1; I ++) // locate the leftmost vertex P0
If (point [I]. Y <point [u]. y) | (point [I]. y = point [u]. Y & point [I]. x <point [u]. x ))
U = I;
Swap (point, 0, U );
Qsort (point + 1, n-1, sizeof (point [0]), CMP); // sort by Polar Angle
For (I = 0; I <= 2; I ++) stack [I] = I; // P0, p1, p2
Top = 2;
For (I = 3; I <= n-1; I ++) // The numbers of vertices in the final convex hull are stored in stack [] sequentially.
{
While (multi (point [I], point [stack [Top], point [stack [Top-1])> = 0) // a non-left-turn point is displayed.
{
If (Top = 0) break;
Top --;
}
Top ++;
Stack [Top] = I;
}
}
// Calculate the Convex Hull area
Double polygonarea (int n, point P [])
{
Double area;
Int I;
Area = 0;
For (I = 1; I <= N; I ++ ){
Area + = (P [stack [I-1]. x * P [stack [I % N]. y-P [stack [I % N]. x * P [stack [I-1]. Y );
}
Return FABS (area)/2;
}

Int main ()
{
Int I;
For (I = 0; I <10; I ++)
Scanf ("% d", & (point [I]. X), & (point [I]. y ));
Grahamscan (10 );
Printf ("% lf \ n", polygonarea (10, point ));
}

 

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.