Find the intersection of a line segment and an arc

       A recent interview encountered a problem, that is, the coordinates of the two endpoints of a line segment A (x1, y1), B (x2, y2), and the center of an arc (the arc from the starting point rotates counterclockwise to the end point) Coordinates C(m,n), radius r, starting angle a1 and ending angle a2 (starting angle and ending angle refer to the angle of turning counterclockwise with the positive direction of the x-axis as the starting point), find the intersection of the line segment and the arc . There is too little information in this area on the Internet, so record it, hoping to give some help to future generations.

      To solve this problem, it can be divided into several steps:

      1. The first step is to find the distance d from the center of the arc to the straight line (line segment) d = |(Ax0 + By0 + C) / Math.sqrt((A^2 + B^2))|, A = Y2-Y1, B = X1-X2, C = X2*Y1-X1*Y2. When d is greater than the arc radius r, it means that the line segment and the arc cannot intersect. You don’t need to calculate the following and return directly; when d<=r, Go to step 2.

      2. Find the intersection of a straight line composed of line segments and a circle composed of arcs. The result may be 1 or 2 intersections. Finding the intersection of a straight line and a circle is to find the solution of the equation group consisting of the equation y=kx+b of the straight line and the equation of the circle (xm)^2+(yn)^2 = r^2. The equation of a straight line can be obtained by bringing in the two coordinate values ​​of the line segment, that is, y1=kx1+b and y2=kx2+b, bringing in k=(y1-y2)/(x1-x2), b=(x1y2- x2y1)/(x1-x2), here we need to consider the existence of slope k and the absence of k (there is also a k=0 that can be treated as k exists). If k exists, after obtaining k and b, substituting the equation of the straight line into the equation of the circle is (xm)^2+(kx+bn)^2=r^2, that is, (1+k^2)x^2+( 2kb-2kn-2m)x+m^2+(bn)^2-r^2=0 The solution of the binary linear equation, according to the root formula, x1 = (-b + sqrt(b^2-4ac) ) / (2a), x2 = (-b-sqrt(b^2-4ac)) / (2a), where a=1+k^2, b=2kb-2kn-2m, c=(bn)^2 -r^2, and then find the coordinates of the intersection. If k does not exist, the equation of the straight line is equal to x=x1, and the equation of the circle is (x1-m)^2+(yn)^2=r^2, that is, y^2-2ny+n^2+(x1 -m)^2-r^2=0, according to the root finding formula, y1 = (-b + sqrt(b^2-4ac)) / (2a), y2 = (-b-sqrt(b^2-4ac) )) / (2a), where a=1, b=-2n, c=n^2+(x1-m)^2-r^2, and then find the coordinates of the intersection point.

      3. Judging whether the intersection point is on the line segment, the method of judging whether the point is on the line segment can be determined by judging whether the point is in the rectangle formed by the two end points of the line segment as diagonal lines (the sides of the rectangle are perpendicular to the x-axis and y-axis), The premise is that the point is already on a straight line composed of line segments. If the intersection point is not on the line segment, then return to empty, otherwise go to the next step.

      4. To judge whether the intersection is on the arc, my method is to first obtain the starting point coordinates of the arc according to the center, radius and starting angle of the arc. The formula is beginPoint.x = m+r*Math.cos(a1) ;beginPoint.y = n+r*Math.sin(a1); Then calculate the starting point angle startAngle of the arc according to the starting point and the center coordinates (the starting point angle here is not the starting angle a1 given by the title, which is the starting angle The origin of the coordinate system is the center of the circle), which can be calculated by the tangent function. The angles obtained by the tangent here are all less than 90 degrees. I then converted the coordinates of each point to the coordinate system with the center of the circle as the origin and then set the center of the circle according to the coordinates of the point. Which quadrant of the coordinate system of the origin determines the angle. In the same way, find the end angle endAngle (startAngle may be greater than andAngle, because the arc is formed from the starting point counterclockwise to the end point), and then find the angle angle of the intersection. When startAngle<endAngle, if startAngle<=angle<=endAngle is satisfied, the intersection is on the arc; when startAngle>endAngle, if angle>=startAngle || angle<=endAngle is satisfied (because angle is distributed between 0~endAngle and startAngle~360 degree area), the intersection is on the arc.

      After the above steps, the intersection point of the line segment and the arc can be obtained, and the screenshot of the program running result:

Below is the download of the algorithm code and test program:

https://download.csdn.net/download/wdglhack/13983942

Guess you like

Origin blog.csdn.net/wdglhack/article/details/111878621