[Poj 1113] Convex Hull for geometric computation (1) {VOLUME wrap algorithm}

Source: Internet
Author: User

{

During the winter vacation, we were writing convex packets.

These articlesArticleSort it out

IntroductionTwo-dimensional convex hullSolutionAlgorithm

And a simple application

}

========================================================== ======================================

1. Convex Set & convex bag

(All the sets in the following section refer to the set in the Euclidean space unless otherwise specified)

Convex Set): Any two-point link is a convex set in the set.

A set in Euclidean space is convex set if it contains all the line segments connecting any pair of its points.

Http://mathworld.wolfram.com/Convex.html

Convex Hull (convex hull): The intersection of all convex sets containing the set S is the convex packet of the Set S.

The convex hull of a set of points s in n dimensions is the intersection of all convex sets containing S.

We often pay attention to the convex hull of a point set, which is also a basic problem in computational geometry.

We now have mature algorithms for finding the convex hull of a plane point set and the convex hull of a spatial point set.

Some algorithms can easily find the convex hull of a point set in the Euclidean space of any dimension.

The convex hull has a beautiful and practical nature. We can use the convex hull to make effective the information contained in a messy point set.Summary

========================================================== ======================================

Ii. convex hull algorithm of a plane point set

(Unless otherwise specified, all the convex packets in the following section refer to the convex packets of the Plane Point Set)

There are two ways to intuitively understand the convex hull

Use an elastic rubber band to pin all nails on a wooden board.

The figure formed by the rubber band is the convex hull of the point set formed by the nail.

Another kind of understanding is that we use a hemp rope to tie an external nail and then pull it around all the nails.

The Hemp Rope finally forms the convex hull of the point set.

The second is a classic algorithm.Gift wrapping)Idea

The volume package algorithm selects the outermost point from a certain point on the convex packet in one direction.

When we return to the initial point, the selected point set is the required convex hull.

There are two other issues that are not clear:

1. How to determine a point that must be on the Convex Hull?

This problem is a good solution. The leftmost point is the smallest point of the abscissa.

If there are multiple such points, the ordinate values in these points are the smallest.

In this way, we can handle the problem of collinearity.

2. How to determine the next vertex (the outermost vertex )?

We need to use the cross product of the vector to solve this problem.

-----------------------------------------------------------------------

Cross Product of a vector)Originally, problems in 3D space were also cleverly applied in 2D space.

Http://mathworld.wolfram.com/CrossProduct.html

(All the cross products in the following section, unless otherwise specified, refer to the New Cross Product defined in the two-dimensional model.

All vector multiplication below is a cross product that points to a quantity unless otherwise specified)

We define the cross product of two-dimensional vectors <X1, Y1> and <X2, Y2> as a real number CP = x1 * y2-x2 * Y1

There are two kinds of cross-points that are commonly used and useful.

1. Half of the cross product is the directed area of a triangle.

This formula can avoid the error of area calculation. If the point is the whole point, all operations are integers.

2. The cross product symbol of the vector represents the direction of the vector rotation

The cross product of a vector does not meet the exchange law.

Multiply vector A by vector B. If the regular expression is a, it rotates clockwise toward B. Otherwise, it is clockwise.

Of course, here, the angle from A to B always considers an angle less than 180 degrees. Otherwise, an error will occur.

-----------------------------------------------------------------------

With the vector, we can selectOutermost sidePoint

For example, we need to select the outermost vertex to wrap the package to J.

Of course, it is inconvenient to directly obtain the outermost point from the red to the angular.

We can consider the Blue badge.

Using vectors, we canWhich of the following points is compared: "More external"

For example, for vertices K and I, we use the vector JK multiplied by the vector Ji to get a number. This number should be negative, indicating that I is more external than K.

The comparison between two vectors hasTransmissionSo we can retrieve the outermost

Traverse all vertices. Compare each vertex with the existing outermost vertex to obtain the new outermost vertex.

