Introduction to common geometric calculation algorithms

Source: Internet
Author: User
Introduction to common geometric calculation algorithms

1. Vector Subtraction

Set two-dimensional vector p = (x1, Y1), q = (X2, Y2)
Then, the vector Subtraction is defined as p-q = (x1-X2, Y1-Y2)
Apparently, p-q =-(Q-P)
If this parameter is not specified, all vertices below are treated as vectors. the subtraction of two points is the subtraction of vectors;

2. Vector Cross Product

Set vector p = (x1, Y1), q = (X2, Y2)
Then the vector cross product is defined as: p × q = x1 * Y2-X2 * Y1. what we get is a scalar which obviously has a property of p × q =-(Q × P) p × (-q) =-(p × q)
If this parameter is not specified, all vertices below are considered as vectors, and vertices multiplication is considered as vector cross product;

The important nature of cross multiplication:

> If P x q> 0, P is clockwise in Q.
> If p × q <0, P is in the counterclockwise direction of Q.
> If p × q = 0, p and q are both in the same direction, but may be reversed.

3. Determine the online segment of a vertex

If the vertex is set to Q and the line segment is P1P2, the basis for determining the vertex Q on this line segment is: (q-P1) × (P2-P1) = 0 and Q is in P1, p2 is inside the rectangle of the diagonal vertex

4. Determine whether two line segments are Intersection

We can determine whether two line segments overlap in two steps:

(1). Rapid Rejection Test

Set the rectangle with the linestring P1P2 as the diagonal line to R and the rectangle with the linestring q1q2 as the diagonal line to T. If the R and t do not intersect, the two linestring obviously do not;

(2). Cross-Site Test

If the two line segments overlap, the two lines must cross each other, as shown in 1. In Figure 1, when P1P2 cross-sets q1q2, the vectors (P1-Q1) and (P2-Q1) are located on both sides of the vector (Q2-Q1), I .e. (P1-Q1) × (Q2-Q1) * (P2-Q1) × (Q2-Q1) <0 can be changed to (P1-Q1) × (Q2-Q1) * (Q2-Q1) × (P2-Q1)> 0 when (P1-Q1) × (Q2-Q1) = 0, the description (P1-Q1) and (Q2-Q1), but because it has passed the rapid Rejection Test, P1 must be on the online segment q1q2. Likewise, (Q2-Q1) × (P2-Q1) = 0 indicates that P2.

Therefore, the basis for determining P1P2 cross-site q1q2 is:

(P1-Q1) × (Q2-Q1) * (Q2-Q1) × (P2-Q1) ≥ 0

Similarly, the basis for determining q1q2 cross-site P1P2 is:

(Q1-P1) × (P2-P1) * (P2-P1) × (Q2-P1) ≥ 0

So far, the problem of determining whether a line segment is intersecting has been completely solved.

5. Determine whether the line segments and straight lines are Intersection

If the P1P2 line segments and the q1q2 Line Intersect, P1P2 cross-site q1q2, that is:

(P1-Q1) × (Q2-Q1) * (Q2-Q1) × (P2-Q1) ≥ 0

6. Determine whether the rectangle contains vertices

You only need to determine whether the horizontal and vertical coordinates of the point are between the left side of the rectangle and the upper and lower sides.

6. Determine whether the line segment, line, and polygon are in the rectangle.

Because the rectangle is a convex set, you only need to determine whether all endpoints are in the rectangle.

7. Determine whether the rectangle is in the rectangle

You only need to compare the left and right boundary with the upper and lower boundary.

8. Determine whether the circle is in the rectangle

A sufficient condition for a circle in a rectangle is that the center of the circle is in the rectangle and the radius of the circle is smaller than or equal to the minimum distance from the center to the four sides of the rectangle.

9. Check whether the vertex is in the polygon.

Take point P as the endpoint and take the ray L to the left. Because the polygon is bounded, the left end of the Ray l must be outside the polygon, consider moving from left to right starting from infinity along l. When we encounter the first intersection with the polygon, we enter the inside of the polygon. When we encounter the second intersection, we leave the polygon, ...... So it is easy to see that when the intersection of L and polygon number C is an odd number, P is inside the polygon, and P is outside the polygon if it is an even number.

