First, Directory
The basic concepts of computational geometry and common algorithms in this paper include the following:
1. The concept of vectors
2. Vector addition and subtraction
3. Vector Cross Product
4. The turn judgment of the broken line
5. Determine if the point is on a line segment
6. Determine if two segments intersect
7. Determine if line segments and lines intersect
8. Determine if the rectangle contains points
9. Determine if line segments, polylines, and polygons are in the rectangle
10. Determine if the rectangle is in the rectangle
11. Determine if the circle is in the rectangle
12. Determine if the point is in the polygon
13. Determine if the segment is within a polygon
14. Determine if the polyline is inside a polygon
15. Determine if the polygon is inside a polygon
16. Determine if the rectangle is inside a polygon
17. Determine if the circle is within the polygon
18. Determine if the point is within the circle
19. Determine if segments, polylines, rectangles, and polygons are within the circle
20. Determine if the circle is within the circle
21. Calculate points to the nearest point of a segment
22. Calculate point to Polyline, rectangle, nearest point of polygon
23. Calculate the nearest distance and intersection coordinates of a point to a circle
24. Calculate the intersection of two lines of collinear segments
25. Calculate the intersection of a segment or line with a segment
26. Find the intersection of segments or lines with polylines, rectangles, and polygons
27. Find the intersection of a line or line with a circle
28. Convex Package Concept
29. The method of convex package
Second, the algorithm introduction
1. The concept of vectors:
If the endpoints of a line segment are in order, we turn this segment into a directed segment (directed segment). If there is a starting point of p1p2 to the line segment P1 at the origin of the coordinates, we can call it vector (vectors) P2.
2. Vector addition and subtraction:
To set the two-dimensional vector p = (x1, y1), Q = (x2, y2), the vector addition is defined as: P + Q = (x1 + x2, y1 + y2), and similarly, vector subtraction is defined as: P-q = (x1-x2, y1-y2). Obviously there is a nature P + q = q + p,p-q =-(q-p).
3. Vector Cross product:
The calculation of the vector cross product is the core part of the algorithm related to line and segment. With vector P = (x1, y1), Q = (x2, y2), the vector fork product is defined as a signed area of 0,0 consisting of (P1), p2, p1+p2, and parallelogram, that is: Pxq = x1*y2-x2*y1, and the result is a scalar. Obviously there are properties Pxq =-(QXP) and PX (-Q) =-(PXQ). Generally, in the case of no explanation, all the points in the algorithm described below are vectors, and the addition and subtraction of two points is the vector addition minus, while the multiplication of points is regarded as the vector cross product.
A very important property of the cross product is the ability to determine the clockwise-counterclockwise relationship between two vectors by its symbol:
If PXQ > 0, then P is in the clockwise direction of Q.
If Pxq < 0, then P is in the counterclockwise direction of Q.
If Pxq = 0, P is collinear with Q, but may also be reversed.
4. The turn of the broken line to judge:
The method of turning judgment of the broken segment can be directly introduced by the character of the vector cross product. For segments P0p1 and p1p2 with a common endpoint, you can determine the turn of a broken segment by calculating the symbol (P2-P0) x (p1-p0):
if (p2-p0) x (P1-P0) > 0, then p0p1 to the right after P1 point to get p1p2.
if (p2-p0) x (P1-P0) < 0, then P0P1 to the left after P1 point to get p1p2.
if (p2-p0) x (p1-p0) = 0, then the P0, P1, p2 three points collinear.
For details, refer to the following figure:
5. Determine if the point is on a line segment:
The point is Q, the line segment is P1P2, and the points Q is based on the segment: (Q-P1) x (P2-P1) = 0 and Q in a rectangle with a diagonal vertex of p1,p2. The former guarantees that the Q point is on the straight line p1p2, the latter is to ensure that the Q point is not in the line P1P2 extension lines or the reverse extension cable, for this step judgment can be achieved by the following process:
On-segment (PI,PJ,PK)//Ensure that the K point is inside the segment
If min (xi,xj) <= xk <= Max (XI,XJ) and Min (yi,yj) <= yk <= Max (YI,YJ)
then return true;
else return false;
In particular, due to the need to consider two special cases of horizontal and vertical segments, min (XI,XJ) <=xk<=max (XI,XJ) and Min (yi,yj) <=yk<=max (YI,YJ) Two conditions must be met at the same time to return truth.
6. Determine if the two segments intersect:
We determine whether two segments intersect in two steps:
(1) Rapid rejection test
The rectangle with the diagonal line p1p2 is R, the rectangle with the segment q1q2 diagonal is T, and if R and T do not intersect, it is obvious that the two segments do not intersect.
(2) Cross-stand test
If two segments intersect, the two segments must cross each other. If the p1p2 crosses the q1q2, the vectors (p1-q1) and (P2-Q1) are on either side of the vector (Q2-Q1) (P1-Q1) x (q2-q1) * (P2-Q1) x (Q2-Q1) < 0. The above formula can be changed to (P1-Q1) x (q2-q1) * (Q2-Q1) x (P2-Q1) > 0. When (P1-Q1) x (q2-q1) = 0 o'clock, the description (P1-Q1) and (Q2-Q1) collinear, but because of the rapid rejection test, so P1 must be on the line segment q1q2; Similarly, (Q2-Q1) x (P2-Q1) = 0 description P2 must be on line q1q2. So the basis for judging p1p2 q1q2 is: (p1-q1) x (q2-q1) * (Q2-Q1) x (P2-Q1) >= 0. Similarly, the basis for judging q1q2 p1p2 is: (Q1-P1) x (P2-P1) * (P2-P1) x (Q2-P1) >= 0. The details are shown in the following figure:
In the same principle, the specific implementation details of this algorithm may differ from this, in addition to this process, we can also refer to the introduction of the algorithm in the implementation.
7. Determine if the segment and line intersect:
With the foundation above, the algorithm is easy. If the segment p1p2 and the line q1q2 intersect, the p1p2 crosses the Q1Q2, namely: (P1-Q1) x (q2-q1) * (Q2-Q1) x (P2-Q1) >= 0.
8. Determine if the rectangle contains points:
Just determine whether the horizontal and vertical coordinates of the point are sandwiched between the left and right sides of the rectangle.
9. Determine if the segments, polylines, and polygons are in the rectangle:
Because the rectangle is a convex set, it is only possible to determine whether all the endpoints are in the rectangle.
10. Determine if the rectangle is in the rectangle:
Just compare the left and right borders and the upper and lower boundaries.
11. Determine if the circle is in the rectangle:
It is easy to prove that the necessary and sufficient condition for a circle in a rectangle is that the center is in the rectangle and the radius of the circle is less than the minimum of the distance from the center to the rectangle.
12. Determine if the point is in the polygon:
Determining whether a point P is in a polygon is a very basic but very important algorithm in computational geometry. To point P as the endpoint, to the left to make the Ray L, because the polygon is bounded, so the left end of the Ray L must be outside the polygon, consider moving along the L from Infinity to the left to the right, when encountered with the first intersection of the polygon, into the interior of the polygon, encountered the second intersection point, left the polygon, ... So it is easy to see that when the number of intersections of L and Polygon C is odd, p is inside the polygon and is even if p is outside the polygon.
But there are special circumstances to be considered. As shown in figure (a) (b) (c) (d) below. In figure (a), the L and polygon vertices intersect, at which point the intersection can only calculate one, and in figure (b), the intersection of L and polygon vertices should not be computed; in figures (c) and (d), L is coincident with one edge of the polygon, and this edge should be ignored. If L is coincident with an edge of a polygon, this edge should be ignored.
For the sake of unification, we are calculating the intersection of the Ray L and the polygon, 1. The horizontal edge of the polygon is not considered; 2. For polygons where the vertices and l intersect, if the vertex is a larger vertex on the edge of which it belongs, the count is ignored; 3. For the case of P on the polygon edge, it is directly possible to judge that p belongs to the multilateral line. Thus the pseudo code of the algorithm is as follows:
count←0;
With P as the endpoint, the right-to-left Ray L;
Each edge of the for Polygon s
Do if p on edge s
then return true;
If s are not horizontal
Then if s an endpoint on L
If the endpoint is an end point with a large ordinate in the s two ends
Then count←count+1
else if S and l intersect
Then count←count+1;
If Count mod 2 = 1
then return true;
else return false;
The way to do the Ray L is: set P ' ordinate and p the same, the horizontal axis is positive infinity (a large positive number), then p and P ' will determine the ray L.
The time complexity of the algorithm for determining whether a point is in a polygon is O (n).
Another algorithm is to use the triangular area of the triangle and the polygon area comparison, the algorithm because the use of floating-point arithmetic will bring a certain error, not recommended for everyone to use.
13. Determine if the segment is within the polygon:
A requirement for a segment within a polygon is that the two ends of the segment are within the polygon, but because the polygon may be concave, this cannot be a sufficient condition for judgment. If a line segment and an edge of a polygon intersect (a two-segment intersection refers to two segments intersecting and the intersection is not at the end of two segments), because the left and right sides of the polygon belong to different parts inside and outside the polygon, the segments must be part of the polygon outside (see Figure A). So we get the second necessary condition for the segment in the polygon: the segments and all the edges of the polygon are not handed in.
Segments and polygons that cross the ends of the segments do not affect whether the segments are within the polygon, but if a vertex of the polygon intersects a segment, you must also determine whether the segment between the two adjacent intersections is contained within the polygon (see Figure B for the inverse).
So we can first find all the vertices of the polygon intersection with the line, and then sort by x-y coordinates (the coordinates are small, for the x-coordinate point, the y-coordinate is small in front, the sorting criterion is to ensure that the horizontal and vertical conditions are correct), So the two adjacent points are adjacent to each other on the line, if the midpoint of any adjacent two points is also within the polygon, then the segment must be within the polygon.
The proof is as follows:
Proposition 1:
If the segment and Polygon's two adjacent intersection P1, P2 's midpoint P ' is also within the polygon, then P1, all points between P2 are within the polygon.
Prove:
Assuming that the p1,p2 between the points are not within the polygon, it is advisable to set the point Q, in P1, P ', because the polygon is a closed curve, so there is a boundary between the outside, and P1 belong to the multilateral line inside, Q belongs to the multilateral outside, P ' belongs to the multilateral, p1-q-p ' completely continuous, so p1q and Qp ' Must cross the boundaries of the polygon, so there are at least two points between the P1,p ' line and the intersection of the polygon, this and p1p2 is adjacent to the intersection of two intersections, so the proposition is established. The certificate is completed.
The inference from Proposition 1 can be deduced directly:
Corollary 2:
The intersection of the set polygon and segment PQ is P1,P2,...... Pn, where Pi and pi+1 are adjacent two intersections, the necessary and sufficient conditions of the PQ in the polygon are: P,q within the polygon and for I =1, 2,......, N-1,pi, the midpoint of Pi+1 is also within the polygon.
In the actual programming, it is not necessary to calculate all the intersection points, first of all, we should determine whether the edges of the line and polygon are inside, if the line segment and the polygon of a side of the intersection of the segment must be outside the polygon, if the line segment and polygon each edge is not within, then the intersection of the segment and polygon must be the end Just determine if the point is on the line.
Here we come to the following algorithm:
The endpoints of the If line end PQ are not all within the polygon
then return false;
The point set pointset initialized to null;
Each edge of the for Polygon s
An endpoint of the do if segment is on S
Then add the endpoint to Pointset;
else if an endpoint on the segment PQ
Then add the endpoint to Pointset;
else if s and segment PQ intersect//This time it's already been done.
then return false;
The points in the Pointset are sorted by x-y coordinates;
For PointSet every two adjacent points pointset[i], pointset[i+1]
Do if PointSet [i], pointset[i+1] The midpoint is not in the polygon
then return false;
return true;
The ordering in this process is almost negligible because the number of intersections is certainly much smaller than the number of vertices n of the polygon, so the complexity at most is constant. So the time complexity of the algorithm is also O (n).
14. Determine if the polyline is inside the polygon:
Just determine whether each segment of the polyline is within the polygon. The time complexity of the algorithm is O (m*n) when the polyline has an M segment and the polygon has n vertices.
15. Determine if the polygon is inside the polygon:
Just determine whether each edge of the polygon is within the polygon. Determines whether a polygon with m vertices has an O (m*n) complexity within a polygon with n vertices.
16. Determine if the rectangle is inside the polygon:
Convert the rectangle to a polygon and then determine if it is inside the polygon.
17 Determine if a circle is inside a polygon:
The circle is within the polygon as long as it calculates the shortest distance from the center to each edge of the polygon, if the distance is greater than or equal to the circle radius. The algorithm for calculating the shortest distance from the center to the polygon is described in the following article.
18. Determine if the point is within the circle:
Calculates the distance between the center of the point and the point within the circle if it is less than or equal to the radius.
19. Determine if line segments, polylines, rectangles, and polygons are within the circle:
Because a circle is a convex set, it is only possible to determine if each vertex is within a circle.
20. Determine if the circle is within the circle:
Set two circle for O1,o2, radius of R1, R2, to determine whether O2 in O1. Compare the size of the R1,R2 first, if the R1<R2 is O2 impossible within the O1, otherwise if the distance between the two centers is greater than R1-R2, then O2 is not in O1; otherwise O2 in O1.
21. Calculate the point to the nearest point of the segment:
If the segment is parallel to the x-axis (y-axis), the point is too far from the line where the segment is perpendicular, perpendicular is easy to calculate, and then calculates the perpendicular, if perpendicular on the line is returned perpendicular, otherwise the end of the perpendicular near the endpoint; The two ends of the line segment are PT1 and pt2, with a slope of k = (pt2.y-pt1. Y)/(pt2.x-pt1.x); the linear equation is: y = k* (x-pt1.x) + pt1.y. Its perpendicular slope is-1/k, the vertical equation is: y = ( -1/k) * (x-point.x) + Point.y.
The two linear equations are solved by: x = (k^2 * pt1.x + k * (POINT.Y-PT1.Y) + point.x)/(k^2 + 1), y = k * (x-pt1.x) + PT1.Y, and then determine if perpendicular is on a line segment, as The perpendicular is returned on the line segment, and if it is not, the distance from both ends to perpendicular is calculated, and the endpoint closer to perpendicular is selected for return.
22. Calculate points to the nearest point of the polyline, rectangle, Polygon:
Just calculate the points to the nearest point of each segment separately, record the closest distance, and take the point where the nearest distance is least.
23. Calculate the nearest distance and intersection coordinates of points to circles:
Returns undefined if the point is at the center of the Circle, because the center is equal in distance from any point in the circle.
The connection point P and center O, if the PO is parallel to the X axis, calculates the horizontal axis of the nearest point as Centerpoint.x-radius or centerpoint.x + radius according to p on the left or right side of O. If the PO is parallel to the y-axis, the nearest point's ordinate is Centerpoint.y-+radius or Centerpoint.y-radius, depending on whether p is above or below the O. If the PO is uneven on the x and Y axes, the PO's slope is present and not 0, when the straight po slope is k = (P.Y-O.Y)/(p.x-o.x). The equation for a straight po is: y = k * (x-p.x) + p.y. The Circle equation is (x-o.x) ^2 + (y-o.y) ^2 = R ^2, and the two equations can solve the intersection of the straight Po and the circle, taking the intersection where the P point is close.
24. Calculate the intersection of two lines of collinear segments:
For two collinear line segments, the position relationship between them has several situations as shown in the following figure. There is no intersection between the two segments in figure (a), and the two segments in figure (b) and (d) have infinite focus; the two segments in figure (c) have an intersection point. Set LINE1 is a long line in two lines, line2 is a shorter one, if Line1 contains the line2 two endpoints, it is the case of figure (d), the two segments have an infinite intersection, if the line1 contains only one endpoint of Line2, Then if an endpoint of line1 equals the end of the line2 that is contained in the line1, then it is the case of the figure (c), when the two segments have only one intersection, otherwise the case of the figure (b), the two segments also have an infinite intersection point; if line1 does not contain any endpoints of line2, it is the figure (a) , then there is no intersection between the two segments.
25. Calculate the intersection of a segment or line with a segment:
Set a line segment to L0 = P1P2, another segment or line as L1 = Q1q2, and the intersection of L0 and L1 to be computed.
1. First to determine whether L0 and L1 intersect (the method has been discussed in the previous article), if not intersect there is no intersection, otherwise the L0 and L1 must have intersection, the following will be L0 and L1 as a straight line to consider.
2. If P1