Now we have solved both of these problems. We can write a volume package algorithm that meets the general requirements.

However, there is still a problem to be solved.CollinearityProblems

Sometimes we need to remove the vertices on the sides of the convex bag.

We usually call the vertex at the top of the convex hullPole

If we only need to retain the pole and remove the vertices on the edge

We only need to obtain the farthest point when getting the outer point.

If we want to keep all vertices on the edge, we only need to retrieve the nearest

The whole volume package method is finally completed.

CompleteCode:

Giftwrapping

 {  $ Inline on  }  
Const Zero = 1e-6 ; Maxn = 100000 ;
Type Point = Record X, Y: extended; End ;
VaR P: Array [ 1 .. Maxn] Of Point;
Ch: Array [ 1 .. Maxn] Of Longint;
Temp, n, m, I, J, K: longint;
Function SGN (X: Extended): longint; inline;
Begin
If ABS (X) < Zero Then Exit ( 0 );
If X < 0 Then SGN: =- 1 Else SGN: = 1 ;
End ;
Function Cross (A, B, C: Point): extended; inline;
Begin
Cross: = (B. x - A. X) * (C. Y - A. Y) - (B. Y - A. Y) * (C. x - A. X );
End ;
Function Dist (A, B: Point): extended; inline;
Begin
Dist: = Sqr (A. x - B. X) + Sqr (A. Y - B. Y );
End ;
Function CMP (A, B, C: Point): Boolean; inline;
VaR Temp: longint;
Begin
Temp: = SGN (Cross (a, B, c ));
If Temp <> 0 Then Exit (temp < 0 ); { * B }
CMP: = Dist (A, B) < Dist (a, c ); { * }
End ;
Begin
Assign (input, ' Hull. In ' ); Reset (input );
Assign (output, ' Hull1.out ' ); Rewrite (output );
Readln (N );
For I: = 1 To N Do
Begin
Readln (P [I]. X, P [I]. y );
If (J = 0 ) Or (P [I]. x < P [J]. X) Or
(SGN (P [I]. x - P [J]. X) = 0 ) And (P [I]. Y < P [J]. Y)
Then J: = I;
End ;
Temp: = J;
While True Do
Begin
K: = 0 ;
INC (m); ch [m]: = J;
For I: = 1 To N Do
If (I <> J) And (K = 0 ) Or
CMP (P [J], p [K], p [I])
Then K: = I;
If K = Temp Then Break;
J: = K;
End ;
For I: = 1 To M Do
Writeln (P [CH [I]. X: 0 : 2 , ' ' , P [CH [I]. Y: 0 : 2 );
Close (input); close (output );
End .

* A change ction
* B remove colinear points

There are two additional notes:

1. The complexity of the volume package algorithm isO (NH)

N is the number of all points H is the number of points eventually on the convex hull

Therefore, the volume package algorithm is suitable for scenarios where the number of vertices on a convex packet is very small, and usually the random data is fast.

However, the algorithm will be slow to construct a lot of data points on the convex packet.

For example, all vertices are on a circumference.

2. The point output by the volume package algorithm isOrderedOf

This is also a basic requirement for the two-dimensional convex hull algorithm.

Generally, further computation can be performed only when the order is ensured.

ChangeCMP FunctionsWe can change the collinearity (fetch/not fetch) mentioned above and the order (clockwise/counterclockwise) here.

========================================================== ======================================

3. Convex Hull area and perimeter

A Simple Algorithm of convex hull is introduced.

Let's look at a specific problem poj 1113 http://poj.org/problem? Id = 1113

The problem is to find the perimeter of a trajectory with a simple polygon distance of at least l.

We can first obtain the convex hull and then obtain the calculation formula.

In fact, the part of the circle can be spliced into an integral circle.

In this case, it is much more important to remove all the colons. Otherwise, the error will be very large.

The code is not pasted. It's very easy.

Next, we will introduce a Faster Algorithm for plane convex hull.

Graham scan and quickhull

Quickhull is strongly recommended.

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.