However, some special cases should be taken into consideration. If l and the vertices of a polygon intersect, in some cases, only one intersection can be calculated. In some cases, the intersection cannot be calculated (you can figure it yourself ); if l and a polygon side overlap, this side should be ignored. For the sake of unification, when we calculate the intersection of Ray l and polygon, 1. Do not consider horizontal edges of polygon; 2. When the vertices of a polygon intersect with L, if the vertex is a vertex with a large vertical coordinate on the edge of the polygon, It is counted; otherwise, it is ignored; 3. In the case that P is located on the side of a polygon, we can directly determine that p is a multilateral row. The pseudo code of the algorithm is as follows:

1. Count limit 0;
2. Take P as the endpoint for the right-to-left Ray l;
3. For each side of a polygon s
4. Do if p is on side s
5. Then return true;
6. If S is not horizontal
7. An endpoint of then if S is on L and is an endpoint with a large vertical coordinate in the vertices at both ends of S.
9. Then count + 1
10. Intersection of else if S and l
11. Then count distinct count + 1;
12. If Count mod 2 = 1
13. Then return true
14. Else return false;

The method for Ray l is as follows: if the Y coordinate of P is the same as that of P, and the Y coordinate is positive infinity (a large positive number), P and P determine the ray L. The complexity of this algorithm is O (n ).

10. Determine whether the line segment is within the polygon.

A necessary condition for a line segment within a polygon is that the two ends of the line segment are within the polygon; if the intersection of a line segment and a polygon side (the intersection of a two-line segment refers to the intersection of two lines and the intersection is not at the endpoint of the two lines ), because the left and right sides of the polygon edge belong to different parts inside and outside the polygon, the line segment must have a part outside the polygon. So we get the second necessary condition of the line segment in the polygon: The line segment and all sides of the polygon are not inner;

The intersection of a line segment and a polygon at both ends of a line segment does not affect whether the line segment is in a polygon. However, if a vertex of a polygon is at the intersection of a line segment, it is also necessary to determine whether the line segment between two adjacent intersections contains the interior of the polygon. Therefore, we can first find all the vertices of the polygon that intersect with the line segment, and then sort them by the X-Y coordinates, so that the adjacent two points are the adjacent two intersections on the online segment, if the midpoint of any adjacent two points is within the polygon, the line segment must be within the polygon. The proof is as follows:

Proposition 1:

If the intersection of a line segment and a polygon is P1 and the point P of P2 is also within the polygon, all vertices between P1 and P2 are within the polygon.

Proof:

Assume that points P1 and P2 do not exist in the polygon. Set the point to Q, which is between P1 and P'. Because the polygon is a closed curve, the interior and exterior are bounded, while P1 is inside the multilateral row, Q is outside the multilateral, p' is inside the multilateral, P1-Q-P is completely continuous, so P1q and qP 'must span the boundary of polygon, so in P1, there are at least two intersection points between the line segment and the polygon. This is in conflict with P1P2, so the proposition is true. Certificate completion

Inferences can be drawn directly from Proposition 1:

Inference 2:

Set the intersection of polygon and PQ to P1, P2 ,...... Pn, where Pi and PI + 1 are adjacent to the two intersections. The condition that the PQ of a line segment is in the polygon is p, q is in the polygon, and for I = 1, 2, ......, The point of N-1, PI, PI + 1 is also within the polygon.

In actual programming, there is no need to calculate all the intersections. First, we should judge whether the edges of a line segment and a polygon are inner. If the inner intersection of a line segment and a certain side of a polygon is outside the polygon; if the line segment and each side of the polygon do not interwork, the intersection of the Line Segment and the polygon must be the end point of the line segment or the vertex of the polygon. You only need to judge whether the point is online.

The algorithm is as follows:

1. If the ends of the PQ line are not all inside the polygon.
2. Then return false;
3. Point Set pointset Initialization is empty;
4. s of each side of the for Polygon
5. The do if line segment has an endpoint on S.
6. Add the endpoint to pointset;
7. An else if s endpoint online segment on PQ
8. then adds this endpoint to pointset;
9. The else if s and the PQ of the line segment must be the inner intersection at this time.
10. Then return false;
11. Sort the points in pointset by the X-Y coordinate, the X coordinate is small in front,
For vertices with the same X coordinate, Y coordinates are smaller than Y coordinates;
12. For pointset, pointset [I], pointset [I + 1]
13. Do if pointset [I], the midpoint of pointset [I + 1] is not in the polygon.
14. Then return false;
15. Return true;

