poj 1269 Intersecting Lines幾何学的直線を計算して交点を求める

2322 ワード

タイトルリンク
队长と军兄に教えてもらって、やっと模版の思想が分かりました.
テンプレートは自分でもう一度書く必要があります.
また、g++コンパイラでは出力は%fで、c++コンパイラでは%lfであることに注意してください.
#include<stdio.h>
#include<cmath>
#include<iostream>
#include<stdlib.h>
using namespace std;

struct point{
       double x,y; 
       point(double a,double b):x(a),y(b){}  //                          
       point(){};                              //         
       friend point operator - (const point &a,const point &b){   //    
             return   point(a.x-b.x,a.y-b.y);     
       }
       friend point operator * (const double &a,const point &b){   //    
              return point(a*b.x,a*b.y);       
       }
       friend point operator / (const point &a ,const double b){  //    
              return point(a.x/b,a.y/b);       
       }
};
struct line{
       point a,b;
} ;


const double eps=1e-8;
int cmp(double x){               //       0
   if(fabs(x)<eps)return 0;
   if(x>eps)return 1;
   else return -1;
}
double det(point a,point b){            //       
      return a.x*b.y-a.y*b.x;   
}
bool parallel(point a,point b){   //       
    if( cmp(det(a,b))==0 ) return true;
    else return false;
}
point Line_make_point(line one,line two){    //       
      double s1=det(one.a-two.a , two.b-two.a);
      double s2=det(one.b-two.a , two.b-two.a);
      return (s1*one.b-s2*one.a)/(s1-s2);      
}


int main()
{
    line one,two;
    int n;
    scanf("%d",&n);
    printf("INTERSECTING LINES OUTPUT
"); while(n--){ scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&one.a.x,&one.a.y,&one.b.x,&one.b.y,&two.a.x,&two.a.y,&two.b.x,&two.b.y); if ( (parallel(one.a-one.b,two.a-two.b)==true) && ((parallel(one.a-two.b,two.a-one.b)==true)) ) { printf("LINE
"); continue; } if ( parallel(one.a-one.b,two.a-two.b)==true ){ printf("NONE
"); continue; } point ans=Line_make_point(one,two); printf("POINT %.2f %.2f
",cmp(ans.x)==0?0:ans.x , cmp(ans.y)==0?0:ans.y ); } printf("END OF OUTPUT
"); //system("pause"); return 0; }