Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:终于过了,记得特判一条线段完全覆盖另一条的情况,附AC代码

Posted by dgzxzyf at 2022-08-12 11:07:08 on Problem 2826
In Reply To:WC,找了好几个AC代码对拍还是WA,艹 Posted by:dgzxzyf at 2022-08-12 10:41:57
#include<iostream>
#include<cstdio>
#include<cmath>
#define pdd pair<double,double>
#define x first
#define y second
using namespace std;
const double eps=1e-8;
int t;
double Min(double a,double b)
{
	return a<b?a:b;
}
double Max(double a,double b)
{
	return a>b?a:b;
}
double EPS(double res)
{
	if(fabs(res)<eps) return 0;
	else return res;
}
bool cross(pdd a,pdd b,pdd c,pdd d)
{
	if(Min(a.x,b.x)>Max(c.x,d.x)||Min(a.y,b.y)>=Max(c.y,d.y)||Min(c.x,d.x)>Max(a.x,b.x)||Min(c.y,d.y)>=Max(a.y,b.y)) return 0;
	else if(EPS(((b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x))*((b.x-a.x)*(d.y-a.y)-(b.y-a.y)*(d.x-a.x)))<=0&&EPS(((d.x-c.x)*(a.y-c.y)-(d.y-c.y)*(a.x-c.x))*((d.x-c.x)*(b.y-c.y)-(d.y-c.y)*(b.x-c.x)))<=0) return 1;
	return 0;
}
pdd a,b,c,d,e;
double k1,k2,b1,b2;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y);
		scanf("%lf%lf%lf%lf",&c.x,&c.y,&d.x,&d.y);
		if(b.y<a.y) swap(a,b);
		if(d.y<c.y) swap(c,d);
		if(b.y>d.y)
		{
			swap(a,c);
			swap(b,d);
		}
		if(!cross(a,b,c,d)||a.y==b.y||c.y==d.y) printf("0.00\n");
		else if(b.x==a.x)
		{
			k2=(d.y-c.y)/(d.x-c.x),b2=(d.x*c.y-d.y*c.x)/(d.x-c.x);
			e.x=a.x,e.y=k2*e.x+b2;
			if(EPS(e.y-b.y)==0) printf("0.00\n");
			else printf("%.2lf\n",fabs((b.y-b2)/k2-b.x)*(b.y-e.y)/2);
		}
		else if(c.x==d.x)
		{
			k1=(b.y-a.y)/(b.x-a.x),b1=(b.x*a.y-a.x*b.y)/(b.x-a.x);
			e.x=c.x,e.y=k1*e.x+b1;
			if(EPS(e.y-b.y)==0) printf("0.00\n");
			else printf("%.2lf\n",fabs(e.x-b.x)*(b.y-e.y)/2);
		}
		else
		{
			k1=(b.y-a.y)/(b.x-a.x),k2=(d.y-c.y)/(d.x-c.x),b1=(b.x*a.y-a.x*b.y)/(b.x-a.x),b2=(d.x*c.y-d.y*c.x)/(d.x-c.x);
			e.x=(b1-b2)/(k2-k1),e.y=k1*e.x+b1;
			if(k1==k2||EPS(e.y-b.y)==0||EPS((b.x-e.x)*(d.y-e.y)-(b.y-e.y)*(d.x-e.x))<0&&b.x<a.x&&d.x<=b.x||EPS((b.x-e.x)*(d.y-e.y)-(b.y-e.y)*(d.x-e.x))>0&&b.x>a.x&&d.x>=b.x) printf("0.00\n");
			else printf("%.2lf\n",fabs(((b.y-b2)/k2-b.x))*(b.y-e.y)/2);
		}
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator