Computer College Undergraduate Program Design Contest (2015 ') Polygon

Source: Internet
Author: User
Polygon Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 195 accepted Submission (s): 41


Problem Description give you a simple polygon and a line, and your task are to calculate the length of the line which is Co Vered by the polygon.
Input There are several test cases.
Each test case starts with one non-negative integer n (3 <= n <= 1000) giving the number of the vertices of the poly Gon. Then following n lines, each line contains two real numbers standing for the x and y coordinates of a vertex. The vertices are given either in clockwise or counterclockwise order. Then four real numbers (SX, SY, TX, Ty), standing for the coordinates of the two points of the.
All real numbers are between-100000 and 100000.
Output for each case print the "total length" of the line this covered by the polygon, with 3 digits on the decimal point .

Sample Input
The 4 0 0 0 1 1 1 1 0 0 0 1 1 4 0 0 0 1--------------+ 1 1 1 0 0, 0
Sample Output
1.414 1.000 6.000


always looking at once space in a daze, those who say not separate friends, turn around, strangers. Familiar, quiet, quiet, left, left, unfamiliar, unfamiliar, disappeared, disappeared, strangers.


#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <
Algorithm> using namespace std;
    struct point {double x;
Double y;
} p[1001], px[10001];
int n;
Double eps=1e-8;
    int CMP (point A, point B) {if (ABS (a.x-b.x) <eps) return a.y<b.y;
Return a.x<b.x; Double Dist (point A,point b) {return sqrt (a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y)), double direction (point Pi,p Oint pj,point PK) {return (pk.x-pi.x) * (PJ.Y-PI.Y)-(pj.x-pi.x) * (PK.Y-PI.Y);} bool On_segment (Point Pi,point pj,point PK) {if (Direction (pi, PJ, pk) ==0) {if (Pk.x>=min (pi.x,pj.x) &&pk.x<=max (pi.x,pj.x) &&
    Pk.y>=min (PI.Y,PJ.Y) &&pk.y<=max (PI.Y,PJ.Y)) return true;
return false;
    BOOL Segments_intersect (Point P1,point p2,point p3,point p4) {double d1=direction (P3,P4,P1);
    Double d2=direction (P3,P4,P2);
    Double d3=direction (P1,P2,P3); Double D4=directiOn (P1,P2,P4);
    if (d1*d2<0&&d3*d4<0) return true;
    else if (d1==0&&on_segment (P3,P4,P1)) return true;
    else if (d2==0&&on_segment (P3,P4,P2)) return true;
    else if (d3==0&&on_segment (P1,P2,P3)) return true;
    else if (d4==0&&on_segment (P1,P2,P4)) return true;
return false;
    Point intersection (Point A1, Point A2, point B1, point B2) {point ret = A1;  Double T = ((a1.x-b1.x) * (B1.Y-B2.Y)-(A1.Y-B1.Y) * (b1.x-b2.x))/((a1.x-a2.x) * (B1.Y-B2.Y)
    -(A1.Y-A2.Y) * (b1.x-b2.x));
    Ret.x + + (a2.x-a1.x) * t;
    Ret.y + + (a2.y-a1.y) * t;
return ret;
    int Inpolygon (point a) {int i;
    Point B,c,d;
    B.Y=A.Y;
    B.X=1E15;
    int count=0;
        For (i=0 i<n; i++) {c = p[i];
        D = p[i + 1];
        if (On_segment (C,d,a)) return 1;
        if (ABS (C.Y-D.Y) <eps) continue; if (On_segment (a,b,c) {if (c.y>d.y) count++;
        else if (on_segment (a,b,d)) {if (d.y>c.y) count++;
    else if (Segments_intersect (a,b,c,d)) count++;
return count%2; BOOL Intersect (point s,point e,point a,point b) {return direction (e,a,s) *direction (e,b,s) <=0;} double Calculate
(Point S,point E)
    {int i,k=0;
    Double sum;
    Point A,b,temp;
        for (i=0; i<n; i++) {a=p[i];
        B=P[I+1];
            if (ABS (direction (e,a,s)) <eps&&abs (direction (e,b,s)) <eps) {px[k++]=a;
        Px[k++]=b;
        else if (Intersect (s,e,a,b)) {px[k++]=intersection (s,e,a,b);
    } if (k==0) return 0.0;
    Sort (px,px+k,cmp);
    PX[K]=PX[0];
    sum=0;
        for (i=0; i<k-1; i++) {a=px[i];
        B=PX[I+1];
        temp.x= (a.x+b.x)/2.0;
   temp.y= (A.Y+B.Y)/2.0;     if (Inpolygon (temp)) sum+=dist (A,B);
return sum;
    int main () {int i;
    Double sum;
    Point S,e; while (~SCANF ("%d", &n) &&n) {for (i=0; i<n; i++) scanf ("%lf%lf", &p[i].x,&p[i)
        . y);
        P[N]=P[0];
        scanf ("%lf%lf%lf%lf", &s.x,&s.y,&e.x,&e.y);
        Sum=calculate (s,e);
    printf ("%.3lf\n", sum);
return 0;
 }

@ hold read "@☆ but beg" ❤ "Ann ★ Next time we do will be better ....
for what this time the topic is in English .... Qaq ...

Related Article

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.