D - Can't be easier 几何计算

D - Can't be easier
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

I'm sure this problem will fit you as long as you didn't sleep in your High School Math classes. 
Yes,it just need a little math knowledge and I know girls are always smarter than we expeted. 
So don't hesitate any more,come and AC it! 
Tell you three point A,B,C in a 2-D plain,then a line L with a slope K pass through C,you are going to find 
a point P on L,that makes |AP| + |PB| minimal. 

Input

The first line contain a t. 
Then t cases followed,each case has two parts,the first part is a real number K,indicating the slope,and the second 
part are three pairs of integers Ax,Ay,Bx,By,Cx,Cy(0 <=|Ax|,|Ay|,|Bx|,|By|,|Cx|,|Cy| <= 10000 ).

Output

Just out put the minimal |AP| + |PB|(accurate to two places of decimals ).

Sample Input

1
2.55
8467 6334 6500 9169 5724 1478

Sample Output

3450.55


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 2009
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}



int main()
{
	ll t,i,j;
	while(scanf("%d",&t)==1)
	{
		
		for(j=1;j<=t;j++)
		{
			int flag ;
			double k,k1,ax,ay,bx,by,cx,cy,dx,dy,len,re,A,B,C;
			scanf("%lf",&k);
			scanf("%lf %lf %lf %lf %lf %lf",&ax,&ay,&bx,&by,&cx,&cy);
			
			
			if(k==0)
			{
				if((ay-cy)*(by-cy)<=0)
					flag = 1;
				else flag = 0;
			}
			else
			{
				if((by- (k*(bx-cx)+cy)) * (ay-(k*(ax-cx)+cy)) <=0)
					flag = 1;
				else flag = 0; 
			} 
			
			//printf("flag %d\n",flag);
			if(flag)
			{
				printf("%.2lf\n",sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by)));
				continue;
			}
			
			if(k == 0)
			{
				dx =  bx;
				dy =  cy*2-by;
			}
			else
			{
				/*k1 = -1/k;
				mx = (cy-by+k1*bx-k*cx)/(k1-k);
				my = k*(mx-cx)+cy;
			
			//	printf("mx=%lf my=%lf\n",mx,my);
				len = (mx-bx)*(mx-bx)+(my-by)*(my-by);
				
				dx = sqrt((4*len)/(k1*k1+1))+bx;
				
				if(dx==bx)
					dx = bx-sqrt((4*len)/(k1*k1+1));
				
				dy = k1*(dx-bx)+by;*/ 
				
				A  = -k;
				B  = 1;
				C  = k*cx-cy;
				dx = bx - 2*A*(A*bx+B*by+C)/(A*A+B*B);
				dy = by - 2*B*(A*bx+B*by+C)/(A*A+B*B);
				//dx = ((1-k*k)*ax+2*k*ay-2*k*(cy-k*cx))/(1+k*k);
				//dy = ((k*k-1)*ay+2*k*ax+2*(cy-k*cx))/(1+k*k);
				
			}
				
			//printf("%.2Lf",len);
		//	printf("dx=%lf dy=%lf\n",dx,dy);
			re = sqrt((dx-ax)*(dx-ax)+(dy-ay)*(dy-ay));
			printf("%.2lf\n",re);
		}
	}
    return 0;
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值