POJ 2653 線段相交判斷 (叉積)

題目鏈接http://poj.org/problem?id=2653


題解:線段相交判斷 叉積基本運用


#include<cstdio>
#include<cstring>
#include<cmath>
#define N 110000
bool ans[N];
//定義點
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0) : x(x), y(y) {}
};

struct segment
{
    Point a, b;
}seg[N];

typedef Point Vector; //Vector 爲 Point的別名


//向量+向量=向量    點+向量=點
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);
}


const double eps = 1e-10;
int dcmp(double x)
{
	if(fabs(x) < eps) return 0;
	else return x < 0 ? -1 : 1;
}

//點積:兩者長度乘積再乘上夾角餘弦 XaXb + YaYb
double Dot(Vector A, Vector B)
{
	return A.x*B.x + A.y*B.y;
}

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


//線段相交判定(原理:大白書258)

//每條線段的兩個端點都在另一條線段的兩側
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2)//a,b線段
{
	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;
}

int main ()
{
    int n;
    while(scanf("%d", &n), n)
    {
        memset(ans, 0, sizeof(ans));

        for(int i = 1; i <= n ;i++)
            scanf("%lf %lf %lf %lf", &seg[i].a.x, &seg[i].a.y, &seg[i].b.x, &seg[i].b.y);

        for(int i = 1; i <= n; i++)
            for(int j = i+1; j <= n; j++)
            {
                if(SegmentProperIntersection(seg[i].a, seg[i].b, seg[j].a, seg[j].b))
                {
                    ans[i] = 1;
                    break;
                }
            }


          int flag = 0;
          printf("Top sticks:");
          for(int i = 1; i <= n; i++)
            if(ans[i] == 0)
                {
                    if(flag == 0)
                        flag = 1;
                    else
                        printf(",");
                    printf(" %d", i);

                }
          printf(".\n");

    }

    return 0;
}




發佈了121 篇原創文章 · 獲贊 10 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章