poj 1410 Intersection

1.线段在矩形内->线段的两个断点在矩形内

2.线段一部分在矩形内->线段与矩形的其中至少一条边相交(包括规则相交以及不规则相交)

代码写的略丑,先这样,区域赛完了改。

注意线段不规范相交相交于端点,以及线段重合

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
using namespace std;

const double PI = acos(-1.0);
const double EPS = 1e-10;

//点的定义
struct Point
{
    double x;
    double y;
    Point(double x = 0,double y = 0):x(x),y(y) {}
    Point(const Point& res)
    {
        x = res.x;
        y = res.y;
    }
    friend ostream& operator << (ostream& os,const Point& object)
    {
        os<<object.x<<" "<<object.y;
        return os;
    }
};

//向量的定义
typedef Point Vector;

struct Line
{
    Point p;
    double ang;
    Vector v;
    Line() {}
    Line(Point P,Vector V): p(P),v(V)
    {
        ang = atan2(v.y,v.x);
    }
    Point point(double t)
    {
        return Point(p.x + (v.x)*t , p.y+ (v.y)*t);
    }
    bool operator < (const Line& L) const
    {
        return ang < L.ang;
    }
};

//圆
struct Circle
{
    Point c;
    double r;
    Circle(Point c,double r):c(c),r(r) {}
    Point point(double a)
    {
        return Point(c.x + cos(a)*r , c.y + sin(a)*r);
    }
};

typedef vector<Point> Polygon;

//考虑误差的加法运算
double add(const double& a,const double& b)
{
    if(abs(a + b) < EPS* (abs(a) + abs(b))) return 0;
    return a + b;
}

//向量+向量 = 向量
Vector operator + (Vector A , Vector B)
{
    return Vector( A.x + B.x, A.y + B.y);
}

//点 - 点 = 向量
Vector operator - (Point A , Point B)
{
    return Vector(A.x - B.x , A.y - B.y);
}

//向量*数 = 向量
Vector operator * (Vector A , double p)
{
    return Vector( A.x * p , A.y * p );
}

//向量/数 = 向量
Vector operator / (Vector A , double p)
{
    return Vector( A.x / p, A.y / p );
}

bool operator < (const Point& a,const Point& b)
{
    return  a.x < b.x || ( a.x == b.x && a.y < b.y);
}

int dcmp(double x)
{
    if( fabs(x) < EPS )
    {
        return 0;
    }
    else
    {
        return x < 0 ? -1 : 1 ;
    }
}

bool operator == (const Point& a,const Point& b)
{
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}

//点乘
double Dot(Vector A , Vector B)
{
    return A.x * B.x + A.y * B.y;
}

//向量的长度
double Length (Vector A)
{
    return sqrt(Dot(A,A));
}

//两向量的夹角
double Angle( Vector A , Vector B)
{
    return acos( Dot(A,B) / Length(A) / Length(B));
}

//向量的极角
double Angle(Vector v)
{
    return atan2(v.y,v.x);
}

//角度转化为弧度
double torad(double deg)
{
    return deg/180*PI;
}

//叉乘
double Cross( Vector A , Vector B)
{
    return A.x * B.y - A.y * B.x;
}

//平行四边形的面积
double Area2(Point A,Point B,Point C)
{
    return Cross(B - A , C - A);
}

//向量旋转 ,rad 为逆时针的旋转角
Vector Rotate ( Vector A ,double rad)
{
    return  Vector(A.x * cos(rad) -  A.y * sin(rad) , A.x * sin(rad) + A.y * cos(rad));
}

//旋转90后把长度归一化,化成单位向量
Vector Normal (Vector A)
{
    double L = Length(A);
    return Vector ( -A.y/L , A.x/L );
}

//经纬度(角度)转化为空间坐标
void get_coord(double R,double lat,double lng,double& x,double& y,double& z)
{
    lat = torad(lat);
    lng = torad(lng);
    x = R*cos(lat)*cos(lng);
    y = R*cos(lat)*sin(lng);
    z = R*sin(lat);
}

//直线的交点
Point GetLineIntersection(Point P,Vector v,Point Q, Vector w)
{
    Vector u = P - Q;
    double t = Cross(w,u) / Cross(v,w);
    return P + v*t;
}

//点到直线的距离,用平行四边形的面积除以底
double DistanceToLine(Point P , Point A , Point B)
{
    Vector v1 = B - A;
    Vector v2 = P - A;
    return fabs( Cross(v1,v2) ) / Length(v1);
}

//点到直线的距离.根据Dot判定点的投影是否在线段上
double DistanceToSegment( Point P , Point A , Point B )
{
    if( A == B) return Length( P - A);
    Vector v1 = B - A , v2 = P - A , v3 = P - B;
    if(dcmp(Dot(v1,v2)) < 0 ) return Length(v2);
    else if(dcmp(Dot(v1,v3)) > 0) return Length(v3);
    else return fabs(Cross(v1,v2)) / Length(v1);
}

