判断直线是否相交

没有提供判断是否共线这种情况的处理代码,但这个代码做更改比较简单


#include <iostream>
#include <iomanip>
#include <limits>

using namespace std;

class POINT{
public:
	int x;
	int y;
	//int z;
	//POINT(int px,int py,int pz){ 
	//	x = px; 
	//	y = py; 
	//	z = pz; 
	//}
	POINT(int px,int py){ 
		x = px; 
		y = py; 
	}
	POINT(POINT &po){
		x = po.x;
		y = po.y;
	}

	POINT& operator=(POINT &rp){
		x = rp.x;
		y = rp.y;
		return *this;
		/*
		POINT pt;
		pt.x = rp.x;
		pt.y = rp.y;
		return pt;*/
	}
};

POINT& operator-(POINT &lp, POINT &rp){
	POINT* pt = new POINT(0,0);
	pt->x = lp.x - rp.x;
	pt->y = lp.y - rp.y;
	return *pt;
}

class LINE{
public:
	POINT pt1;
	POINT pt2;
	LINE(POINT p1, POINT p2):pt1(p1),pt2(p2){}
};

int crossproduct(POINT &pt, LINE &le){
	POINT p1 = pt - le.pt1;
	POINT p2 = le.pt2 - le.pt1;

	return ((p2.x * p1.y) - (p2.y * p1.x));
}

bool iscrossline(LINE &lea, LINE &leb){

	if((crossproduct(lea.pt1, leb)*crossproduct(lea.pt2, leb) > 0) ||
		(crossproduct(leb.pt1, lea)*crossproduct(leb.pt2, lea) > 0)){
		return 0;
	}else{
		return 1;
	}
}

int main(int argc, char** argv)
{
	POINT leapt1(0,0);
	POINT leapt2(0,2);
	POINT lebpt1(2,2);
	POINT lebpt2(0,0);
	LINE lea(leapt1,leapt2);
	LINE leb(lebpt1,lebpt2);

	cout << iscrossline(lea,leb) << endl;

	return 0;
}




你可能感兴趣的:(判断直线是否相交)