Computational Geometry Fundamentals (templates)

Source: Internet
Author: User

1. Polygon Area Calculation

1     DoubleS (Point p[],intN)2     {  3         DoubleAns =0; 4P[n] = p[0]; 5          for(intI=1; i<n;i++)  6Ans + = Cross (p[0],p[i],p[i+1]); 7         if(Ans <0) ans =-ans; 8         returnAns/2.0; 9}

2. Find Convex bag

1     BOOLCMP (Point a,point B)2     {  3         DoubleK =Cross (MINA,A,B); 4         if(k<0)return 0; 5         if(k>0)return 1; 6         returnDist (MINA,A) <Dist (MINA,B); 7     }  8       9     voidGraham (Point p[),intN)Ten     {   One          for(intI=1; i<n;i++)   A            if(p[i].y<p[0].y | | (P[i].y = = p[0].y && p[i].x < p[0].x)) -Swap (p[i],p[0]);  -MinA = p[0];  theP[n] = p[0];  -Sort (p+1, p+n,cmp);  -stack[0] = p[0];  -stack[1] = p[1];  +top =2;  -          for(intI=2; i<n;i++)   +         {   A              while(Top >=2&& Cross (stack[top-2],stack[top-1],p[i]) <=0) top--;  atstack[top++] =P[i];  -         }   -}

3. Center of gravity for any polygon

1Point Gravity [Point p[],intN)2     {  3 Point o,t; 4o.x = O.Y =0; 5T.x = T.y =0; 6P[n] = p[0]; 7         DoubleA =0; 8          for(intI=0; i<n; i++)  9A + = Cross (o,p[i],p[i+1]); TenA/=2.0;  One          for(intI=0; i<n; i++)   A         {   -T.x + = (p[i].x + p[i+1].x) * Cross (o,p[i],p[i+1]);  -T.y + = (p[i].y + p[i+1].Y) * Cross (o,p[i],p[i+1]);  the         }   -T.x/=6*A;  -T.y/=6*A;  -         returnT;  +}

4. Finding the coordinates of the intersection of segments

1     BOOLSegment_crossing (Segment u,segment v)/** Determine if line segments intersect*/  2     {  3              return(Max (u.a.x,u.b.x) >=min (v.a.x,v.b.x)) &&4(Max (v.a.x,v.b.x) >=min (u.a.x,u.b.x)) &&5(Max (U.A.Y,U.B.Y) >=min (V.A.Y,V.B.Y)) &&6(Max (V.A.Y,V.B.Y) >=min (U.A.Y,U.B.Y)) &&7(Cross (V.A,U.B,U.A) *cross (U.B,V.B,U.A) >=0) &&8(Cross (U.A,V.B,V.A) *cross (V.B,U.B,V.A) >=0)); 9     }  Ten        One     /** Seek the coordinates of the line intersection, or return the address of the intersection p if no intersection returns NULL*/   Apoint*CrossPoint (Segment u,segment v) -     {   - Point p;  the         if(segment_crossing (u,v)) -         {   -p.x= (V.B,U.B,U.A) *v.a.x-cross (V.A,U.B,U.A) *v.b.x)/(Cross (V.B,U.B,U.A)-Cross (V.A,U.B,U.A));  -P.y= (V.B,U.B,U.A) *v.a.y-cross (V.A,U.B,U.A) *v.b.y)/(Cross (V.B,U.B,U.A)-Cross (V.A,U.B,U.A));  +             return&p;  -         }   +         returnNULL;  A}

5. Radius and center of Triangle circumscribed Circle

1 Point circle_point (Point a,point b,point C)2     {  3         DoubleA=Dist (B,C); 4         Doubleb=Dist (A,C); 5         DoubleC=Dist (A, b); 6         Doublep= (A+B+C)/2.0; 7         DoubleS=sqrt (p* (p-a) * (p-b) * (P-c)); 8R= (a*b*c)/(4*s);//the radius of the triangular circumscribed circle is R9       Ten         Doublet1= (A.X*A.X+A.Y*A.Y-B.X*B.X-B.Y*B.Y)/2;  One         DoubleT2= (A.X*A.X+A.Y*A.Y-C.X*C.X-C.Y*C.Y)/2;  A Point Center;  -Center.x= (t1* (A.Y-C.Y)-t2* (A.Y-B.Y))/((a.x-b.x) * (A.Y-C.Y)-(a.x-c.x) * (a.y-b.y));  -Center.y= (t1* (a.x-c.x)-t2* (a.x-b.x))/((A.Y-B.Y) * (a.x-c.x)-(A.Y-C.Y) * (a.x-b.x));  the         returnCenter;  -}

6. Rotation jam to find the diameter of the convex hull, that is, the plane farthest point pair, p[] is the convex hull point set

1     DoubleRotating_calipers (Point p[],intN)2     {  3         intK =1; 4         DoubleAns =0; 5P[n] = p[0]; 6          for(intI=0; i<n;i++)  7         {  8              while(Fabs (Cross (p[i],p[i+1],p[k]) < Fabs (Cross (p[i],p[i+1],p[k+1])))  9K = (+ K1) %N; Tenans = max (ans, max (Dist (p[i],p[k)), Dist (p[i+1],p[k]));  One         }   A         returnans;  -}

7. To find the width of the convex bag

1     DoubleRotating_calipers (Point p[],intN)2     {  3         intK =1; 4         DoubleAns =0x7FFFFFFF; 5P[n] = p[0]; 6          for(intI=0; i<n;i++)  7         {  8              while(Fabs (Cross (p[i],p[i+1],p[k]) < Fabs (Cross (p[i],p[i+1],p[k+1])))  9K = (+ K1) %N; Ten             DoubleTMP = Fabs (Cross (p[i],p[i+1],p[k]));  One             DoubleD = Dist (p[i],p[i+1]);  Aans = min (ans,tmp/d);  -         }   -         returnans;  the}

Computational Geometry Fundamentals (templates)

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.