计算几何。。。?

没过,不知道哪错了,有人帮我debug吗?

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
typedef double db;
struct point
{
	db x,y;
	point(){}
	point(db _x,db _y)
	{
		x = _x;
		y = _y;
	} 
	friend point operator + (const point &a,const point &b)//重载运算符
	{
		return point(a.x + b.x,a.y + b.y);
	}
	friend point operator - (const point &a,const point &b)
	{
		return point(a.x - b.x,a.y + b.y);
	}
	friend point operator * (const point &a,const db d) 
	{
		return point(a.x * d,a.y *d); 
	}
	friend db operator * (const point &a,const point &b)//叉乘 
	{
		return a.x * b.y - a.y * b.x;
	}
	db norm()
	{
		return sqrt(x * x + y * y);
	}
	friend db dot(const point &a,const point &b)
	{
		return a.x * b.x + a.y * b.y;
	}//点乘
}Point[5];
struct seg
{
	point a,b;
	db d;
	seg(){}
	seg(point _x,point _y)
	{
		a = _x;
		b = _y;
		d = (b.y - a.y) / (b.x - a.x);
	}
	friend point cross(const seg &s,const seg &t)
	{
		db s1 = (s.a - t.a) * (t.b - t.a);
		db s2 = (s.b - t.b) * (t.a - t.b);
		return s.a + (s.b - s.a) * (s1/(s1 + s2));
	}
}Seg[3];
int main()
{
	db x1,y1,x2,y2;
	cin>>x1>>y1>>x2>>y2;
	Seg[1] = seg(point(x1,y1),point(x2,y2));
	cin>>x1>>y1>>x2>>y2;
	Seg[2] = seg(point(x1,y1),point(x2,y2));
	printf("%lf",cross(Seg[1],Seg[2]));
	return 0;
}

 

posted @ 2018-05-26 22:53  DukeLv  阅读(121)  评论(0编辑  收藏  举报