Second: It's time for us to talk about new ideas.

Source: Internet
Author: User

The previous article roughly introduced the classics in the area of triangle partitioning. From this article, I will show you the new algorithms, let's talk about it in a different way.
Maybe you have never been in touch with this field. Maybe your current work is cool with this field. Maybe you think this field is doomed to have nothing to do with you. I will tell you that if I saw this article a month ago, I will have the same idea. I believe that this seemingly professional essay will let every visitor know what I am talking about and give you some inspiration, more importantly, I will show you how to implement your ideas in an object-oriented manner in subsequent articles so that they can actually work for you.
The following is a video Video video using this algorithm for triangle partitioning with holes and polygon. Let's take a look at this video.

Any plane polygon is the most feared word. For a special polygon, such as a convex polygon, it is easy to come up with an algorithm that selects any vertex that is connected to a vertex that is not adjacent to the polygon. Unfortunately, there are indeed some convex polygon in my problem, but more of them are strange polygon, because these polygon are input by users and I cannot expect them. After an analysis, the requirement for a new algorithm is imminent.
I was lucky enough to find this new algorithm, and then I got these words. The new algorithm consists of three parts: Finding guides, finding intersections, and searching triangles.

1. Search for guides
After a long search on the Internet, I finally found the inspiration in a forum's reply and decided to start from here. This is the scanning line (horizontal Green Line ).
 

Here I will first describe the scanning line: the scanning line is horizontal, and its ordinate is taken from the ordinate of a certain vertex of the polygon. Therefore, there will be eight scanning lines in the polygon. The figure shows the first line. Assume that the number of vertices in a polygon is N, and the number of lines scanned is less than or equal to N. In the worst case, N is obtained. If the ordinate values of two or more vertices are the same, the number of scanned lines is reduced. The time complexity of searching for these scanning lines is O (N). Just traverse the vertices once.
When you are careful, you must have discovered the Vertical Green line on the left. I call it the left auxiliary line. It has a major effect! The selection method of this line is to find the Minimum horizontal coordinates of all vertices and move them to the left (this value is arbitrary). What is its function, I will tell you the answer later. The time complexity of searching for this left auxiliary line is O (N ).

2. Finding intersection points
Although everything above is very important, it is just a prelude. Now let's move on to the main melody.
The function of the left auxiliary line is revealed here: the left auxiliary line must be out of the polygon, so that the scanning lines of each row start from the left auxiliary line and the starting point must be outside the polygon, so that if the scanning line and the polygon intersection, the first time must be a polygon, the second time must be a polygon, the third time must be a polygon, and the fourth time ......, In short, the odd point of intersection is the entry polygon point, and the even point of intersection is the exit polygon point. The number of intersection points must be an even number (for example, there are four intersections in the fourth scanned line ).
The intersection of each row should be retained for future use. Create a two-dimensional table to create a new line for each scanning line, and save the intersection of the scanning line and the polygon in each row. Note that the scanning line is found when traversing each vertex, And the ordinate of each vertex cannot be sorted in descending or ascending order, therefore, it is necessary to sort the scanning line array once so that they can be ranked from top to bottom in the orders table. The sorting time complexity of this step is O (N * logN ), so far, our algorithm's time complexity has increased to O (N * logN ).
 

After careful consideration, you can find out again. Isn't it true that there is only one intersection point for this scanning line? Yes, it has special processing for it.
Each scanned line must pass through at least one polygon vertex because of the scanning line selection method. The vertex that passes through is actually the intersection of the scanning line and the two edges. This is obvious, that is, the same point is obtained when the intersection of the two consecutive edges and the scanning line are obtained, in this case, some special processing is required. You will find that when this happens, there are two possibilities: one is that the scanning line does not enter the polygon after it passes through the point, but a side ball is played and the polygon is removed, the top vertex is like this. The other is that the scanning line enters the polygon from outside the polygon through the vertex, And the leftmost vertex in the second row is like this. This must be treated differently. If it is the first case, we think that the scanning line enters the polygon and goes out of the polygon at the same time. we add the point to the orders table twice, in the second case, we think it is a common point, just add it once. As for how they differentiate themselves by computing, let's go back to the sixth session: finding the point of intersection, one step ahead of the victory.
It is worth noting that the point in the middle of the third row will also occur when the edge Ball is played. It does not walk out of the polygon, so it is more accurate: the first case is that the position of the polygon is not changed after the scanning line passes through a vertex of the polygon (inside and outside). The second case is that the scanning line changes the position of the polygon after passing through a vertex of the polygon.
Each scanned line must be handed over to all edges of the polygon. The vertex table is filled with the obtained intersection. because the number of edges is the same as that of the polygon vertex, so the time required for this step is O (N ^ 2 ). After this step is completed, we get a two‑dimensional table filled with all vertices, and the time complexity reaches O (N ^ 2 ).

 
We will introduce the sequence table in details in the sixth session: finding the intersection point, one step left before victory.

3. Search for triangles
Finally, we have reached the climax, and all our previous efforts here will turn into the expected results.
When building the sequence table mentioned above, do you think of its function? Why is there any implicit meaning in such a structure that the even point of each row has. Let's move on to figure out why we need to build a scanning line. What is the significance of its introduction.
In the orders table, the intersections of each row are unordered and stored in an array. For our analysis, we need to sort the intersections of each row from left to right according to the abscissa, the number of intersection points in each row reaches N in the worst case, because a scanning line may be handed over to all sides, and the sorting time complexity of this line is O (N * logN ), because there are N rows, the time complexity of this step is O (N ^ 2 * logN). So far, the time complexity of this algorithm has increased to O (N ^ 2 * logN ).
Each row in the orders table has an even number of intersections, and all the intersections in the even number of indexes are entry points, and all the intersections in the odd number of indexes are exit points, this has been explained above (the index of the first intersection is 0). Now we propose a point pair, that is, the adjacent two points constitute such a point pair, the scanning line in the point pair must be within the polygon. In this way, the two vertices corresponding to the two adjacent scanning lines will surround a part of the polygon area. The shape of this area must be trapezoid, finding all such areas will fill the whole polygon, And the trapezoid will be converted into a triangle, which is easy. When looking for a trapezoid, You need to traverse all the scanning lines. When traversing each scanning line, You need to traverse each intersection of the scanning line, therefore, the time complexity required for this step is O (n ^ 2 ).

Of course, there will be some special situations in the process of searching for a trapezoid. We will go into details in "7: Searching for a triangle and winning a red flag.

This algorithm is easy to understand. The time complexity of O (N ^ 2 * logN) is acceptable, and the core code is about 300 lines. The actual application performance is still commendable. The following articles will cover various aspects of this article. The implementation method uses a lot of object-oriented techniques. The next article will explain in detail how to implement the display of each step in the video.

 

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.