Convex Package Algorithm __ Math

Source: Internet
Author: User

Understand the convex bag first

The convex package must first say the definition of convexity, the simple point is that the point of any two points in the plane neighborhood is in the neighborhood, then the neighborhood has convexity. With a simple scrutiny, it can be found that a point with a first order derivative discontinuity in the neighborhood must not be represented linearly by a set of dots. Further down the content belongs to the mathematical analysis, to our algorithm design is not helpful, temporarily first.

The general computational geometry problem is the planar domain of the discrete point set that is processed, so we are interested in finding a convex convex polygon that contains the smallest area of the point set. As common sense should know that the vertex on the convex package is necessarily a subset of the point set, so we can design an efficient algorithm based on this property.

In this paper, we will introduce three kinds of methods to find convex packages of planar point set, and pay special attention to the convex package of convex package of polygon, so as to help us to study deeply.

Gift Wrapping method: This trick I think or not to say, because I think no one will use, in addition to better understanding of no good.

Graham-scan: This should be the first O (NLGN) algorithm, the implementation is relatively simple, the basic idea is to maintain a convex curve, so it requires that the algorithm at the beginning must know at least one point on the convex package as its starting point (fortunately this is relatively simple). It has the disadvantage of using it directly to find a convex package of a given polygon may cause errors (the algorithm art gives the sample), so the algorithm must be ordered before the beginning of the order.

Melkman: So far the best convex package algorithm, I strongly recommend the algorithm. Its basic operation is the same as that of Graham-scan, except that it obtains the convex package of the current inspected point at any time, so it has an incomparable advantage that it is an online algorithm (to add a point to the point set without having to recalculate). For a given polygon, it can directly ask for its convex package without first ordering. And it has the biggest advantage is that the implementation is very simple, so it is particularly suitable for use in the game.

Although the Melkman is good, but graham_scan is commonly used

Ordering of preliminary points and decision of left-turn

Ordering of points

To find the convex package of fixed-point set, it usually requires some preprocessing process, and the ordering of points is one of them. A method of ordering points according to a certain rule is given below, which plays an important role in many algorithms for finding convex packages.

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml"/>


1. Find a point that must be on the convex package (this is easy to ^_^, usually take the point of the horizontal axis or ordinate), recorded as P0,

2. Link P0 and other points, respectively, calculate these segments and the "vertical downward direction" of the angle, according to the angle from the small arrival in the order of the other end of each segment (one end is P0) labeled as P1, P2, P3 ...

turn left to determine

 

This is the classical computational geometry problem, the judgment vector p1= (x1,y1) to p2= (X2,y2) does turn left, only need to judge x1*y2-x2*y1 positive and negative, if the result is positive, then turn left from P1 to P2. Which is the cross product of the vector.

The Graham algorithm is like this.

1. Order the points (see the basic article), in order to ensure the formation of the circle, the P0 in the second place in the tail of the point table;

2. Prepare stack: Build stack s, stack pointer set to T, push 0, 1, 23 points into stack s;

3. For the next point I

As long as s[t-1], s[t], I do not make a left turn

The stack is repeatedly retired;

Press I into the stack s

4. The point in the stack is the convex package;

Its core is expressed in C language, just the following paragraph:

T=-1;

s[++t]=0; S[++t]=1; s[++t]=2;

for (i=3;i<n;i++)

{

while (!left (s[t-1],s[t],i))

t--;

S[++t]=i;

}

More complete code

Number of vertices of int top;//convex package
struct POINT
{
int x,y;
}P[MAXN],STACK[MAXN];

int max (int a,int b)
{
Return a>b?a:b;
}

int dis (point p1,point p2)///Two The distance squared
{
Return (p1.x-p2.x) * (p1.x-p2.x) + (P1.Y-P2.Y) * (P1.Y-P2.Y);
}

Cross product, the result is less than the polar angle of the expression vector p0p1 than the p0p2, equal to the two vector collinear
int multi (point P1, point P2, point p0)
{

Return (p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (P1.Y-P0.Y);
}

int CMP (point a,point B)
{
if (multi (a,b,p[0]) >0)
return 1;
if (multi (a,b,p[0]) ==0&&dis (a,p[0)) <dis (b,p[0))
return 1;
return 0;
}

//graham_scan essence
void Graham_scan (point p[],point stack[],int N)
{
 
 int i,j,k=0;
&nbs p;top=2;
 point temp;
 //looking for the bottom and left point
 for (i=1;i<n;i++)
  if p[i].y<p[k].y| | ((P[I].Y==P[K].Y) && (p[i].x<p[k].x))
   k=i;
 //Specifies the point as P "0";
 temp=p[0];
 p[0]=p[k];
 p[k]=temp;
 //from small to large, Short distance to sort

 //above is essential, if you press the above write, may time out, or use the following sort
 sort (p+1,p+n,cmp);
 //Core
  STACK[0]=P[0],STACK[1]=P[1],STACK[2]=P[2];
 for (i=3;i<n;i++)
 {
  while top>1&&multi (p[i],stack[top],stack[ TOP-1]) >=0
   top--
  stack[++top]=p[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.