计算几何入门

poj 3304

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
using namespace std;
const int maxn=1e6+10;
const double EXP=1e-9;
struct point  { double x,y;};
struct segment{ point a,b; }pa[maxn];
struct line   { point a,b; line(point x,point y){ a=x,b=y; };   };;
double XXX(point a,point b,point c)
{
    return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
}
bool check_x(line a,segment b)
{
    point x=a.a;
    point y=a.b;
    point c=b.a;
    point d=b.b;
    if( XXX(c,x,y)*XXX(d,x,y)<=0 ) return 1;
    return 0;
}
bool check_f(line a,int n)
{
    //<<"n"<<endl;
    point c=a.a;
    point d=a.b;
    if( fabs( sqrt( (c.x-d.x)*(c.x-d.x)+(c.y-d.y)*(c.y-d.y) ) )<EXP ) return false;
   // cout<<1<<endl;
    for(int i=1;i<=n;i++)  if( check_x(a,pa[i])==0 ) return false;
    return true;
}
int main()
{
    int T; scanf("%d",&T);
    while(T--)
    {
        int n; scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            point a;point b;
            scanf("%lf %lf %lf %lf",&a.x,&a.y,&b.x,&b.y);
            pa[i]={a,b};
        }
        bool flag=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {

                 if(check_f(line(pa[i].a,pa[j].a),n)) { flag=1; break;}
                 if(check_f(line(pa[i].a,pa[j].b),n)) { flag=1; break;}
                 if(check_f(line(pa[i].b,pa[j].a),n)) { flag=1; break;}
                 if(check_f(line(pa[i].b,pa[j].b),n)) { flag=1; break;}
            }
        }
        if(flag==1) printf("Yes!
");
        else        printf("No!
");
    }
}

poj 2398

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1e6+10;
const double EXP=1e-9;
struct point{double x,y;};
struct segment{ point a,b; }pa[maxn];
int n,m;
int num[maxn];
int d[maxn];
void make(point s)
{
    int l=0;
    int r=n+1;
    while(l+1!=r)
    {
        int mid=(l+r)/2;
        point a=pa[mid].a;
        point b=pa[mid].b;
        double x1=a.x-s.x;
        double y1=a.y-s.y;
        double x2=b.x-s.x;
        double y2=b.y-s.y;
        if(x1*y2-x2*y1>EXP) r=mid;
        else              l=mid;
    }
    //cout<<r<<endl;
    num[r]++;
    //cout<<r<<endl;
    return;
}
bool up(segment a,segment b)
{
    return a.a.x<b.a.x;
}
int main()
{
  //  ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    while(1)
    {
        scanf("%d",&n); if(n==0) break;
        scanf("%d",&m);
        memset(num,0,sizeof(num));
        memset(d,0,sizeof(d));
        double x1,y1,x2,y2; scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
        pa[0]={{x1,y2},{x1,y1}};
        pa[n+1]={ {x2,y2},{x2,y1}};
        for(int i=1;i<=n;i++)  { double a,b;scanf("%lf %lf",&a,&b); pa[i]={ {b,y2},{a,y1}};  }
        sort(pa,pa+n+1,up);
        for(int i=1;i<=m;i++)  { double a,b;scanf("%lf %lf",&a,&b); point x={a,b}; make(x);  }
        for(int i=1;i<=n+1;i++){ d[num[i]]++;  }
        printf("Box
");
        for(int i=1;i<=m;i++)
        {
            if(d[i]) printf("%d: %d
",i,d[i]);
        }

    }
    return 0;
}
原文地址:https://www.cnblogs.com/Andromeda-Galaxy/p/10446311.html