The complexity of this algorithm is O (n ). Because the number of vertices must be much smaller than the number of vertices in a polygon, sorting is at most a constant level of complexity, which is almost negligible.

11. You only need to judge whether each line segment of the line is within the polygon. If there are m line segments and N vertices in a polygon, the complexity is O (M * n ).

12. To determine whether a polygon is within a polygon, you only need to determine whether each side of the polygon is within the polygon. Determine whether a polygon with M vertices has an O (M * n) Complexity in a polygon with n vertices ).

13. Determine whether the rectangle is within the polygon to convert it into a polygon, and then determine whether it is within the polygon.

14. Determine whether the circle is within the polygon as long as the shortest distance from the center to each side of the polygon is calculated. If the distance is greater than or equal to the circle radius, the circle is within the polygon. The algorithm for calculating the shortest distance from the center of a polygon to each side of a polygon is described later.

15. Determine whether the point is within the circle to calculate the distance from the center to the point. If it is less than or equal to the radius, the point is within the circle.

16. Determine whether the line segment, line, rectangle, and polygon are in the circle. Because the circle is a convex set, you only need to determine whether each vertex is in the circle.

17. judge whether the circle is inside the circle

Set the circle to O1, O2, and radius to R1 and R2 respectively. Determine whether O2 is within O1. First, compare the sizes of R1 and R2. If R1 <R2, O2 cannot be within O1; otherwise, if the distance between the two centers is greater than R1-R2, O2 is not within O1; otherwise, O2 is in O1.

18. Calculate the closest point to a line segment

If the line segment is parallel to the X axis (Y axis), the point is used as the vertical line of the line where the line segment is located. It is easy to obtain the vertical foot, and then calculate the vertical foot, if the DRDs row is on the online segment, the DRDs row is returned. Otherwise, the endpoint near the DRDs row is returned;

If the line is not on the X axis or on the Y axis, the slope exists and is not 0. Set the two ends of a line segment to pt1 and pt2, and the slope is:
K = (pt2.y-pt1. y)/(pt2.x-pt1.x );
The linear equation is:
Y = K * (X-pt1.x) + pt1.y
Its vertical line slope is-1/K,
The vertical line equation is:
Y = (-1/K) * (X-point. X) + point. Y
Simultaneous two-line equation solutions:
X = (k ^ 2 * pt1.x + K * (point. Y-pt1.y) + point. X)/(K ^ 2 + 1)
Y = K * (X-pt1.x) + pt1.y;

Then judge whether the DRDs foot is in the online segment. If the DRDs foot is in the online segment, the DRDs foot is returned. If not, calculate the distance between the two ends and the DRDs foot, and select the endpoint closer to the DRDs foot to return.

19. to calculate the closest point of a point to a line, rectangle, or polygon, you only need to calculate the closest point of a point to each line segment, record the closest distance, and obtain the least recent point.

20. Calculate the nearest distance from a point to a circle

