题意: 给你两条直线,判断他们的位置,1.平行,2.重合,3.相交
思路:平行:点在同一边,并且点到直线的距离相等,重合:两点到直线的距离为都0,其余就是相交
g++ 判double 要用%f 不能用%lf
c++判double 都可以
叉乘的面积是有向的,要加绝对值啊!!!下次一定要注意啊
还有就是模板不能写错啊.......
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct Point
{
double x,y;
Point(double x = 0,double y = 0) : x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector a,Vector b) { return Vector(a.x+b.x,a.y+b.y) ;}
Vector operator - (Vector a,Vector b) { return Vector(a.x-b.x,a.y-b.y) ;}
Vector operator * (Vector a,double p) { return Vector(a.x*p,a.y*p) ;}
Vector operator / (Vector a,double p) { return Vector(a.x/p,a.y/p) ;}
double Dot(Vector a,Vector b) { return a.x*b.x + a.y*b.y ;}
double Length(Vector a) { return sqrt(Dot(a,a)) ;}
double Cross(Vector a,Vector b) { return a.x*b.y - a.y*b.x;}
const double eps = 1e-5;
int dcmp(double x)
{
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
bool operator == (Point a, Point b)
{
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
bool operator < (Point a,Point b)
{
return a.x < b.x && (a.x == b.x && a.y < b.y);
}
Point LineIntersection(Point p,Vector v,Point q,Vector w)
{
Vector u = p - q;
double t = Cross(w,u) / Cross(v,w);
return p + v*t;
}
double PointtoLineDistance(Point p,Point a,Point b)
{
double L = Length(b-a);
double area = Cross(p-a,b-a);
return fabs(area) / L;
}
int main()
{
int t;
scanf("%d",&t);
printf("INTERSECTING LINES OUTPUT\n");
while(t--)
{
int x1,y1,x2,y2,x3,y3,x4,y4;
scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
Point a(x1,y1),b(x2,y2),c(x3,y3),d(x4,y4);
double d1 = PointtoLineDistance(c,a,b);
double d2 = PointtoLineDistance(d,a,b);
if(dcmp(d1) == 0 && dcmp(d2) == 0)
{
printf("LINE\n");
}
else if(dcmp(d1-d2) == 0 && Cross(b-a,c-a)*Cross(b-a,d-a) > 0)
{
printf("NONE\n");
}
else
{
Point p = LineIntersection(a,b-a,c,d-c);
printf("POINT %.2f %.2f\n",p.x,p.y);
}
}
printf("END OF OUTPUT\n");
return 0;
}