NYOJ 1132 promise me a medal (判断两线段是否相交)

promise me a medal

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 2
描述

  you all know that creat2012 will go to a regional. and i want you all pray for us. thx.

  for this problem, you need judge if two segments is intersection(相交的). if intersect(相交), print yes and the point. else print no.

  easy ? ac it.

输入
T <= 100 cases
8 real numbers (double)
promise two segments is no coincidence.
输出
no
or yes and two real number (one decimal)
样例输入
2
0 0 2 2 1 0 3 2
0 0 2 2 0 2 2 0
样例输出
no
yes 1.0 1.0


坑点在于首尾相连这一情况

思路:先判断线段是否相交,然后在求交点

ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define MAXN 201000
#define MAX(a,b) a>b?a:b
#define fab(a) ((a)>0?(a):-(a))
using namespace std;
struct s
{
	double x;
	double y;
}fb,fe,sb,se,r;
double a,b,c,a1,b1,c1,a2,b2,c2;
void fun(s p,s q)
{
    a=q.y-p.y;
    b=p.x-q.x;
    c=q.x*p.y-p.x*q.y;
}
int find(s p1,s p2,s q1,s q2)
{
    double m,n;
    fun(p2,q2);
    a2=a,b2=b,c2=-c;
    m=a*p1.x+b*p1.y+c;
    n=a*q1.x+b*q1.y+c;
    if(m*n>0)
	return 0;
    if(fab(m)<1e-8&&fab(n)<1e-8)
    {
        if(fab(b)<1e-8)
        {
            if(((p1.y<min(p2.y,q2.y)&&q1.y<min(p2.y,q2.y))||(p1.y>max(p2.y,q2.y)&&q1.y>max(p2.y,q2.y))))
			return 0;
            else
            {
                if(p1.y==p2.y||p1.y==q2.y)
				r.x=p1.x,r.y=p1.y;
                else
				r.x=q1.x,r.y=q1.y;
                return 1;
            }
        }
        if(((p1.x<min(p2.x,q2.x)&&q1.x<min(p2.x,q2.x))||(p1.x>max(p2.x,q2.x)&&q1.x>max(p2.x,q2.x))))
		return 0;
        else
        {
            if(p1.x==p2.x||p1.x==q2.x)
			r.x=p1.x,r.y=p1.y;
            else
			r.x=q1.x,r.y=q1.y;
            return 1;
        }
    }
    fun(p1,q1);
    a1=a,b1=b,c1=-c;
    m=a*p2.x+b*p2.y+c;
    n=a*q2.x+b*q2.y+c;
    if(m*n>0)
	return 0;
    r.x=(c1*b2-c2*b1)/(a1*b2-a2*b1);
    r.y=(a1*c2-a2*c1)/(a1*b2-a2*b1);
    return 1;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&fb.x,&fb.y,&fe.x,&fe.y,&sb.x,&sb.y,&se.x,&se.y);
		if(find(fb,sb,fe,se))
		printf("yes %.1lf %.1lf\n",r.x,r.y);
		else
		printf("no\n");
	}
	return 0;
}



你可能感兴趣的:(NYOJ 1132 promise me a medal (判断两线段是否相交))