HDU 2671 Can't be easier点直線の対称点について

978 ワード

/*
  ,  A,B       , min(PA+PB)=dis(A,B),
  ,  B        B`,min(PA+PB)=dis(A,B`)
*/
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
    int c;
    double k,x1,y1,x2,y2,x3,y3,x,y,dis,t,a,b;
    cin>>c;
    while(c--)
    {
        cin>>k>>x1>>y1>>x2>>y2>>x3>>y3;
        if(x3!=1.0) t=k-k*x3+y3; //     (1,t) (x3,y3)      L
        else t=2*k-k*x3+y3;  //     (2,t) (x3,y3)      L
        a=(1-x3)*(y2-y3)-(t-y3)*(x2-x3);
        b=(1-x3)*(y1-y3)-(t-y3)*(x1-x3);
        if(a*b>0) //a,b AC,BC      L   ,        
        {
            y=(2*y3+(k*k-1)*y2+2*k*(x2-x3))/(k*k+1); //    (CA+CB)        k   AB        -1/k      B`
            x=x2-k*(y-y2);       
            dis=sqrt((x1-x)*(x1-x)+(y1-y)*(y1-y)); 
        }
        else dis=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        printf("%.2lf
",dis); } return 0; }