Distance Aizu - CGL_2_D

简单题不解释
题目链接

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<stack>
#define In inline
#define PI atan(1.0)*4
#define enter puts("")
#define MaxN 0x3f3f3f3f
#define MinN 0xc0c0c0c0
#define bug(x) cerr<<#x<<'='<<x<<' '
#define ALL(a) a.begin(), a.end()
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define mset(a,b) memset(a,b,sizeof(a))
#define sz(a) (int)a.size()
#define eps 1e-8
#define buff ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
template<typename T>
T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<typename T>
T lcm(T a,T b){return a*b/gcd(a,b);}
typedef long long ll;
typedef pair<int,int> PII;

In ll read()
{
	ll ans = 0;
	char ch = getchar(), las = ' ';
	while(!isdigit(ch)) las = ch, ch = getchar();
	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
	if(las == '-')	ans = -ans;
	return ans;
}
In void write(ll x)
{
	if(x < 0) x = -x, putchar('-');
	if(x >= 10)	write(x / 10);
	putchar(x%10 + '0');
}
int sgn(double x){
	if(fabs(x) < eps) return 0;
	return x > 0 ? 1:-1;
}
struct Point{
	double x, y;
	Point(){}
	Point(double x, double y):x(x),y(y){}
	Point operator - (Point B){return Point(x-B.x,y-B.y);}
	bool operator == (Point B){return sgn(x-B.x) == 0 && sgn(y-B.y) == 0;}
};
typedef Point Vector;
double Distance(Point A, Point B){return hypot(A.x-B.x,A.y-B.y);}
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;}
struct Segment{
	Point p1, p2;
	Segment(){}
	Segment(Point p1, Point p2):p1(p1),p2(p2){}
}; 
typedef Segment Line;
double Dis_point_line(Point p, Line v){
	return fabs(Cross(p-v.p1,v.p2-v.p1))/Distance(v.p1,v.p2);
}
double Dis_point_seg(Point p, Segment v){
	if(sgn(Dot(p-v.p1,v.p2-v.p1)) < 0 || sgn(Dot(p-v.p2, v.p1-v.p2)) < 0)
		return min(Distance(p, v.p1), Distance(p, v.p2));
	return Dis_point_line(p, v);
}
bool Cross_segment(Point a, Point b, Point c, Point d){
	double c1 = Cross(b-a, c-a), c2 = Cross(b-a, d-a);
	double d1 = Cross(d-c, a-c), d2 = Cross(d-c, b-c);
	return sgn(c1)*sgn(c2) < 0 && sgn(d1)*sgn(d2) < 0 || a==c || a==d || b==c || b==d;
}
void read(Point& a){
	scanf("%lf%lf", &a.x, &a.y);
}
void run_case(){
	Point p1, p2, p3, p4;
	read(p1), read(p2), read(p3), read(p4);
	double dist1, dist2, dist3, dist4;
	Segment p12 = {p1, p2}, p34 = {p3, p4};
	dist1 = Dis_point_seg(p3, p12), dist2 = Dis_point_seg(p4, p12);
	dist3 = Dis_point_seg(p1, p34), dist4 = Dis_point_seg(p2, p34);
	if(Cross_segment(p1, p2, p3, p4))	printf("%.10lf\n", 0.0);
	else printf("%.10lf\n", min(min(dist1, dist2),min(dist3,dist4)));
}

int main()
{
    int _ = read();
    while(_ --){
		run_case();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

marx97 ٩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值