//点在直线上的投影
Point GetLineProjection(Point P, Point A , Point B)
{
    Vector v = B - A ;
    return A + v*(Dot(v,P - A)/ Dot(v,v));
}

//线段相交判定
//规范相交,两线有一个公共点,且不是端点
//充要条件 每条直线的两个端点都在另一条直线的两侧,叉积符号不同
bool SegmentProperIntersection(Point a1 , Point a2 , Point b1 , Point b2)
{
    double c1 = Cross(a2 - a1 , b1 - a1), c2 = Cross( a2 - a1 ,b2 - a1),
           c3 = Cross( b2 - b1 , a1 - b1), c4 = Cross( b2 - b1 , a2 - b1 );
    return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp( c4 ) < 0;
}

// 判断一个点是否在另外一条线段上
bool OnSegment(Point p , Point a1 , Point a2)
{
    return dcmp( Cross(a1 - p ,a2 - p)) == 0 && dcmp(Dot(a1 - p , a2 - p)) < 0;
}

//计算多边形的有向面积,p[0]划分
double PolygonArea(Point* p,int n)
{
    double area = 0;
    for(int i = 1 ; i < n - 1 ; i++)
    {
        area += Cross(p[i] - p[0] , p[i+1] - p[0] );
    }
    return area/2;
}

//直线和圆的交点
int getLineCircleIntersection(Line L , Circle C ,double& t1, double& t2, vector<Point>& sol)
{
    double a = L.v.x , b = L.p.x - C.c.x , c = L.v.y , d = L.p.y - C.c.y;
    double e = a*a + c * c , f = 2*(a*b + c*d) , g = b*b + d*d -C.r*C.r;
    double delta = f*f - 4*e*g;
    if(dcmp(delta) < 0) return 0; //相离
    if(dcmp(delta) == 0)
    {
        t1 = t2 = - f/( 2 * e);
        sol.push_back(L.point(t1));
        return 1;
    }
    t1 = ( -f - sqrt(delta)) / ( 2 * e);
    t2 = ( -f + sqrt(delta)) / ( 2 * e);
    sol.push_back(L.point(t1));
    sol.push_back(L.point(t2));
    return 2;
}

//两圆相交
int getCircleIntersection(Circle C1, Circle C2 ,vector<Point>& sol)
{
    double d = Length(C1.c - C2.c);
    if(dcmp(d) == 0)
    {
        if(dcmp(C1.r - C2.r) == 0) return -1;//两圆重合
        return 0;
    }
    if( dcmp( C1.r + C2.r - d) <0) return 0;
    if(dcmp(fabs(C1.r - C2.r) - d ) > 0) return 0;

    double a = Angle(C2.c - C1.c);
    double da = acos((C1.r*C1.r + d*d - C2.r*C2.r) / (2*C1.r*d));
    Point p1 = C1.point(a-da);
    Point p2 = C2.point(a+da);
    sol.push_back(p1);
    if(p1 == p2) return 1;
    sol.push_back(p2);
    return 2;
}

//过定点做圆的切线
//过点p到圆c的切线,v[i]是第i条切线的向量,返回切线的条数
int getTangents(Point p,Circle C , Vector* v)
{
    Vector u = C.c - p;
    double dist = Length(u);
    if(dist < C.r) return 0;
    else if(dcmp(dist - C.r) == 0)
    {
        v[0] = Rotate(u,PI/2);
        return 1;
    }
    else
    {
        double ang = asin(C.r / dist );
        v[0] = Rotate(u,-ang);
        v[1] = Rotate(u,+ang);
        return 2;
    }
}

//两圆的公切线
int getTangents(Circle A , Circle B , vector<Point>& a, vector<Point>& b)
{
    int cnt = 0;
    if(A.r  <  B.r)
    {
        swap(A,B);
        swap(a,b);
    }
    int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
    int rdiff = A.r - B.r;
    int rsum = A.r + B.r;
    if(d2 < rdiff*rdiff) return 0;

    double base = Angle(B.c - A.c);
    if(d2 == 0 && A.r == B.r ) return -1;
    if(d2 == rdiff * rdiff)
    {
        a.push_back(A.point(base));
        b.push_back(B.point(base));
        cnt++;
        return 1;
    }
    double ang = acos((A.r - B.r) / sqrt((double)d2)) ;
    a.push_back(A.point(base + ang));
    b.push_back(B.point(base + ang));
    cnt++;
    a.push_back(A.point(base - ang));
    b.push_back(B.point(base - ang));
    cnt++;
    if(d2 == rsum*rsum)
    {
        a.push_back(A.point(base));
        b.push_back(B.point(base + PI));
        cnt++;
    }
    else if(d2 > rsum*rsum)
    {
        double ang = acos((A.r + B.r) / sqrt((double)d2));
        a.push_back(base + ang);
        b.push_back(base + ang + PI);
        cnt++;
        a.push_back(base - ang);
        b.push_back(PI + base - ang);
        cnt++;
    }
    return cnt;
}

