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.

This algorithm was invented by Graham, a mathematical master. He used to be the president of the American Mathematical Society (AMS), at&t's chief scientist, and IJA chairman. (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 implemented, the vector cross product can be used to determine and set the newly added vertex to P.N
+ 1. The last point is P.N. The last point is P.N
-1. If the vector <pN
-1, PN> and <pN,
PN
+ 1> If the cross product of is positive (check whether it is negative by counterclockwise scan), delete the previous one. 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 the results of the convex hull are stored in another array, optimization may be performed at the code level. 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 ).

**************************************** **************************************** **************************************** **************************************** ************************

Take zoj1453 as an example of http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 453

# Include <iostream> # include <vector> # include <map> # include <stack> # include <algorithm> # include <queue> # include <list> # include <set> # include <string. h> # include <stdlib. h> # include <math. h> # include <stdio. h> # include <ctype. h> # include <iomanip> using namespace STD; # define ll long # define PI ACOs (-1) # define n 110 # define INF 9999999999 # define EPS 1e-6 // ***************** Note: This is a counter-clockwise vertex! * ************ // Struct point {Double X, Y;}; point P [N], chull [N], P0, stck [N]; int m; // Number of convex hull vertices int N; // All Points // (B, A) x (C, A) double cross (point, point B, point C) {return (B. x-a.x) * (C. y-a.y)-(B. y-a.y) * (C. x-a.x);} // <0 then Ac in AB clockwise, need clockwise turn double DIS (point a, point B) {return SQRT (. x-b.x) * (. x-b.x) +. y-b.y) * (. y-b.y);} // sorting bool CMP (point a, point B) {double T = cross (P0, A, B ); return T> 0 | (t = 0 && DIS (P0, A) <DIS (P0, B); // T> 0 that is, p0b in p0a counterclockwise} void convexhull () {int I, J, K; m = 0; If (n <3) {for (I = 0; I <n; I ++) stck [I] = P [I]; M = N; return;} For (k = 0, I = 0; I <n; I ++) if (P [I]. Y <p [K]. Y | (P [I]. y = P [K]. Y & P [I]. x <p [K]. x) k = I; p0 = P [k]; // base point P [k] = P [0]; P [0] = P0; sort (p + 1, P + N, CMP); stck [0] = P [0]; stck [1] = P [1]; int Top = 1; for (I = 2; I <n; I ++) {While (top> 1 & cross (stck [Top-1], stck [Top], p [I]) <EPS) {top --;} stck [++ top] = P [I];}/* is also equivalent to I = 2; while (I <n) {Double K = cross (stck [Top-1], stck [Top], P [I]); If (k <0 & top> 1) Top --; else {stck [++ top] = P [I ++] ;}} */M = Top + 1;} void solve () {double ans = 0.0; int I; for (I = 0; I <m-1; I ++) ans + = DIS (stck [I], stck [I + 1]); ans + = DIS (stck [0], stck [M-1]); printf ("%. 2f \ n ", ANS);} int main () {// freopen (" a.txt "," r ", stdin); While (scanf (" % d ", & N) {int I; for (I = 0; I <n; I ++) scanf ("% lf", & P [I]. X, & P [I]. y); If (n = 1) {printf ("0.00 \ n"); continue;} If (n = 2) {printf ("%. 2f \ n ", 2 * Dis (P [0], p [1]); // twice !!!! Continue;} convexhull (); solve ();} 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.