Computational geometry-convex hull problem (i.)

Source: Internet
Author: User

Note: This is one of the brief personal summary series of the MOOC course in Deng Junhui, Tsinghua University, in spring 2016, and I will synchronize the course content updates. However, it is possible to write not exactly the course content, but also some personal understanding. If you are interested in computational geometry after reading this article, please go to the appropriate MOOC platform to learn the full course of teacher Deng. Such well-designed and choreographed courses should not be betrayed. Thank you, teacher Deng!

Knowledge Dependence:

Before reading this article you only need to have basic geometry knowledge and algorithmic knowledge. The code implementation requires a lost C + + base. Because the author is lazy and doesn't want to draw, you need a little bit of brain theater. But believe me, it will be interesting to visualize the problem step-by-step in my head. Don't believe me? Look down.

Write in front

The computational geometry is the "algorithm and data structure" of the geometric version, which is true. She has the abstract and logical algorithm, but also the image of geometry and intuitive. This combination of two seemingly contradictory features made her a very interesting subject.

Calculate geometry, which is weighed on the calculation. That is, we use the computer, select efficient algorithm to solve the problem of geometry. See, eventually still settled in the "algorithm", so if said "computational geometry" is wrapped in "geometric" coat "algorithm design", completely can.

Since the study is an algorithm, it must also be the basis of many higher-level disciplines. Yes, the geometry, CAD, and even the path planning of artificial intelligence, can be seen in the figure of calculation.

Each discipline has its important and fundamental basic problems, computational geometry is no exception, convex hull problem is in the calculation of geometry occupies such a position.

Next I'll cover the convex hull problem. Mr Tang has said that he wants his high school graduates to understand, and I will do everything I can to introduce them in a streamlined and clear language.

What is a convex bag (convex Hull)

The strict mathematical definition is tedious and simple to say, a lot of nails are nailed in one area of the wall, you use a rubber band, pull it off, the ambassador can include all the nails, and then let go, when the rubber band formed the polygon is convex bag.

It is easy to see that the convex hull has at least two features:

    • Closed simple polygons (with no holes in the middle);
    • All the inner angles are less than 180 °;

The so-called convex hull problem is how to quickly find the "rubber Band" (convex hull) problem given these "nails" (point sets).

Pole (Extreme point) algorithm

What is the pole?

Very simply, the "nail" that is involved in fixing the "rubber Band" is the pole.

Now let's focus on the pole. Why is it? Because if we find out all the poles naturally we have found the convex hull.

So how do you determine which points in a given set point are the poles? Or another way to ask is, what is the difference between the pole and the other points?

It is clear to us that the Poles will not be included in a triangle made up of any other three points, rather than the Poles must be able to find a triangle consisting of three other points, enclosing it.

So, naturally, we can get this algorithm for finding the poles:

Enumerations can be made up of all the triangles that make up a set of fixed-point sets, and for each such triangle, the enumeration is a bit (not the vertex that makes up such a triangle), and if it falls within the triangle for the current point, then we can immediately determine that it must not be the pole and can be ruled out. Finally, after all the enumerations have been completed, all the points that are not excluded by us constitute the poles. We also get the convex hull at this time.

Seems very clear concise, finished?

...... There is one more question, how do we encode the implementation to determine whether a point falls on a certain triangle or something?

With a little thought we will find that we need to turn the problem further into a simple question. That is, determine the relationship between the point and the triangle.

We define the positive direction counterclockwise and the three edges of the triangle as directed segments.

At this point, if we can determine that the point is on the left side of the triangle three, then it must fall within the triangle. You can enumerate the validations and vice versa.

Yes, the problem is more streamlined.

Again, how do we tell if a point is on the left side of the directed segment?

Have studied analytic geometry friend certainly to this question is not unfamiliar, "the Directed area" This concept can save us (if you are not clear, see: Have to Area _ Baidu Encyclopedia). Under the premise of our current defined positive direction, if the directed area between the point and the two breakpoints of the directed segment is positive, then this point must be on the left side of the directed segment. Note that this method is still used frequently in the algorithm behind us, so if you have doubts about it, be sure to understand it.

OK, so far, all our problems have been solved.

All of the above analysis of the problem of the process, there is no place in the mathematics of the "idea of", or the idea of "divide and conquer" in the algorithm, that is, a big problem into a more easily resolved small problems, layered into the final, we can solve the problem, the original problem solved naturally. This is what we Chinese often say "punches, trivial." You will find that we still use this idea in the back.

Code implementation: (Every step is very clear, and there are comments, it is recommended to combine the code to comb the idea again)

1 /*******************************2 * extreme_point_algorithm for convex Hull construction3 * Time Complexity:o (n^4)4 ********************************/5 6 voidExtremepoint (Point s[],intN) {7     //initialization: "Presumption of innocence", that all points are poles, and then one by one excluded8      for(ints =0; s < n; s++) S[s].extreme =true;9 Ten     //enumerate all the triangles One      for(intp =0; P < n; p++) A          for(intQ = p +1; Q < n; q++) -              for(intr = q +1; R < N; r++) { -                 //enumeration is a bit the                  for(ints =0; s < n; s++) { -                     //remove vertices and points that have been judged to be non-poles -                     if(s = = P | | s = = Q | | s = = r | |!s[s].extreme) -                         Continue; +                     //Conduct Intriangle Test -                     if(Intriangle (S[p], s[q], s[r], S[s])) +S[s].extreme =false; A                 } at             } - } -  - BOOLIntriangle (point P, point Q, Point R, point s) { -     //make ToLeft Test on each side of the triangle respectively -     BOOLPqleft =ToLeft (P, Q, s); in     BOOLQrleft =ToLeft (q, R, s); -     BOOLRpleft =ToLeft (R, p, s); to     //if the ToLeft Test results are the same, then it is indicated in this triangle +     return(Pqleft = = Qrleft) && (Qrleft = =rpleft); - } the  * BOOLToLeft (point P, point Q, point s) { $     //The toleft Test is equivalent to twice times "direction area" to avoid the problem of precision caused by division or trigonometric operation.Panax Notoginseng     returnAREA2 (P, Q, s) >0; - } the  + intArea2 (point P, point Q, point s) { A     //twice times "forward area" the     return +p.x * Q.Y-P.Y *q.x -+ q.x * S.Y-Q.Y *s.x $+ s.x * P.Y-S.Y *p.x; $}

Although we seem to have solved this problem, a friend familiar with algorithmic complexity analysis may have realised from the beginning that this is a time-n^4 algorithm that is totally unacceptable in practical applications!

So, in the next few installments of this series, we'll walk through a step-by-step approach to more computational geometry classic algorithms that are getting less complex. At the same time, we will briefly analyze the lower bound of the theoretical time complexity of convex hull problem, and you will see that there is an algorithm that can achieve the lower bound of theoretical time complexity.

"To Be Continued"

Computational geometry-convex hull problem (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.