HDU 1154 poj 2462 cutting a polygon computational ry

Source: Internet
Author: User

Good Harvest: Line Segments and straight lines are more skillful, and some details are also improved.

It gives you a polygon, a straight line. Note that it is a straight line that determines the sum of the length of this line inside the polygon.

It is clear that all the intersections are sorted in one direction. The line segments between two adjacent points must be either within the polygon or not within the polygon.

You only need to judge that the midpoint between two points is not within the polygon.

It is worth noting that when you calculate the intersection of a straight line and a line segment, You can first determine whether a line can interwork with a line segment, and then calculate the intersection, which can save a lot of computations. Otherwise, it may time out.

Show me a glimpseCode

View code

# Include <math. h>
# Include <cstdio>
# Include <algorithm>
Using Namespace STD;
Const Int Maxn = 100010 ;
Const Double EPS = 1E- 8 ;
Inline Int Max ( Int X, Int Y ){ Return X> Y? X: Y ;}
Inline Double Max ( Double X, Double Y ){ Return X> Y? X: Y ;}
InlineDouble 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 Operator + ( Const Point & T) Const {
Point TMP;
TMP. x = x + T. X;
TMP. Y = Y + T. Y;
Return TMP;
}
Bool Operator = ( Const Point & T) Const {
Return SGN (x-t.x) = 0 & SGN (y-t.y) = 0 ;
}
} P [maxn], Set [Maxn];
Struct SEG {point S, E ;};
Struct Line { Double A, B, C ;};
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 ));}
Bool CMP (point a, point B ){ Return (A. x <B. x | FABS (A. x-b.x) <EPS & 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 );}
// Determine line collinearity
Bool Chonghe (point a, point B, point C, point D ){ Return ! SGN (Cross (a, B, c ))&&! SGN (Cross (A, B, D ));}
// Determine whether the line A and B intersect with the C and D segments
Bool Inter (point a, point B, point C, point D ){ Return SGN (Cross (a, B, c) * SGN (Cross (a, B, d) <= 0 ;}
Bool Dotonseg (point P, point S, point E ){
If (P = S | P = E) Return True ;
Return SGN (Cross (S, E, p) = 0 & SGN (P. x-s.x) * (p. x-e.x) <= 0 & SGN (P. y-s.y) * (p. y-e.y) <= 0 ;
}
Line turn (point S, point E ){
Line ln;
Ln. A = S. Y-E. Y;
Ln. B = E. X-S. X;
Ln. c = S. x * E. Y-E. x * S. Y;
Return Ln;
}
Bool 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 True ;
}
Bool Point_in_polygon (point O, point * P, Int N ){
Int I, T;
Point A, B;
P [N] = P [ 0 ]; T = 0 ;
For (I = 0 ; I <n; I ++ ){
If (Dotonseg (O, P [I], p [I + 1 ]) Return True ;
A = P [I]; B = P [I + 1 ];
If (A. Y> B. Y ){
Point TMP = A; A = B; B = TMP;
}
If (Cross (O, a, B) <-EPS & A. Y <O. Y-EPS & O. Y <B. Y + EPS)
T ++;
}
Return T & 1 ;
}
Int Tot, n, m;
Double Solve (){
Double Ans =0 ;
Sort ( Set + 1 , Set + Tot + 1 , CMP );
TOT = unique ( Set + 1 , Set + 1 + ToT )- Set - 1 ;
For (Int I = 1 ; I <tot; I ++ ){
Point TMP;
TMP. x = ( Set [I] + Set [I + 1 ]). X/ 2 ; TMP. Y = ( Set [I] + Set [I + 1 ]). Y/ 2 ;
Bool Yes = point_in_polygon (TMP, P, N );
If (Yes) ans + = dist ( Set [I], Set [I + 1 ]);
}
Return Ans;
}
Int Main (){
Int I, J;
While (Scanf ( " % D " , & N, & M )){
If (N = 0 & M = 0 ) Break ;
For (I = 0 ; I <n; I ++) scanf ( " % Lf " , & P [I]. X, & P [I]. Y); P [N] = P [ 0 ];
While (M --){
SEG seg; Tot = 0 ;
Scanf ( " % Lf " , & Seg. S. X, & Seg. S. Y, & Seg. E. X, & Seg. E. y );
For (J = 0 ; J <n; j ++ ){
Point pp;
If (Chonghe (SEG. S, SEG. E, P [J], p [J + 1 ]) Set [++ Tot] = P [J], Set [++ Tot] = P [J + 1 ];
If (Inter (SEG. S, SEG. E, P [J], p [J + 1 ])
If (Intersect (SEG. S, SEG. E, P [J], p [J + 1 ], Pp )) Set [++ Tot] = pp;
}
If (TOT = 0 )
{
Printf ( " 0.000 \ n " );
Continue ;
}
Set [++ Tot] = Seg. S; Set [++ Tot] = Seg. E;
Printf ( " %. 3lf \ n " , Solve ());
}
}
Return 0 ;
}
/*
4 9
0 0
0 1
1 1
1 0
0 0 1 1
1 1 0 0
0 0 1 0
0 0 0.5 0
0 0.5 1 0.5
0 1 1 1
1 1 1 0
0.75 0.75 0.75 0.25
0 0.25 1 0.75
*/

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.