Two-dimensional convex hull Template

Source: Internet
Author: User
# Include <stdio. h> # Include <Math. h> # Include <Algorithm> # Include <Iostream> Using   Namespace  STD;  Const   Int Maxn = 1000  ;  Struct  Point {  Int  X, Y ;}; point list [maxn]; Int  Stack [maxn], top;  Int Cross (point P0, Point P1, point P2) //  Calculate the cross product p0p1 x p0p2  {  Return (P1.x-Snapshot X) * (p2.y-Snapshot y)-(p1.y-Snapshot y) * (p2.x- Optional X );}  Double DIS (point P1, point P2) //  Calculate the P1P2 distance  {  Return SQRT (( Double ) (P2.x-p1.x) * (p2.x-p1.x) + (p2.y-p1.y) * (p2.y- P1.y ));}  Bool CMP (point P1, point P2) //  The polar ordering function. If the angle is the same, the distance is smaller than the previous one.  {  Int TMP = cross (list [ 0  ], P1, P2 );  If (TMP> 0 ) Return   True  ;  Else  If (TMP = 0 & DIS (list [ 0 ], P1) <DIS (list [ 0 ], P2 )) Return   True  ;  Else   Return   False  ;}  Void Init ( Int N) //  Enter and place the vertices at the bottom left of the page in list [0]. And performs polar sorting. {  Int  I, K; point P0; scanf (  "  % D  " , & List [ 0 ]. X, & list [ 0  ]. Y); returns x = List [ 0  ]. X; Policy = List [ 0  ]. Y; k = 0  ; For (I = 1 ; I <n; I ++ ) {Scanf (  "  % D  " , & List [I]. X ,& List [I]. y );  If (Optional Y> list [I]. Y) | (Optional Y = list [I]. Y) & (Optional X> List [I]. X) {returns x = List [I]. X; Policy = List [I]. Y; k = I ;}} list [k] = List [ 0  ]; List [  0 ] = P0; sort (list + 1 , List + N, CMP );}  Void Graham ( Int  N ){  Int  I;  If (N = 1 ) {Top = 0 ; Stack [0 ] = 0  ;}  If (N = 2  ) {Top = 1  ; Stack [  0 ] = 0  ; Stack [  1 ] = 1  ;}  If (N>2  ){  For (I = 0 ; I <= 1 ; I ++) stack [I] = I; top = 1  ;  For (I = 2 ; I <n; I ++ ){  While (Top> 0 & Cross (list [stack [Top- 1 ], List [stack [Top], list [I]) <= 0 ) Top -- ; Top ++ ; Stack [Top] = I ;}}} 

 

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.