C++ realizes finding the intersection point of two straight lines, and finding the vertical foot at a point outside the known straight line

Two points on the known line or the slope and intercept of the known line are known to find the intersection point and vertical foot.
The two cases come from the fact that in the actual solution, two points on the boundary can be found to list the equation of a straight line, or a more accurate method can be used to obtain multiple points to fit a straight line.

1. Find the intersection point of two straight lines (two-point formula)

insert image description here

The equation of the straight line L1 is:
y − y 1 y 2 − y 1 = x − x 1 x 2 − x 1 \frac{y - y_1}{y_2 - y_1} = \frac{x - x_1}{x_2 - x_1}y2y1yy1=x2x1xx1
The equation of the straight line L2 is:
y − y 3 y 4 − y 3 = x − x 3 x 4 − x 3 = t \frac{y - y_3}{y_4 - y_3} =\frac{x - x_3}{x_4 - x_3} =ty4y3yy3=x4x3xx3=t
可得:
y = ( y 4 − y 3 ) ∗ t + y 3 x = ( x 4 − x 3 ) ∗ t + x 3 y = (y_4 -y_3)*t+y_3\\ x = (x_4 -x_3)*t+x_3 y=(y4y3)t+y3x=(x4x3)t+x3
Substitute the above x, y equation into the equation of the straight line L1:
( y 4 − y 3 ) ∗ t + ( y 3 − y 1 ) y 2 − y 1 = ( x 4 − x 3 ) ∗ t + ( x 3 − x 1 ) x 2 − x 1 \frac{(y_4 - y_3)*t+(y_3-y_1)}{y_2 - y_1} =\frac{(x_4 - x_3)*t+(x_3-x_1)}{x_2 - x_1}y2y1(y4y3)t+(y3y1)=x2x1(x4x3)t+(x3x1)

a 1 = y 4 − y 3 , a 2 = y 3 − y 1 , a 3 = y 2 − y 1 b 1 = x 4 − x 3 , b 2 = x 3 − x 1 , b 3 = x 2 − x 1 a_1 = y_4-y_3,a_2 =y_3-y_1,a_3 =y_2-y_1\\ b_1 = x_4-x_3,b_2 =x_3-x_1,b_3 =x_2-x_1a1=y4y3,a2=y3y1,a3=y2y1b1=x4x3,b2=x3x1,b3=x2x1
可得
a 1 t + a 2 a 3 = b 1 t + b 2 b 3 t = a 3 b 2 − a 2 b 3 a 1 b 3 − a 3 b 1 \frac{a_1t+a_2}{a_3} =\frac{b_1t+b_2}{b_3}\\ t = \frac{a_3b_2-a_2b_3}{a_1b_3-a_3b_1} a3a1t+a2=b3b1t+b2t=a1b3a3b1a3b2a2b3
Substitute t back to the original x, y formula to get the coordinates of the intersection point.

C++ implementation:

	Point p1,p2,p3,p4;//对应的四个点,根据实际应用场景赋值
	double a1, a2, a3, b1, b2, b3;
	double t;
	Point pp1, pp2;
	a1 = p4.y - p3.y;
	a2 = p3.y - p1.y;
	a3 = p2.y - p1.y;
	b1 = p4.x - p3.x;
	b2 = p3.x - p1.x;
	b3 = p2.x - p1.x;
	t = (a3 * b2 - a2 * b3) / (a1 * b3 - a3 * b1);
	pp1.x = (p4.x - p3.x) * t + p3.x;
	pp1.y = (p4.y - p3.y) * t + p3.y;

2. Find the intersection point of two straight lines (oblique intercept type)

insert image description here

The equation of the straight line L1 is:
y − y 1 y 2 − y 1 = x − x 1 x 2 − x 1 = t \frac{y - y_1}{y_2 - y_1} = \frac{x - x_1}{x_2 - x_1}=ty2y1yy1=x2x1xx1=The equation of t
-line L2 is
y = kx + by=kx+by=kx+b
can be obtained from the equation of L1:
x = ( x 2 − x 1 ) ∗ t + x 1 y = ( y 2 − y 1 ) ∗ t + y 1 x = (x_2-x_1)*t+x_1\\ y = (y_2-y_1)*t+y_1x=(x2x1)t+x1y=(y2y1)t+y1
代入L2的方程可得:
( y 2 − y 1 ) ∗ t + y 1 = k ( ( x 2 − x 1 ) ∗ t + x 1 ) + b t = ( k x 1 + b − y 1 ) ( y 2 − y 1 ) − k ( x 2 − x 1 ) (y_2-y_1)*t+y_1=k((x_2-x_1)*t+x_1)+b\\ t=\frac{(kx_1+b-y_1)}{(y_2 - y_1)-k(x_2-x_1)} (y2y1)t+y1=k((x2x1)t+x1)+bt=(y2y1)k(x2x1)(kx1+by1)
Substitute t back to the original x, y formula to get the coordinates of the intersection point.

C++ implementation:

	Point p1,p2;//对应的两个点,根据实际应用场景赋值
	vector<Point> points;//用来拟合直线的点
	Vec4f line;
	fitLine(points, line, DIST_L2, 0, 0.01, 0.01);//拟合得到直线,具体求解根据实际情况
	double cos_theta = line[0];
	double sin_theta = line[1];
	double x0 = line[2], y0 = line[3];
	double k = sin_theta / cos_theta;
	double b = y0 - k * x0;
	t = (k * p1.x + b - p1.y) / (p2.y - p1.y - k * (p2.x - p1.x));
	Point pp1;
	pp1.x = (p2.x - p1.x) * t + p1.x;
	pp1.y = (p2.y - p1.y) * t + p1.y;

3. Ask for the vertical foot (two-point style)

insert image description here

The equation of the straight line L is:
y − y 1 y 2 − y 1 = x − x 1 x 2 − x 1 \frac{y - y_1}{y_2 - y_1} = \frac{x - x_1}{x_2 - x_1}y2y1yy1=x2x1xx1
The direction vector of L is:
( a , b ) = ( x 2 − x 1 , y 2 − y 1 ) (a,b) = (x_2-x_1,y_2-y_1)(a,b)=(x2x1,y2y1)
then the direction vector of the vertical line perpendicular to L is:
( − b , a ) = ( y 1 − y 2 , x 2 − x 1 ) (-b,a) = (y_1-y_2,x_2-x_1)(b,a)=(y1y2,x2x1)
passing through the point (x3,y3) is:
x − x 3 − b = y − y 3 a = t \frac{x-x_3}{-b} = \frac{y-y_3} {a} = tbxx3=ayy3=t
可得
x = − b t + x 3 y = a t + y 3 x = -bt+x_3\\ y=at+y_3 x=bt+x3y=at+y3
Substituting into the equation of the straight line L can find t:
t = a ( y 1 − y 3 ) + b ( x 3 − x 1 ) a 2 + b 2 t = \frac{a(y_1 - y_3)+b(x_3- x_1)}{a^2 + b^2}t=a2+b2to ( and1y3)+b(x3x1)
Substituting t back to the original x, y formula can get the foot.

C++ implementation:

	Point p1,p2;//对应的两个点,根据实际应用场景赋值	
	int a = p2.x - p1.x;
	int b = p2.y - p1.y;
	double t = (a * (p1.y - p3.y) + b * (p3.x - p1.x));
	t = t / (a * a + b * b);//分两步计算,直接一步实现会由于精度问题导致结果出错
	Point p;
	p.x = -b * t + p3.x;
	p.y = a * t + p3.y;

4. Find the vertical foot (oblique intercept type)

insert image description here

The equation of the straight line L is:
y = k 1 x + b 1 y=k_1x+b_1y=k1x+b1
Suppose the equation of the vertical line is:
y = k 2 x + b 2 y=k_2x+b_2y=k2x+b2
The slopes of two straight lines perpendicular to each other satisfy the following relationship (if the slope does not exist, you need to find it separately):
k 2 = − 1 k 1 k_2 = -\frac{1}{k_1}k2=k11
From (x3,y3) on the vertical line,
b 2 = y 3 − k 2 x 3 b_2 = y_3-k_2x_3b2=y3k2x3
Simultaneous equations
y = k 1 x + b 1 y = k 2 x + b 2 y = k_1x+b_1\\ y = k_2x+b_2y=k1x+b1y=k2x+b2
Get
x = b 2 − b 1 k 1 − k 2 x=\frac{b_2-b_1}{k_1-k_2}x=k1k2b2b1
Substitute x into any of the above linear equations to get y, that is, to get the foot (x, y).

C++ implementation:

	Point p3;//直线外一点
    vector<Point> points;//用来拟合直线的点
	Vec4f pline;
	fitLine(points, pline, DIST_L2, 0, 0.01, 0.01);
	cos_theta = pline[0];
	sin_theta = pline[1];
	x0 = pline[2], y0 = pline[3];
	double k1 = sin_theta / cos_theta;
	double b1 = y0 - k1 * x0;
	double b2 = 0;
	double k2 = -1 / k1;
	b2 = p3.y - k2 * p3.x ;
	pp1.x = (b2 - b1) / (k1 - k2);
	pp1.y = k2 * pp1.x + b2;

5. The slope does not exist

If the line is horizontal, that is, the equation of the line is y=c, then the y of the vertical foot found at a point outside the line is c, and the x is the same as the x at a point outside the line. The vertical is the same, so I won’t go into details.

Guess you like

Origin blog.csdn.net/Lianhaiyan_zero/article/details/130582747