Two dimen1_convex hull

Source: Internet
Author: User
Two dimen1_convex hull

Two-dimensional convex hull

Translated by Felicia crazy

(Qq 125376968 MSN felicia1101@hotmail.com)

Prerequisites
  • Geometry
Mathematical Model

A point set in the plane is given to find the convex polygon with the smallest area so that these points are within the polygon (or on its side ).

It can be seen that the vertices of a polygon must be points in the set point.

Example: grazing cows

Farmer John wants to build a fence to prevent nasty local college students from turning them over while their cows are sleeping. Each of his cows has a favorite grazing point, which farmer John wants to enclose. John, the farmer, wants a convex shape, because it is easier to drive the cows into the farm.

Help Farmer John determine the shape of the fence with the smallest area and which includes all cows love to eat grass.

Algorithm

There are several algorithms to solve the problem of two-dimensional convex hull. Here, we will only introduce the "volume package" algorithm that is easier to code and remember (in fact, it is what we call the grahan scanning-Translator's note ).

The basic idea of the algorithm is to add vertices around a point in the convex hull from a clockwise or counterclockwise direction, make sure that each inner angle is smaller than 180 degrees (the final answer is convex ). If the angle of the three consecutive vertices is greater than 180 degrees, delete the intermediate vertex. You can use the cross product of two continuous vectors along the polygon edge to determine whether the angle is greater than 180 degrees.

Convex hull algorithm process:

  • Find a point in the convex hull.
  • Calculate the angle between the line between each point and the midpoint and the X axis (within the range of 0 to degrees)
  • Sort vertices based on these angles
  • Add the first two vertices
  • For other vertices except the last Vertex
  • Make it the next Vertex on the convex hull
  • Check whether the angle between it and the first two vertices is greater than 180 degrees.
    • If the angle between it and the first two vertices is greater than 180 degrees, delete the vertex before it.
  • Add the last Vertex
    • Complete the preceding deletion task
    • Check whether the angle between the last vertex and its first vertex and the first vertex is greater than 180 degrees, or whether the angle between the last vertex and the first and second vertex is greater than 180 degrees.
    • If the first condition is true, delete the last vertex and check the last vertex.
    • If the second condition is true, delete the first vertex and continue the check.
    • If neither of the two conditions is true, stop.
  • Because of the angle calculation method, the time complexity of increasing the vertex is linear (that is, O (n )). Therefore, the time complexity of running is determined by the time complexity of sorting. Therefore, the time complexity of the volume package method is O (NLogN), Which is the best.
Pseudocode

Here is the pseudocode of the convex packet algorithm:

# X (I), y (I) is the X, Y position
# Of the I-th point
# Zcrossprod (V1, V2)-> Z component
# Of the vectors V1, V2
# If zcrossprod (V1, V2) <0,
# Then V2 is "right" of V1
# Since we add counter-clockwise
# & Lt; 0-& gt; angle & gt; 180 deg
1 (midx, midy) = (0, 0)
2 For all points I
3 (midx, midy) = (midx, midy) +
(X (I)/npoints, y (I)/npoints)
4 For all points I
5 angle (I) = atan2 (Y (I)-midy,
X (I)-midx)
6 perm (I) = I

7 sort perm based on the angle () Values
# I. e., angle (Perm (0) <=
Angle (Perm (I) for all I

# Start making hull
8 hull (0) = perm (0)
9 hull (1) = perm (1)
10 hullpos = 2
11 For All Points P, Perm () Order,
Except perm (npoints-1)
12 while (hullpos> 1 and
Zcrossprod (Hull (hullpos-2 )-
13 hull (hullpos-1 ),
Hull (hullpos-1)-P) <0)
14 hullpos = hullpos-1
15 hull (hullpos) = P
16 hullpos = hullpos + 1

# Add last point
17 p = perm (npoints-1)
18 while (hullpos> 1 and
Zcrossprod (Hull (hullpos-2 )-
19 hull (hullpos-1 ),
Hull (hullpos-1)-P) <0)
20 hullpos = hullpos-1

21 hullstart = 0
22 do
23 flag = false
24 if (hullpos-hullstart> = 2 and
Zcrossprod (p-
25 hull (hullpos-1 ),
Hull (hullstart)-P) <0)
26 p = hull (hullpos-1)
27 hullpos = hullpos-1
28 flag = true
29 If (hullpos-hullstart> = 2 and
Zcrossprod (Hull (hullstart)-P,
Hull (hullstart + 1 )-
Hull (hullstart) <0)
30 hullstart = hullstart + 1
31 flag = true
32 while flag
33 hull (hullpos) = P
34 hullpos = hullpos + 1
Tracking

For the following trace, we use these vertices:

Select a center, calculate the angle, and sort.

Now, we start by adding the first two vertices.

Now, add the third vertex. Since this step does not constitute a angle greater than 180 degrees with the first two vertices, we only need to add this vertex.

Add the fourth vertex. This time, there is no angle above 180 degrees, so you don't have to do more work.

Add the fifth vertex.

Since the third, fourth, and fifth vertices constitute an angle greater than 180 degrees (a "right" turn), we delete the fourth vertex.

The second, third, and fourth vertices do not constitute an angle greater than 180 degrees, so we have completed the task of adding the fifth vertex.

Add the sixth, seventh, and eighth vertices. There is no additional work in this process.

Then, add the last vertex.

The eighth and ninth vertex form a "right turn" with the first vertex; Delete the ninth vertex.

The seventh, eighth, and first vertex have been completed, but the eighth, first, and second vertex constitute a "right turn", so we must delete the first vertex.

Now, the eighth vertex, the second vertex, and the third vertex constitute a "right turn", so we have to delete the second vertex.

The remaining vertices do not violate the "Left turn" principle, so we have completed the task and created a convex hull for the vertex.

Problem prompt

A question similar to placing a vertex in a polygon is usually a convex hull. If the question requires a convex polygon with the smallest area or a convex polygon with the smallest perimeter, we can almost be sure that it requires a convex bag.

Promotion

Unfortunately, this algorithm cannot be simply extended to three dimensions. Fortunately, the 3D convex hull algorithms are all super complicated (more disgusting than four-dimensional), so the question is unlikely to be answered.

If you add any constraints to a polygon, this algorithm is used up (for example, there are no more than N vertices in a polygon, or a rectangle is required ).

Difficulties in the example tree [IOI 1991, problem 2]

Known: There are some trees, you must use wire around these trees, so that you use the shortest wire. Which trees will calculate the polygon vertices and the length of the wire, and whether the farmer's hut is inside the polygon, out of it, or crossing the polygon edge?

Analysis: the length of vertices and wire of a polygon can be obtained directly from the problem. The farmer's hut is a rectangle on the coordinate axis. You need a little geometric knowledge to determine that all vertices in the rectangle are in the convex bag, both out of the convex bag, or some are in the convex bag, some are outside the convex hull. In this way, you can get the answer you want. Check the ry manual and find the solutions to these problems.

The construction of the moat

Known: calculates the minimum length of the moat containing these polygon except a polygon.

Analysis: the convex hull of a known polygon is equivalent to the convex hull of all its vertices. This is a combination problem and requires a loop and a convex hull generator. For each house, delete the house and calculate the convex hull of the remaining vertices. Select the house with the smallest convex hull. Note that you only need to consider the houses whose vertices overlap with the whole convex hull. Considering the houses in the whole convex hull is not helpful for the problem.

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.