If the point is at the center of the center, undefined is returned.
The connection point P and the center O. If the Po is parallel to the X axis, the abscissa of the closest point is centerpoint Based on P on the left or right of O. x-radius or centerpoint. X + radius, as shown in 4 (a). If the Po is parallel to the Y axis, the ordinate of the closest point is calculated as centerpoint based on whether P is above or below o. Y + radius or centerpoint. y-radius, 4 (B.

If the Po is not on the X and Y axes, the PO slope does not exist, as shown in (c. At this time, the slope of the straight line Po is
K = (P. Y-O. Y)/(p. x-o. X)
The linear Po equation is:
Y = K * (X-p. x) + P. Y
Set the square course:
(X-o. x) ^ 2 + (Y-O. Y) ^ 2 = R ^ 2,
The two equations can solve the intersection of a straight line Po and a circle, and take the intersection point closest to the point P.

21. Calculate the intersection of two collocated Line Segments

The positional relationship between two line segments is shown in Figure 5. The two line segments in Figure 5 (a) have no intersection points. The two line segments in Figure 5 (B) and (d) have infinite focal points. The two line segments in Figure 5 (c) have one intersection point. Set line1 to a longer line segment and line2 to a shorter one. If line1 contains two endpoints of line2, It is shown in Figure 5 (d, the two line segments have infinite intersections. If line1 only contains one endpoint of line2, if an endpoint of line1 is equal to the endpoint of line2 contained by line1, It is shown in Figure 5 (c) in this case, there is only one intersection of the two line segments. Otherwise, there is an infinite intersection between the two lines in Figure 5 (c). If line1 does not contain any endpoint of line2, in Figure 5 (a), there is no intersection between the two line segments.

22. Calculate the intersection of a line or line and line

Set a line segment to L0 = P1P2 and another line segment or line to L1 = q1q2. The intersection of l0 and L1.

1. first, determine whether l0 and L1 are intersecting (the method has been discussed previously). If they do not, there is no intersection. Otherwise, it indicates that l0 and L1 must have an intersection, here we will consider both l0 and L1 as a straight line.

2. If the horizontal coordinates of P1 and P2 are the same, that is, 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 as that of Q1, it means that l0 and L1 are collocated. If L1 is a straight line, they have infinite intersections, if L1 is a line segment, you can use the algorithm "Calculate the intersection of two collinearity lines" to calculate their intersection points (this method has been discussed previously );
Ii. Otherwise, it indicates that l0 and L1 are parallel and they have no intersection points;
B) If L1 runs unevenly on the Y axis, the abscissa of the intersection point is P1. The ordinate coordinates of the intersection point can be calculated from the linear equation of L1;
3. if the abscissa of P1 and P2 is different, but Q1 and Q2 are the same, that is, L1 is parallel to the Y axis, the abscissa of the intersection is Q1, the vertical coordinates of the intersection can be calculated from the linear equation of l0;
4. If the ordinate values of P1 and P2 are the same, that is, l0 is parallel to the X axis.
A) if L1 is parallel to the X axis,
I. if the abscissa of P1 is the same as that of Q1, it indicates that l0 and L1 are both linear. If L1 is a straight line, they have infinite intersections, if L1 is a line segment, you can use the algorithm "Calculate the intersection of two collinearity lines" to calculate their intersection points (this method has been discussed previously );
Ii. Otherwise, it indicates that l0 and L1 are parallel and they have no intersection points;

B) If L1 is not on the X axis, the vertical coordinate of the intersection point is P1. the abscissa of the intersection point can be calculated from the linear equation of L1;
5. if the vertical coordinates of P1 and P2 are different, but Q1 and Q2 are the same, that is, L1 is parallel to the X axis, the vertical coordinates of the intersection are Q1, the abscissa of the intersection point can be calculated from the linear equation of l0;
6. The rest is that the slope of L1 and l0 both exists and is not 0.
A) Calculate the slope K0 of l0 and the slope k1 of L1;
B) If k1 = k2
I. if Q1 is on l0, it indicates that l0 and L1 are both linear. If L1 is a straight line, there is an infinite point of intersection, if L1 is a line segment, you can use the algorithm "Calculate the intersection of two collinearity lines" to calculate their intersection points (this method has been discussed previously );
II. If Q1 is not on l0, it means that l0 is parallel to L1, and there is no intersection between them.
C) A two-line equations can solve the intersection.

Note: This algorithm is not complex, but it should be discussed clearly in different situations, especially when the two line segments are collocated separately. Therefore, the algorithm for finding two collocated lines is written separately in the previous article. In addition, the vector cross-multiplication is used to determine whether a line segment and a line segment (or line) are intersecting at the beginning. If the result is an intersection, all the lines can be considered as a straight line at the end.

23. Calculate the intersection of a line segment or line with a line, a rectangle, or a polygon, and calculate the intersection with each side.

24. intersection of a line or line and a circle

 

Set the center to O, the circle radius to R, and the two points on the straight line (OR Line Segment) L are P1 and P2.
1. If l is a line segment and P1 and P2 are included in the circle o, there is no intersection. Otherwise, proceed to the next step.
2. If l is parallel to the Y axis,
A) calculate the distance from the center of the circle to the distance (DIS) from l.
B) If dis> r, there is no intersection between l and circle;
C) the coordinate of the two intersections can be obtained by using the alignment theorem, as shown in 6 (a). Note that the tangent of the L and the circle is considered.
3. If l is parallel to the X axis, it is similar to L's parallel to the Y axis;
4. If l is neither parallel to the X axis nor parallel to the Y axis, the slope k of l can be obtained, and then the point-oblique equation of l can be listed. The intersection of L and the circle can be solved by combining with the circle equation;
5. If l is a line segment, for the intersection points obtained in 2, 3, and 4, you must determine whether it belongs to the range of the line segment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.