Graham's scan Method for Solving Convex Hull Problems

Source: Internet
Author: User
Concept

Convex Hull is a concept in computational ry (graphics. Given a two-dimensional point set on a given two-dimensional plane, a convex bag is a convex multi-border shape formed by connecting the points on the outermost layer. It can contain all vertices in a point set. For detailed definitions and related concepts, see Wikipedia: convex hull.

ThisAlgorithmInvented by the math master Graham, he used to be chairman of the American Mathematical Society (AMS), chief scientist at at&t, and Chairman of the IJA. (It's so sweaty that the Daniel can also play acrobatics ~)

 

Problem

A two-dimensional point set on a given plane is used to solve its convex hull.

 

Process

1. Select h with the smallest y coordinate among all vertices as the base point. If the Y coordinate of multiple vertices is the minimum value, the minimum X coordinate is selected. Points with the same coordinates should be excluded. Then, sort the vectors

 

 

2. The line segment

3. When adding a dot, you must consider whether the preceding line segment will appear on the convex hull. Starting from the base point, the rotation direction of each adjacent line segment on the convex hull should be the same, and the scanning direction is the opposite. If the newly added point changes the rotation direction of the new line segment and the online segment, it can be determined that the last point is not on the convex hull. When the implementation is complete, the vector cross product can be used for judgment. The newly added vertex is Pn + 1, the previous vertex is pn, And the next vertex is pn-1. When clockwise scanning is performed, if the cross product between the vectors <PN-1, PN> and <Pn, Pn + 1> is positive (whether the cross product is negative by clockwise scanning), the previous point is deleted. The deletion process needs to be traced back. All vertices with the opposite cross product symbols are deleted, and the new vertices are added to the convex hull.

 

 

When K points are added, the

 

Complexity

This algorithm can directly perform operations on the original data, so the space complexity is O (1 ). However, if you store the results of the convex hull in another arrayCodeLevel. The time complexity is at least the O (nlgn) of the fast sorting because it needs to be sorted before scanning the convex hull ). The complexity of the subsequent scanning process is O (n), so the complexity of the entire algorithm is O (nlgn ).

 

C ++/STL implementation
# Include <algorithm> # include <iostream> # include <vector> # include <math. h> using namespace STD; // two-dimensional point (or vector) struct definition # ifndef _ windef_struct point {int X; int y ;};# endiftypedef vector <point> ptarray; // determine whether two points (or vectors) are equal bool operator = (const point & pt1, const point & pt2) {return (pt1.x = pt2.x & pt1.y = pt2.y);} // compare the vector and the X axis vector (1, 0) bool comparevector (const point & pt1, const point & pt2) {// evaluate the modulo float M1 of the Vector = SQRT (float) (pt1.x * pt1.x + pt1.y * pt1.y); float m2 = SQRT (float) (pt2.x * pt2.x + pt2.y * pt2.y )); // obtain the Inner Product float V1 = pt1.x/M1, V2 = pt2.x/m2 from (1, 0). // If the vector angle is equal, returns a return value that is closer to the base point and ensures the ordered return (V1> V2 | V1 = v2 & M1 <m2);} // calculates the void calcconvexhull (ptarray & vecsrc) of the convex hull) {// a vertex set must have at least three vertices to form a polygon if (vecsrc. size () <3) {return;} // search for the base point ptbase = vecsrc. front (); // set the 1st points to the minimum point for (ptarray: iterator I = Vecsrc. Begin () + 1; I! = Vecsrc. end (); ++ I) {// If the Y value of the current vertex is smaller than the minimum vertex, or the Y value is equal, the X value is smaller if (I-> Y <ptbase. Y | (I-> Y = ptbase. Y & I-> x> ptbase. x) {// use the current vertex as the minimum vertex ptbase = * I;} // calculates the vector for (ptarray: iterator I = vecsrc. begin (); I! = Vecsrc. end ();) {// exclude vertices that are the same as the base point to avoid the IF (* I = ptbase) {I = vecsrc error in the subsequent sorting calculation. erase (I);} else {// from the base point to the target point I-> X-= ptbase. x, I-> Y-= ptbase. y; ++ I ;}/// sort by the angle between each vector and the abscissa. begin (), vecsrc. end (), & comparevector); // deletes the same vector vecsrc. erase (unique (vecsrc. begin (), vecsrc. end (), vecsrc. end (); // obtain the vector for (ptarray: reverse_iterator rI = vecsrc. rbegin (); Ri! = Vecsrc. rend ()-1; ++ RI) {ptarray: reverse_iterator rinext = RI + 1; // vector Triangle Formula ri-> X-= rinext-> X, ri-> Y-= rinext-> Y;} // Delete the for (ptarray: iterator I = vecsrc. begin () + 1; I! = Vecsrc. end (); ++ I) {// trace back to delete the vector with the opposite rotation direction, and use the outer product to determine the rotation direction for (ptarray: iterator ilast = I-1; ilast! = Vecsrc. begin ();) {int V1 = I-> X * ilast-> Y, V2 = I-> y * ilast-> X; // If the cross product is less than 0, there is no reverse rotation. // If the cross product is equal to 0, you also need to determine whether the direction is opposite if (V1 <V2 | (V1 = v2 & I-> X * ilast-> x> 0 & I-> y * ilast-> Y> 0 )) {break;} // After deleting the previous vector, update the current vector and connect it to the beginning and end of the previous vector. // vector Triangle Formula I-> X + = ilast-> X, i-> Y + = ilast-> Y; ilast = (I = vecsrc. erase (ilast)-1 ;}// accumulate all the vectors connected to the beginning and end in sequence and convert them to coordinate vecsrc. front (). X + = ptbase. x, vecsrc. front (). Y + = ptbase. y; For (ptarray: iterat Or I = vecsrc. Begin () + 1; I! = Vecsrc. end (); ++ I) {I-> X + = (I-1)-> X, I-> Y + = (I-1)-> Y ;} // Add the base point. All convex packets are computed to complete vecsrc. push_back (ptbase);} int main (void) {int nptcnt = 100; // generated random points ptarray vecsrc, vecch; For (INT I = 0; I <nptcnt; ++ I) {point ptin = {rand () % 20, Rand () % 20}; vecsrc. push_back (ptin); cout <ptin. x <"," <ptin. Y <Endl;} calcconvexhull (vecsrc); cout <"\ nconvex hull: \ n"; for (ptarray: iterator I = vecsrc. begin (); I! = Vecsrc. End (); ++ I) {cout <I-> x <"," <I-> Y <Endl;} 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.