Calculate the geometric algorithm Overview Original: Angry Robe May 17, 2003 Calculation Geometric Algorithm Overview This station Original: Angence Robe, Introduction Computer's emergence makes many originally cumbersome work greatly simplified, but there are some people in people Intuitive seems to be easy, but you need to take out a uncommon general solution, such as a geometric issue. As a branch of computer science, calculate the geometric main study to solve geometric problems. In the field of modern engineering and mathematics, the geometry has a very important application in many fields such as graphics, robotics, large scale integrated circuit design and statistics. In this article, we will make a comprehensive introduction to the basic algorithm for calculating geometric commonly used, I hope to understand and apply the knowledge solving problem of the calculation geometry. II. Calibration of the directory The basic concepts and common algorithms of the finishing of this paper include the following: The concept of vector judges the cross-galvation segment of the vector to determine whether the two-wire segment is intersecting whether the two-wire segment intersects the judge whether the two-wire segment intersects the line segment and the straight line intersect It is judged whether or not the rectangle includes a point determination line segment, the fold line, and the polygon is determined in the rectangle whether the rectangle is determined in the rectangle whether the circle is determined in the rectangle whether it is determined in the polygon to determine whether the line segment is determined in the polygon to determine whether the polygon is determined in the polygon. Decision is determined whether the rectangle determines whether or not the circle is determined in the polygon in the polygon to determine the line segment, the line, the rectangle, and the polygon is determined in the circle whether there is a nearest point calculation point to the line segment in the circle, rectangular , The nearest point calculation point of the polygon to the nearest distance and intersection coordinates calculate the intersection of the two common lines of line segments calculated line segments or linear and line segments of intersection or linear and fold lines, rectangles, and polygonal intersections. Concept of intersection bumps. Construction of the concept of bumps. Three, algorithm. Vector concept: If the endpoint of a line segment is in order, we have a Directed Segment. If the starting point P1 of the line segment P1P2 is in the coordinate original, we can call it a vector (Vector) P2. Vector add-off method: set two-dimensional vector P = (x1, y1), q = (x2, y2), then the vector add is defined as: P Q = (x1 x2, y2), the same, vector subtraction Defined as: P - Q = (X1 - X2, Y1 - Y2). Obviously, the nature P Q = Q P, P - Q = - (q - p). Vector Fork: Calculating vector fork is a core part of a line and line segment-related algorithm. Set vector p = (x1, y1), q = (x2, y2), then the vector fork is defined as the parallel quadrangular area of (0, 0), P1, P2, and P1 P2, ie, : P × q = x1 * Y2 - x2 * Y1, the result is a scalar. Obviously, properties P × Q = - (q × p) and p × (- q) = - (p × Q). Generally, in the case where it is not described, all points in this document are regarded as vectors, and the addition and subtraction of the two points is the sum of the vectors, while the multiplication is considered as a vector fork. A very important nature of the fork is to determine the two vectors of the two vectors with each other. If P × Q> 0, the P × Q> 0 is the clockwise direction of Q. If P × Q <0, P is in the counterclockwise direction of Q. If P × q = 0, the P and Q co-line, but it may also be in reverse.
The passage of the fold line segment is judged that the method of causing the row segment can be directly launched by the nature of the vector fork. For line segments P0P1 and P1P2 with common end points, the passage of the line segment can be determined by calculating the symbol of (P2 - P0) × (P1 - P0): if (p2-p0) × (p1 - p0)> 0, P0P1 gets P1P2 after P1 point. If (p2 - p0) × (P1 - P0) <0, the P0P1 is p1P2 after the P1 point is left to the left. If (p2 - p0) × (P1 - P0) = 0, the P0, P1, P2 three-point common line. Specific cases can be referring to the following figure: whether the judgment point is on the line segment: set points to q, the line segment is P1P2, the basis for judging the point q in this line segment is: (Q - P1) × (P2 - P1) = 0 and q Within the rectangle of the diagonal vertex with P1 and P2. The former guarantees whether the Q point is on the line P1P2, the latter guarantees whether the Q point is not in the extension line or reverse extension line of the line segment P1P2, the judgment of this step can be implemented in the following procedure: On-segment (pi, pj, pk IF min (xi, xj) <= xk <= max (xi, xj) and min (yi, y) <= yk <= max (yi, yj) THEN RETURN TRUE; ELSE RETURN FALSE; especially pay attention to Due to the need to consider two special circumstances in horizontal line segments and vertical line segments, Min (Xi, XJ) <= xk <= max (xi, xj) and min (yi, yj) <= YK <= max (yi, yj) two A condition must be satisfied at the same time to return true values. Determine whether the two-wire segment intersects: We are divided into two steps to determine if the two line segments are intersecting: (1) Rapid exclusion test Set with line segment P1P2 as the diagonal rectangle of R, set the rectangle of the line segment Q1Q2 as a diagonal line, if R and T do not intersect, apparent that the two-wire segment will not intersect. (2) Inter-across test If the two-tier segment intersects, the two-line segments will inevitably cross each other. If P1P2 is across Q1Q2, the vector (P1 - Q1) and (P2 - Q1) are located on both sides of the vector (Q2 - Q1), ie (P1 - Q1) × (Q2 - Q1) * (P2 - Q1) × (P2 - Q1) × Q2 - Q1) <0. The above can be rewritten (P1 - Q1) × (Q2 - Q1) * (Q2 - Q1) × (P2 - Q1)> 0. When (P1 - Q1) × (Q2 - Q1) = 0, the consensus (P1 - Q1) and (Q2-Q1) collinear, but because the P1 has been placed on the line segment Q1Q2; the same, (Q2 - Q1) × (P2 - Q1) = 0 Description P2 must be on the line segment Q1Q2. Therefore, it is determined that the basis of the P1P2 across Q1Q2 is: (p1 - q1) × (Q2 - Q1) * (Q2 - Q1) × (P2 - Q1)> = 0. The basis for judge the Q1Q2 across P1P2 is: (Q1 - P1) × (P2 - P1) × (P2 - P1) × (Q2 - P1)> = 0. The specific cases are shown below: At the same principle, specific implementation details for this algorithm may vary from this, except for this process, you can also refer to the implementation of the algorithm.
The judgment line segment and the straight line intersect: have the above foundation, this algorithm is very easy. If the line segment P1P2 is intersecting the line Q1Q2, the P1P2 is across Q1Q2, namely: (p1 - q1) × (Q2 - Q1) * (Q2 - Q1) × (P2 - Q1)> = 0. It is judged whether or not the rectangle includes points: Judging whether the abscissa of the point and the ordinate are clamped between the left and right sides of the rectangle. The determination line segment, the fold line, and the polygon are in the rectangle: because the rectangle is a convex set, so it is possible to determine if all endpoints are in the rectangle. The judgment is in the rectangle: as long as the left and right boundary and the upper boundary can be compared. Decision is in a rectangle: It is easy to prove that the filling condition in the rectangle is: the radius of the center in the rectangle and the radius of the circle is less than or equal to the center to the distance of the rectangular four sides. The judgment point is in the polygon: The determination point P is in the polygon is a very basic but very important algorithm that calculates a geometry in the geometry. With point P as endpoint, beamed to the left, since the polygon is bound, the left end of the ray L must be in the polygon, considering the move from the infinity, move from the end, encounter and polygonal When the first intersection, I entered the interior of the polygon. When I met the second intersection, I left the polygon, ... so it is easy to see that when the number of intersection points of L and polygons is odd, P is in the polygon , Is an even number of words p in the polygon. But some special circumstances should be considered. As shown in the figure below (a) (b) (c) (d). In the figure (a), the vertices of the L and polygons intersect, at this time, the intersection can only calculate one; in Figure (b), the intersection of the L and polygonal vertices should not be calculated; in Figures (c) and (d) , One side of the L and polygons is complicated, and this side should be ignored. If the L and polygons are complicated, this side should be ignored. For unity, we are calculating the intersection of rays L and polygons, 1. For the horizontal edge of the polygon; For the case where the vetet and L of the polygon are intersected, if the vertex is a large vertex of the longitudinal coordinates thereof, count, otherwise ignore; 3. For the case of p on the side of the polygon, it can be directly determined that the P belongs to the multilateral row. The pseudo code of this derived algorithm is as follows: count ← 0; with P as endpoints, as the left ray L; for the FOR polygon, each side S DO IF P on the edge s Return True; if S is not horizontal One endpoint of THEN IF S is on L. IF is the endpoint of the two ends of the longitudinal coordinate, the longitudinal coordinate, the endpoint THEN COUNT ← count 1 else IF S and L intersects THEN COUNT ← count 1; if count mod 2 = 1 Then Return True; Else Return False; Method in which ray L is: set the orders of P 'and P', the abscissa is infinite (a positive number), then P and P 'determines the ray L. The time complexity of this algorithm in the polygon is O (N). Another algorithm is to compare with a symbolic triangular area and a polygonal area, which will bring a certain error due to the use of floating point numbers, and do not recommend everyone.
The determination line segment is in the polygon: one of the necessary conditions in the polygon in the polygon is that the two endpoints of the line segment are in the polygon, but since the polygon may be concave, this cannot be a sufficient condition of judgment. If the line segment and a parallel of the polygon (two-segment internal sex refers to the endpoints of the two-wire segment intersecting and the intersection is not in the two-wire segment), because the left and right sides of the polygonal side are different parts of the different parts, there will be some part of the line segment. Outside the polygon (see Figure A). So we get the second essential condition of the line segment in the polygon: all edges of the line segment and the polygon do not communicate. The two ends of the line segment and polygonal intersecting line segments do not affect whether the line segments are in the polygon; however, if a vertex and line segments of the polygon intersect, it is also necessary to determine whether the line segments between the two phase neighborhoods are included in the polygon (reverse example b). Therefore, we can first seek all the polygonal vertices that intersect the line segments, and then sorted according to XY coordinates (X coordinate small rows, the same point as the X coordinates, Y coordinate small rows, this sorting criterion is also for The guarantee is correct and vertically determined), such adjacent points are two intersections adjacent on the line segment, and if the midpoints of any adjacent two points are also in the polygon, the line segment must be in the polygon. Proof as follows: Proposition 1: If the line segment and the neutral P 'of the two-phase adjacent points P1, P2 are in the polygon, all points between P1 and P2 are in the polygon. Certification: Suppose the P1, P2 contains a point in the polygon, but it is also possible to set between P1, P ', because the polygon is a closed curve, so there is a boundary between the inside and outside, and P1 belongs to multilateral rows The inside, Q belongs to the multilateral exterior, P 'belongs to the internal, P1-Q-P' completely continuous, so P1Q and QP 'must span the boundary of the polygon, so there is at least two line segments between P1 and P'. And polygonal intersections, this and P1P2 are contradictions between adjacent two intersections, and the life is established. Certificate. Directly can be derived from proposition 1: Inference 2: The intersection of polygons and line segment PQ is P1, P2, ... pn, where Pi and Pi 1 are adjacent two intersections, line segment PQ in a polygonal fill condition Yes: p, q are also in the polygon in the polygon and for i = 1, 2, ..., N-1, PI, PI 1. In actual programming, there is no need to calculate all intersections, first determine whether the line segment and the edge of the polygonal intersection, if the line segment and the parallel border of the polygon must be defined in the polygon; if the line segment and the polygon are If the line segment and the intersection of the line segment and the polygon must be the endpoint of the line segment or the vertex of the polygon, as long as the judgment point is on the line segment.
At this point, we derive the algorithm as follows: The endpoint of the IF terminal PQ is not all in the multilateral shape. Add PointSet; ELSE IF S EtOAc. The endpoint is added to the POINTSET; ELSE IF S and the line segment PQ intersect // This time it is definitely the internal hand of the Then Return False; Coordinate sorting; for each of For PointSet PointSet [i], PointSet [i 1] do if pointset [i], point set [i 1] in the polygon in the polygon; Return True; Sort in the process Because the number of intersection points is far less than the number of vertices of polygons, up to constant levels of complexity, almost negligible. Therefore, the time complexity of the algorithm is also o (n). It is determined whether the fold line is in a polygon: as long as it is determined whether each line segment of the fold line can be in the polygon. The arrangement of the fold line has a M strip section, and the polygon has N top points, the algorithm has a time complexity of O (M * n). It is judged whether or not the polygon is within the polygon: as long as it is determined whether or not each side of the polygon is within the polygon. It is judged whether a polygon having a M top point is in a polygon complexity in a polygon of N (M * n). It is judged whether or not the rectangle is in a polygon: convert the rectangle to a polygon, and then determine if it is within the polygon. The judgment is in the polygon: as long as calculating the shortest distance of each side of the polygon to the polygon, the circle is greater than or equal to the circular radius. The circle is in the polygon. The algorithm for calculating the shortest distance of each side of the center to polygons is described later. Whether the judgment point is in the circle: calculate the distance to this point, if it is less than or equal to the radius, the point is in the circle. The determination line segment, the fold line, the rectangle, the polygon is in the circle: because the circle is convex, so as long as it is judged whether or not each vertex is in the circle. The judgment is in the circle: sets the two circles to O1, O2, and the radii is R1, R2, respectively, to determine whether O2 is in O1. First compare the size of R1 and R2, if R1 Single two straight line equations solution: x = (k ^ 2 * pt1.x k * (point.y - pt1.y) Point.x) / (k ^ 2 1), y = k * (x - pt1.x) pt1.y; then determine if the vertex is on the line segment, if it returns a parallel to the line segment; if the two endpoints to the perspective are calculated, the endpoint is selected to return. . The most recent point of the point to the fold line, rectangle, and polygon: as long as the nearest point of each line segment is calculated, the most recent distance is recorded, and the minimum distance is minimized. Calculate the nearest distance and intersection coordinates of the circle: if this point is in the center, because the center to the distance of any point is equal, returning undefined. Connection point P and the center O, if the PO is parallel to the x-axis, according to the left side of the P on the left or on the right, the nearest point is calculated as Centerpoint.x - Radius or CenterPoint.x Radius. If the PO is parallel to the Y axis, according to the upper side of the P or lower, the nearest point is calculated as centralpoint.y - radius or centerpoint.y - radius. If the PO is not parallel to the X-axis and the Y axis, the slope of the PO is present and is not 0, and the straight line PO slope is k = (p.y - O.y) / (p.x - o.x). The equation of the straight line PO is: y = k * (x - p.x) p.y. The confession equation is: (x - ox) ^ 2 (Y - OY) ^ 2 = R ^ 2, the establishment of the two-way group can solve the line PO and round intersection, take the intersection of the P point can. Calculate the intersection of the line segments of the two common lines: For two common lines, the positional relationship between them has several situations shown below. Figure (a) has no intersection; two line segments in Figures (b) and (d) have endless focus; the two line segments in Figure (c) have a intersection. It is a short one in the two line segments. Line2 is a shorter, and if line1 contains two endpoints of line2, the case (D) is the case, and the two-wire section has endless intersection; if line1 contains only line2 One endpoint, then if one endpoint of Line1 is equal to the endpoint of the line2 included by Line1, the case (c) is the case (c), at this time there is only one intersection point, otherwise the case of Figure (b), the two line segments are also there Infinite intersection; if line1 does not contain any endpoints of line2, the case of Figure (a), there is no intersection between two lines. Calculate the intersection of line segments or linear and line segments: Set a line segment is L0 = P1P2, another line segment or straight line is L1 = Q1Q2, the calculation is the intersection of L0 and L1. 1. First, it is determined whether L0 and L1 intersect (the method has been discussed foregoing), if not intersect, there is no intersection, otherwise the L0 and L1 must be intended, and the L0 and L1 are considered in line below. 2. If the P1 and P2 abscissions are the same, i.e., L0 is parallel to Y-axis a) If L1 is also parallel to the Y-axis, i. If the ordinate of P1 is the same, the ordinate of the Q1 is the same, the L1 is common line, if L1 is a straight line They have endless intersections, if the L1 is the line segment, the algorithm for calculating the intersity of the two common line segments "is for their intersection (this method has been discussed in the forego); II. Otherwise, L0 and L1 are parallel, they have no intersection (B) If L1 is not parallel to the Y axis, the cross coordinate of the intersection of the intersection is the cross coordinates of P1, and the intersection orders can be calculated in the straight line equation of L1; 3. If the P1 and P2 are different, the Q1 and Q2 absorbers are the same, that is, L1 is parallel to the Y-axis, the cross coordinates of the intersection of the cross-coordinate are the cross coordinates of Q1, and the intersection orders can be calculated in the linear equation of L0; 4. If P1 and P2 ordinate, ie, L0 is parallel to X-axis a) If L1 is also parallel to the X-axis, i. If the abscissa of the P1 is the same, the L0 and L1 common lines are explained, if L1 is a straight line. They have endless intersections, if the L1 is the line segment, the algorithm for calculating the intersity of the two common line segments "is for their intersection (this method has been discussed in the forego); II. Otherwise, L0 and L1 are parallel, they have no intersection If L1 is not parallel to the X axis, the intersection of the ordinate of the intersection is the ordinate of P1, and the intersection of the intersection is calculated in the straight line equation of L1; 5. If the ordinate of P1 and P2 is different, the Q1 and Q2 ordinate are the same, that is, L1 is parallel to the X axis, and the intersection of the ordinate of the intersection is equal to Q1, and the intersection is calculated in the linear equation of L0; 6. The remaining situation is the case where the slope of L1 and L0 is present and is not 0. A) calculates the slope K0, L1 of L0, and the slope K1; b) If K1 = K2 I. If Q1 is on L0, L0 and L1 common line, if L1 is a straight line, there is an endless point, if L1 is the algorithm of "calculating the intersity of two common line segments", "The method is discussed in the foregoing); II. If Q1 is not On L0, the L0 and L1 are parallel, and they have no intersection. c) Singing two linear equations can solve the intersection to discuss this algorithm, but it is clear that the situation is discussed clearly, especially when the two line segments have a separate line, so two common lines will be obtained in the foregoing The algorithm of the line segment is written separately. In addition, starting with the vector fork judge whether the line segment (or straight) is intersecting the line segment (or straight), if the result is intersecting, then the line segment can be considered as a straight line. It should be noted that we can overwrite the line or line segment equation as the form of AX BY C = 0, so that some of the above steps can be merged, shortened code length, but due to parameters first, this algorithm It will take more time. Ask of line segments or straight lines, rectangles, polygons intersections: respectively ask the intersection of each side.