www.pudn.com > GetIntersectPoint.zip > GetIntersectPoint.cpp


 
 
 
void GetIntersectTwoLines(const double x1, const double y1, const double x2, const double y2,  
						  const double x3, const double y3, const double x4, const double y4,  
						  double& xt, double& yt)  
{ 
// Compute the intersection point of two line segments 
// 
	double denominator = (y4-y3)*(x2-x1) - (x4-x3)*(y2-y1); 
	if(fabs(denominator) <= MEAN_ERR) 
	{ 
		xt = 0.0; 
		xt = 0.0; 
		return; 
	} 
		 
	double ua = (x4-x3)*(y1-y3) - (y4-y3)*(x1-x3); 
	ua /= denominator; 
	 
	double ub = (x2-x1)*(y1-y3) - (y2-y1)*(x1-x3); 
	ub /= denominator; 
	 
	xt = x1 + ua*(x2-x1); 
	yt = y1 + ua*(y2-y1); 
} 
 
 
 
void GetIntersectTwoLines(const double P1[2], const double P2[2],  
						  const double P3[2], const double P4[2],  
						  double Pt[2]) 
{ 
// Compute the intersection point of two line segments 
// 
	double denominator = (P4[1]-P3[1])*(P2[0]-P1[0]) - (P4[0]-P3[0])*(P2[1]-P1[1]); 
	if(fabs(denominator) <= MEAN_ERR) 
	{ 
		Pt[0] = 0.0; 
		Pt[0] = 0.0; 
		return; 
	} 
		 
	double ua = (P4[0]-P3[0])*(P1[1]-P3[1]) - (P4[1]-P3[1])*(P1[0]-P3[0]); 
	ua /= denominator; 
	 
	double ub = (P2[0]-P1[0])*(P1[1]-P3[1]) - (P2[1]-P1[1])*(P1[0]-P3[0]); 
	ub /= denominator; 
	 
	Pt[0] = P1[0] + ua*(P2[0]-P1[0]); 
	Pt[1] = P1[1] + ua*(P2[1]-P1[1]); 
}