Braham algorithm 1 for Convex Hull

Source: Internet
Author: User

Emotion:

When I was in college, I often heard from teachers that comments must be written when writing code. I recently read a lot of people's code and found that there are few comments one by one, which is really painful. I don't know. Why did they delete all the comments when they posted the code? Or are they too strong to write comments, which is a waste of time or not? There are really few notes. When they were doing projects for the Japanese, they were very strict with the code format requirements, so they gradually developed the habit of writing even comments. However, the Japanese are a little harsh. They even have a few spaces, fullwidth, halfwidth, and comments.

I 'd better write comments, saving others from complaints.

Some time ago, I accidentally saw the problem about the convex bag. The specific concept and meaning of the convex bag are not mentioned. The convex bag is similar to this:

If there are a lot of wooden piles on the prairie, You Need To enclose them with a rope and enclose them in a circle. Determine the Coordinate Position of the wooden pile that forms the ring.

According to the materials, Braham is the most famous algorithm to solve the problem of convex hull.

1. I modified the Braham algorithm.

Use the XY coordinate system to represent the position of the wooden pile. First, find the Wooden Pile with the smallest y value or the smallest y value with the same X value. This does not change. Then, the Braham algorithm sorts other wooden piles, sort the size from small to large based on the angle or distance of the first starting wooden pile, and then find the points that meet the requirements in these sorted nodes.

My modification to it is that it is not sorted. From the starting point, I will directly find the next point, and then continue searching for the next point based on the found point, the starting point is found for the next vertex, and the process ends.

How can we determine whether a certain point meets the requirements? It depends on whether all other points are in the clockwise direction of the vector formed by the base point and the point.

Given 3 points, base, P1, p2. calculate flag = (base. x-p2.x) * (p1.y-p2.y)-(p1.x-p2.x) * (base. y-p2.y ). flag> 0, indicating that P2 is in the clockwise direction of base-> P1 vector, flag <0, indicating that P2 is in the clockwise direction of base-> P1, flag = 0, indicating base, P1, p2 is three-point in line. If the three points are collocated, find the point closest to the base point.

The Code is as follows: (my comments are quite detailed)

/** <Br/> * Another idea of the Braham algorithm <br/> * use the starting point in the bottom or bottom-left corner to simulate the reality and follow the clockwise direction, find the next vertex of the convex edge <br/> * and then find the next vertex Based on the vertex to be found. Then, when the next vertex is located at the last vertex, the start point is found, and the process ends. <br/> * this process has a bug, which does not support the case where three points are collocated. <br/> * if you want to fix this bug with this idea, very troublesome, <br/> * The regular Braham algorithm is king. <br/> **/<br/>/** <br/> * search for a graph composed of the pointset array midpoint. <br/> * use pointset [0] (head) point is the starting point <br/> * The Y coordinate of the head point is the smallest. If the Y coordinate has multiple points, find the smallest vertex in the X coordinate <br/> * all vertices in the returned vector and connect them to the edges of the convex hull in order <br/> **/<br /> Public vector <point> brahamscan (point [] pointset) {<br/> // record the subscript of the element that meets the starting point requirement <br/> int unum = 0; <br/> for (INT I = 1; I <pointset. length; I ++) {<br/> If (pointset [unum]. y> pointset [I]. Y | <br/> (pointset [unum]. y = pointset [I]. Y & pointset [unum]. x> pointset [I]. x) {<br/> unum = I; <br/>}< br/> // place the elements that meet the requirements first <br/> If (unum! = 0) {<br/> point TMP = pointset [0]; <br/> pointset [0] = pointset [unum]; <br/> pointset [unum] = TMP; <br/>}< br/> vector <point> edge = new vector <point> (); <br/> point head = pointset [0]; <br/> edge. addelement (head); <br/> // after sorting is completed, all other points are in the direction of the head-> pointset [1] vector counterclockwise <br/> bsort (pointset ); <br/> while (! Head. equals (pointset [1]) {<br/> edge. addelement (pointset [1]); <br/> // place pointset [1] at the zero position <br/> // next time, the sorting continues Based on pointset [0, place the next point of pointset [0] at the position of pointset [1] <br/> point TMP = pointset [0]; <br/> pointset [0] = pointset [1]; <br/> pointset [1] = TMP; <br/> bsort (pointset ); <br/>}< br/> return edge; <br/>}< br/>/** <br/> * Find a point P in the Set array, so that all other points are in the clockwise direction of set [0] P <br/> * place that point in the position of set [1] <br/> **/<br/> private void bsor T (point [] Set) {<br/> int unum = 1; <br/> for (INT I = 2; I <set. length; I ++) {<br/> // do not consider the case where the three points are collocated at the moment <br/> If (multi (set [0], set [unum], set [I]) <0) {<br/> unum = I; <br/>}< br/> // place that point in the location set [1]. <br/> If (unum! = 1) {<br/> point temp = set [1]; <br/> set [1] = set [unum]; <br/> set [unum] = temp; <br/>}< br/>/** <br/> * It is based on P0, determine whether the P2 point is in the clockwise direction of the p0p1 vector or in the clockwise direction. <br/> *> 0: the P2 point is in the counterclockwise direction of the p0p1 vector. <br/> * = 0: three-point collinearity <br/> * <0: point P2 in the clockwise direction of the p0p1 vector <br/> **/<br/> private int multi (point P0, Point P1, point P2) {<br/> return (p0.x-p2.x) * (p1.y-p2.y)-(p1.x-p2.x) * (p0.y-p2.y); <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.