Circles - Cross Points of a Circle and a Line

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D
円と直線の交点を出力する問題。
普通に冗長に書いたつもりがなぜかコードの短さ2位を達成。
不思議だな。
他の人は変数名が長いとかなのだろうか?


#include<stdio.h> 
#include<algorithm>
#include<math.h>
struct E{
   double x,y;
   bool operator<(const E& e1)const{
       if(x!=e1.x)return x<e1.x;
       return y<e1.y;
   }
};
 
int main(){
   double cx,cy,r;
   int q;
   double x1,y1,x2,y2;
   scanf("%lf %lf %lf %d",&cx,&cy,&r,&q);
   for(int i=0;i<q;i++){
       scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
       x1-=cx;
       y1-=cy;
       x2-=cx;
       y2-=cy;
       double x4,y4;
       x4=x2-x1;
       y4=y2-y1;
       double a,b,c,d,t1,t2;
       a=x4*x4+y4*y4;
       b=2*x1*x4+2*y1*y4;
       c=x1*x1+y1*y1-r*r;
       d=b*b-4*a*c;
       if(d<0)d=0;
       t1=(-b-sqrt(d))/(2.0*a);
       t2=(-b+sqrt(d))/(2.0*a);
       E ans[2];
       ans[0].x=x1+t1*x4+cx;
       ans[0].y=y1+t1*y4+cy;
       ans[1].x=x1+t2*x4+cx;
       ans[1].y=y1+t2*y4+cy;
       std::sort(ans,ans+2);
       printf("%lf %lf %lf %lf\n",ans[0].x,ans[0].y,ans[1].x,ans[1].y);
   }
}
最終更新:2016年11月08日 08:40