Poj 2653 pick-up sticks line segment intersection Problem

Source: Internet
Author: User
# Include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> # include <cmath> # include <algorithm> # include <queue> using namespace STD; # define INF 0x3f3f3f # define EPS 1e-8const int maxn = 100000 + 3; struct point {Double X; Double Y ;}; struct line {Point U; point v ;} stick [maxn]; int visit [1000]; int sig (Double X) {return (x> EPS)-(x <-EPS);} double mult (point P0, point P1, point P2) {return (p1.x-converted X) * (p2.y-converted y)-(p2.x-converted X) * (p1.y-converted y);} int line_in_line (line L1, line l2) // intersection of line segments (including intersections) {If (SIG (mult (l1.u, l2.u, l1.v) * sig (mult (l1.u, l2.v, l1.v )) <= 0 & sig (mult (l2.u, l1.u, l2.v) * sig (mult (l2.u, l1.v, l2.v) <= 0) return 1; return 0 ;} int main () {# ifndef online_judge freopen ("in", "r", stdin); # endif int N, top; while (scanf ("% d", & N) & N) {scanf ("% lf", & Stick [1]. u. x, & Stick [1]. u. y, & Stick [1]. v. x, & Stick [1]. v. y); visit [Top = 1] = 1; for (INT I = 2; I <= N; I ++) {scanf ("% lf", & Stick [I]. u. x, & Stick [I]. u. y, & Stick [I]. v. x, & Stick [I]. v. y); For (Int J = 1; j <= top; j ++) {If (line_in_line (stick [I], stick [visit [J]) {visit [J] =-1 ;}} int cur = 0; For (Int J = 1; j <= top; j ++) {If (visit [J]> 0) visit [++ cur] = visit [J];} Top = cur; visit [++ top] = I ;} printf ("top sticks:"); For (INT I = 1; I <top; I ++) {printf ("% d,", visit [I]);} printf ("% d. \ n ", visit [Top]);}

The line_in_line () function can also be written as follows:

Int line_in_line (line L1, line l2) // intersection of line segments (including intersections) {// return (max (l1.u. x, l1.v. x)> = min (l2.u. x, l2.v. x) & MAX (l2.u. x, l2.v. x)> = min (l1.u. x, l1.v. x) & MAX (l1.u. y, l1.v. y)> = min (l2.u. y, l2.v. y) & MAX (l2.u. y, l2.v. y)> = min (l1.u. y, l1.v. y) & mult (l1.u, l2.v, l2.u) * mult (l2.v, l1.v, l2.u)> = 0 & mult (l2.u, l1.v, l1.u) * mult (l1.v, l2.v, l1.u)> = 0 );}

I wonder why I wrote it out.

Code. I used visit to judge whether it was optimized. I would like to give a reply:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <queue>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8const int maxn = 100000+3;struct Point{    double x;    double y;};struct Line{    Point u;    Point v;}stick[maxn];bool visit[maxn];int Sig(double x){    return (x>eps) - (x<-eps);}double Mult(Point p0, Point p1, Point p2){    return (p1.x-p0.x)*(p2.y-p0.y) - (p2.x-p0.x)*(p1.y-p0.y);}int line_in_line(Line L1, Line L2){    if(Sig(Mult(L1.u,L2.u,L1.v))*Sig(Mult(L1.u,L2.v,L1.v))<=0 && Sig(Mult(L2.u,L1.u,L2.v))*Sig(Mult(L2.u,L1.v,L2.v)) <=0)        return 1;    return 0;}int main(){#ifndef ONLINE_JUDGE    freopen("in","r",stdin);#endif    int n;    while(scanf("%d",&n) && n)    {        memset(visit, false, sizeof(visit));        for(int i = 1; i <= n; i++)        {            scanf("%lf%lf%lf%lf",&stick[i].u.x,&stick[i].u.y,&stick[i].v.x,&stick[i].v.y);            for(int j = 1; j < i; j++)            {                if(!visit[j] && line_in_line(stick[i],stick[j]))                    visit[j] = true;            }        }        printf("Top sticks:");        for(int i = 1; i< n; i++)        {            if(!visit[i])                printf(" %d,",i);        }        printf(" %d.\n",n);    }}

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.