Poj 1514 Metal Cutting semi-flat cross plane cutting

Source: Internet
Author: User

Half plane intersection template O (N ^ 2) in http://www.cnblogs.com/wuyiqi/archive/2012/03/30/2426175.html

Question: Judge the sum of the shortest cut marks of the convex polygon required to be cut out on a square.

There are only eight sides in total, and the data size is small. The time limit is 10 s. The order of brute force enumeration and cutting is calculated.

This is my first contact with plane cutting. It can be called a virgin cutting.

View code

# Include <cmath>
# Include <cstdio>
# Include <algorithm>
Using Namespace STD;
Const Int Maxn = 100 ;
Const Double EPS = 1E- 8 ;
Inline Double Min ( Double A, Double B ){ Return A <B? A: B ;}
Inline Double SGN ( Double X ){ Return FABS (x) <EPS? 0 :( X> 0 ? 1 :- 1 );}
Struct Point {
Double X, Y;
Point ( Double Tx = 0 , Double Ty = 0 ) {X = TX; y = ty ;}
Bool Operator = ( Const Point & T) Const {
Return SGN (x-t.x) =0 & SGN (y-t.y) = 0 ;
}
} P [maxn], set [maxn], St [maxn], TMP [maxn], PP [maxn];
Struct SEG {point S, E ;};
Inline Double Dist (point a, point B ){ Return SQRT (A. x-b.x) * (A. x-b.x) + (A. y-b.y) * (A. y-b.y ));}
Inline Double Cross (point a, point B, point C ){ Return (B. x-a.x) * (C. y-a.y)-(B. y-a.y) * (C. x-a.x );}
Inline Bool Outside (SEG seg, point P ){ Return Cross (SEG. S, SEG. E, P)> EPS ;}
Inline Bool Inside (SEG seg, point P ){ Return Cross (SEG. S, SEG. E, P) <-EPS ;}
Point intersect (point P1, point P2, point P3, point P4, point & P ){
Double A1, B1, C1, A2, B2, C2, D;
A1 = p1.y-p2.y; b1 = p2.x-p1.x; C1 = p1.x * p2.y-p2.x * p1.y;
A2 = p3.y-p4.y; b2 = p4.x-p3.x; C2 = p3.x * p4.y-p4.x * p3.y;
D = A1 * B2-A2 * B1;
If (FABS (d) <EPS)Return False ;
P. x = (-C1 * B2 + C2 * B1)/d;
P. Y = (-A1 * C2 + A2 * C1)/d;
Return P;
}
Double W, h;
Int A [ 10 ], N, PN;
Double Cut (SEG seg, point P []) {
Int I, j, TOT = 0 ;
Point A, B;
A = B = point ( 0 , 0 );
Bool S, E;
For (I = 0 ; I <PN; I ++ ){
If (! Outside (SEG, P [I]) PP [tot ++] = P [I];
Else {
If (I = 0 &&! Outside (SEG, P [pn- 1 ]) {B =;
PP [tot ++] = intersect (SEG. S, SEG. E, P [I], p [pn-1 ], );
}
If (I! = 0 &&! Outside (SEG, P [I- 1 ]) {B =;
PP [tot ++] = intersect (SEG. S, SEG. E, P [I], p [I- 1 ], );
}
If (! Outside (SEG, P [I + 1 ]) {
B =;
PP [tot ++] = intersect (SEG. S, SEG. E, P [I], p [I + 1 ], );
}
}
}
PP [tot] = PP [ 0 ]; Pn = tot;
Memcpy (St, PP, Sizeof (Pp ));
Return Dist (A, B );
}
Int Main (){
Int I;
While (Scanf ( " % Lf " , & W, & H )! = EOF ){
Double Ans = 1e20;
St [ 4 ] = ST [ 0 ] = Point ( 0 , 0 );
St [ 1 ] = Point ( 0 , H );
St [ 2 ] = Point (W, H );
St [ 3 ] = Point (W, 0 );
Memcpy (TMP, St,Sizeof (St ));
Scanf ( " % D " , & N); seg ts [ 100 ];
For (I = 0 ; I <n; I ++ ){
Scanf ( " % Lf " , & P [I]. X, & P [I]. y );
A [I] = I;
} P [N] = P [ 0 ];
For (I = 0 ; I <n; I ++) ts [I]. S = P [I], TS [I]. E = P [I + 1 ];
Do {
Double Tlen = 0 ;
Memcpy (St, TMP, Sizeof (TMP ));
Pn = 4 ;
For (I = 0 ; I <n; I ++ ){
Tlen + = cut (TS [A [I], St );
}
Ans = min (ANS, tlen );
} While (Next_permutation (A, A + n ));
Printf ( " Minimum total length = %. 3lf \ n " , ANS );
}
Return 0 ;
}

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.