Braham algorithm 2 for Convex Hull

Source: Internet
Author: User

In <a href = "http://blog.csdn.net/secondsquare/archive/2009/03/11/3980570.aspx"> Braham algorithm 1 </a>, There is a bug in modifying the regular standard Braham algorithm, that is, it does not support three-point collinearity.

So out of admiration for the master, I wrote a standard Braham algorithm.

1. First find the smallest point of Y coordinate, or the smallest point of Y coordinate with the same X coordinate, and place it at 0.

2. Sort the 1-(length-1) points in ascending order of the vector polar angle and distance between the 0 points. Flag = multi (0, I, j). If the flag is greater than 0, the J point is in the clockwise direction of the 0-> I point, the polar angle of 0-> I is smaller than that of 0-> J. If the flag is <0, it indicates that J is in the clockwise direction of 0-> I, the polar angle of 0-> I is greater than that of 0-> J. If the flag is 0, the size is determined based on the distance.

3. Place, and on the stack. Point 1 must be the point that meets the requirements. It is the next point at point 0. Then we need to judge whether is the next point. Then, you can determine whether all other points are in the clockwise direction of the stack [Top-1]-> stack [Top] vector. If yes, the next point will be pushed into the stack; if not, the top point of the stack will pop up and pushed to the next point. Until there is no point to be pushed into the stack.

The Code is as follows: (I wrote a detailed comment)

/** <Br/> * implement the regular Braham algorithm below <br/> * 1. Place the points that meet the requirements at the [0] position. <br/> * 2, sort other vertices by the Polar Angle of [0], from small to small <br/> * 3, put [0] [1] [2] into the stack, then, you can determine whether all other points are in the clockwise direction of the [Top-1] [top] vector. <br/> * 3.1 If yes, remove a point and put it in the stack. Then, continue to judge, until no more points are put into the stack <br/> * 3.2 If not, the [Top] Point is displayed and the next point is added, continue judgment <br/> **/<br/> Public point [] brahamscan2 (point [] Set) {<br/> // record the subscript of the element that meets the starting point requirements <br/> int unum = 0; <br/> for (INT I = 1; I <set. length; I ++) {<br/> If (set [unum]. y> set [I]. Y | <Br/> (set [unum]. y = set [I]. Y & set [unum]. x> set [I]. x) {<br/> unum = I; <br/>}< br/> // place the elements that meet the requirements first <br/> If (unum! = 0) {<br/> point TMP = set [0]; <br/> set [0] = set [unum]; <br/> set [unum] = TMP; <br/>}< br/> // sort the set and quickly sort it <br/> qsort (set, 1, set. length-1); <br/>/* <br/> * sort is alright <br/> for (Int J = 0; j <set. length; j ++) {<br/> system. out. println (set [J]); <br/>}< br/> */<br/> // create a stack. <br/> point stack [] = new point [set. length]; <br/> int Top =-1; <br/> // set [1] is the first Meeting point <br/> stack [++ top] = set [0]; <br/> stack [++ top] = set [1]; <br/> // s Tack [++ top] = set [2]; <br/> // traverses the remaining vertices to meet the requirements of pushing them to the stack and not meeting the requirements, pop-up stack <br/> for (INT I = 2; I <set. length; I ++) {<br/> // press set [I] into the stack <br/> stack [++ top] = set [I]; <br/> // if the requirement is not met, the stack is displayed. <br/> If (! Isok (set, stack [Top-1], stack [Top]) <br/> top --; <br/>}< br/> return stack; <br/>}< br/>/** <br/> * isok: <br/> * determine whether all vertices in the set are in the clockwise direction of the end vector. <br/> **/<br/> private Boolean isok (point [] set, point start, Point End) {<br/> Boolean flag = true; <br/> for (INT I = 0; I <set. length; I ++) {<br/> // <0: clockwise direction of the set [I] at start-> end vector, exit <br/> If (multi (START, end, set [I]) <0) {<br/> flag = false; <br/> break; <br/>}< br/> return flag; <br/>}< br/>/** <br/> * use set [0] As the base point for set [1] .. set [length-1] sorting <br/> * If multi (set [0], set [I], set [J]) <0, the clockwise direction of set [J] In set [0]-> set [I, <br/> * Set [0]-> set [J] has a shorter polar angle than set [0]-> set [I, set [J] should be placed before set [I] <br/> **/<br/>/** <br/> * fast sorting: <br/> **/<br/> private void qsort (point [] Set, int start, int end) {<br/> If (start <End) {<br/> int x = start; <br/> Int J = end; <br/> int I = start + 1; <br/> // when I = J, all elements greater than set [x] Are On The right of set [X, all elements smaller than set [x] are on the left of set [x] <br/> while (I <= J) {<br/> // locate a set [J] <set [X] and switch <br/> while (j> = I) {<br/> // <0: set [J] points in the clockwise direction of the set [0]-> set [x] vector <br/> // switch between set [J] And set [x] <br/>/ /= 0: the three points are Collocated and the distance is determined. <br/> // The distance is small. <br/> int flag = multi (set [0], set [X], set [J]). <br/> If (flag <0 | (flag = 0 & (point. DIS (set [J], set [0]) <point. DIS (set [X], set [0]) {<br/> point TMP = set [x]; <br/> set [x] = set [J]; <br/> set [J] = TMP; <br/> X = J; <br/> j --; <br/> break; <br/>}< br/> j --; <br/>}< br/> // find set [I]> set [X], and exchange <br/> while (I <= J) {<br/> //> 0: the set [I] point is in the clockwise direction of the set [0]-> set [x] vector <br/> // switch between set [I] And set [x] <br/>/ /= 0: the three points are collocated. The distance is determined. <br/> // The distance is large. <br/> int flag = multi (set [0], set [X], set [I]). <br/> If (flag> 0 | (flag = 0 & (point. DIS (set [I], set [0])> point. DIS (set [X], set [0]) {<br/> point TMP = set [x]; <br/> set [x] = set [I]; <br/> set [I] = TMP; <br/> X = I; <br/> I ++; <br/> break; <br/>}< br/> I ++; <br/>}< br/> qsort (set, start, x-1); <br/> qsort (set, x + 1, end ); <br/>}< br/>}

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.