线的交点交点

2023-09-11 04:17:45 作者:不哭不闹面带微笑〃

如何找到一个行是否拦截在多边形

How to find whether a line intercepted in a polygon

推荐答案

您可以在此实现在某些网页中读出一个合理的答案

You can read a reasonable answer from this implementation found in some webpage

Point  * intersection2(Point * _line1, Point * _line2) {

Point  p1,p2,p3,p4;
p1=_line1[0]; p3=_line2[0];
p2=_line1[1]; p4=_line2[1];

// Store the values for fast access and easy
// equations-to-code conversion
double x1 = p1.x, x2 = p2.x, x3 = p3.x, x4 = p4.x;
double y1 = p1.y, y2 = p2.y, y3 = p3.y, y4 = p4.y;

double A1 = y2-y1;
double B1 = x1-x2;
double C1 = (A1*x1)+(B1*y1);

double A2 = y4-y3;
double B2 = x3-x4;
double C2 = A2*x3+B2*y3;

double det = A1*B2 - A2*B1;

if (det==0){
	return NULL;
}else{
	// Return the point of intersection
	Point  * ret = new CvPoint2D64f ();
	ret->x = (B2*C1 - B1*C2)/det;
	ret->y = (A1*C2 - A2*C1)/det;
	return ret;

}

}

参考。 C ++示例:几何概念相交线及其应用,2D绘图 通过lbackstrom 从现场天亚社code

Reference. C++ Example: Geometry Concepts Line Intersection and its Applications, 2D drawing By lbackstrom from site ucancode