//求三角形的外接圆
Circle CircumscribedCircle(Point p1 , Point p2 , Point p3)
{
    double Bx = p2.x - p1.x , By = p2.y - p1.y;
    double Cx = p3.x - p1.x , Cy = p3.y - p1.y;
    double D = 2*(Bx*Cy - By*Cx);
    double cx = (Cy*(Bx*Bx + By*By) - By*(Cx*Cx + Cy*Cy))/D + p1.x;
    double cy = (Bx*(Cx*Cx + Cy*Cy) - Cx*(Bx*Bx + By*By))/D + p1.y;
    Point p = Point(cx,cy);
    return Circle(p,Length(p1-p));
}

//求三角形的内切圆
Circle InscribedCircle(Point p1 , Point p2 , Point p3)
{
    double a = Length(p2 - p3);
    double b = Length(p3 - p1);
    double c = Length(p1 - p2);
    Point p = (p1 * a + p2 * b + p3*c)/(a+b+c);
    return Circle(p,DistanceToLine(p,p1,p2));
}

//点在多边形内的判定
int isPointInPolygon(Point p,Polygon poly)
{
    int wn = 0;
    int n = poly.size();
    for(int i = 0 ; i < n ; i++)
    {
        if(OnSegment(p , poly[i] , poly[(i+1)%n])) return -1;//在边界上
        int k = dcmp(Cross(poly[(i+1)%n] - poly[i] , p - poly[i]));
        int d1 = dcmp(poly[i].y - p.y);
        int d2 = dcmp(poly[(i+1)%n].y - p.y);
        if( k > 0 && d1 <= 0 && d2 > 0) wn++;
        if( k < 0 && d2 <= 0 && d1 > 0) wn--;
    }
    if(wn != 0) return 1;//内部
    return 0;//外部
}

//计算凸包
int ConvexHull(Point* p , int n,Point* ch)
{
    sort(p,p+n);
    int m = 0 ;
    for(int i = 0 ; i < n ; i++)
    {
        while( m > 1 && Cross(ch[m-1] - ch[m-2] , p[i] - ch[m-2]) <= 0 ) m--;
        ch[m++] = p[i];
    }
    int k = m;
    for(int i = n -2 ; i >= 0 ; i --)
    {
        while(m > k && Cross(ch[m-1] - ch[m-2] , p[i] - ch[m-2] ) <= 0) m--;
        ch[m++] = p[i];
    }
    if(n > 1) m--;
    return m;
}

int main()
{
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif // LOCAL
    int n;
    scanf("%d",&n);
    while(n--)
    {
        Point Start,End;
        Point LeftTop,RightBottom;
        Point LeftBottom,RightTop;
        scanf("%lf %lf",&Start.x,&Start.y);
        scanf("%lf %lf",&End.x,&End.y);
        scanf("%lf %lf",&LeftTop.x,&LeftTop.y);
        scanf("%lf %lf",&RightBottom.x,&RightBottom.y);
        if(LeftTop.x > RightBottom.x )
        {
            swap(LeftTop.x,RightBottom.x);
        }
        if(RightBottom.y > LeftTop.y)
        {
            swap(RightBottom.y,LeftTop.y);
        }
        LeftBottom.x = LeftTop.x;
        LeftBottom.y = RightBottom.y;
        RightTop.x = RightBottom.x;
        RightTop.y = LeftTop.y;
        bool flag = false;
        if(SegmentProperIntersection(LeftBottom,LeftTop,Start,End) || OnSegment(Start,LeftBottom,LeftTop) || OnSegment(End,LeftBottom,LeftTop))
        {
            flag = true;
        }
        if(SegmentProperIntersection(RightBottom,RightTop,Start,End) || OnSegment(Start,RightBottom,RightTop) || OnSegment(End,RightBottom,RightTop))
        {
            flag = true;
        }
        if(SegmentProperIntersection(LeftBottom,RightBottom,Start,End) || OnSegment(Start,LeftBottom,RightBottom) || OnSegment(End,LeftBottom,RightBottom))
        {
            flag = true;
        }
        if(SegmentProperIntersection(LeftTop,RightTop,Start,End) || OnSegment(Start,LeftTop,RightTop) || OnSegment(End,LeftTop,RightTop) )
        {
            flag = true;
        }
        if(( Start.x >= LeftBottom.x && Start.x <= RightBottom.x && Start.y >= LeftBottom.y && Start.y <= LeftTop.y) && (End.x >= LeftBottom.x && End.x <= RightBottom.x && End.y >= LeftBottom.y && End.y <= LeftTop.y) )
        {
            flag = true;
        }
        if(OnSegment(LeftBottom,Start,End) || OnSegment(LeftTop,Start,End) || OnSegment(RightBottom,Start,End) || OnSegment(RightTop,Start,End))
        {
            flag = true;
        }
        if(flag)
        {
            printf("T\n");
        }
        else
        {
            printf("F\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值