Computational Geometry knowledge __java

Source: Internet
Author: User
Tags polyline

First, the introduction

The advent of computers has made many of the previously tedious tasks much simpler, but there are a number of things that are easy to see in the eyes of people who need to come up with a common solution that is not simple, such as geometric problems. As a branch of computer science, computational geometry focuses on algorithms for solving geometric problems. In the field of modern engineering and mathematics, computational geometry is very important in the fields of graphics, robotics, VLSI design and statistics. In this article, we will make a comprehensive introduction to the basic algorithms commonly used in computational geometry, and hope to help you understand and apply the knowledge of computational geometry to solve problems.

Ii. Table of Contents

In this paper, the basic concepts of computational geometry and commonly used algorithms include the following:

The concept of vector

vector addition and subtraction

Vector Cross Product

The turn judgment of the folded segment

Determine if the point is on a line segment

To determine whether two segments intersect

To determine whether segments and lines intersect

Determines whether a rectangle contains dots

Determines whether a segment, polyline, or polygon is in a rectangle

Determines whether the rectangle is in the rectangle

To determine whether a circle is in a rectangle

Determine if the point is in the polygon

To determine whether a segment is inside a polygon

To determine if a polyline is inside a polygon

To determine whether a polygon is inside a polygon

To determine whether a rectangle is inside a polygon

To determine whether a circle is inside a polygon

Determine if the point is within the circle

To determine whether a segment, polyline, rectangle, or polygon is inside a circle

To determine whether a circle is inside a circle

Calculate point to nearest point of line

Calculate points to the nearest point of a polyline, rectangle, or polygon

Calculates the nearest distance and intersection coordinates of a point to a circle

Calculates the intersection point of a two-line line segment

To compute the intersection point of a segment or line with a segment

To find the intersection point of a segment or line with a polyline, rectangle, or polygon

To find the intersection point of a segment or line with a circle

The concept of convex package

The method of solving the convex bag

Iii. Introduction to the algorithm

The concept of vectors:

If the endpoints of a line segment are sequentially divided, we make the segment a directed segment (directed segment). If there is a starting point for the line segment P1p2 P1 at the origin of the coordinate, we can call it vector p2.

Vector Addition and subtraction:

Set the two-dimensional vector P = (x1, y1), Q = (x2, y2), then the vector addition is defined as: P + Q = (x1 + x2, y1 + y2), the same, vector subtraction is defined as: P-q = (x1-x2, y1-y2). There is obviously a nature P + q = q + p,p-q =-(q-p).

Vector Cross product:

The calculation of vector cross product is the core part of the algorithm related to line and segment. for vector P = (x1, y1), Q = (x2, y2), the vector cross product is defined as a signed area of a parallelogram composed of (0,0), p1, p2, and p1+p2, i.e.: Pxq = x1*y2-x2*y1, and the result is a scalar. Apparently of a nature pxq =-(QXP) and PX (-Q) =-(PXQ). Generally, in the case of no explanation, all the points in the algorithm below are considered as vectors, and the addition and subtraction of two points is the vector addition minus, while the point multiplication is regarded as the vector cross product.

A very important property of a cross product is that it can be used to determine the cis-counterclockwise relationship between two vectors.

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, then P and Q are collinear, but may also be reversed.

The turning judgment of the folding segment:

The method of judging the deflection of the folded segment can be introduced directly from the nature of the vector cross product. For segments P0p1 and p1p2 with a common endpoint, the turn of the folded segment can be determined by calculating the symbol (P2-P0) x (p1-p0):

if (p2-p0) x (P1-P0) > 0, the P0P1 is p1p2 on the right side of the P1 point.

if (p2-p0) x (P1-P0) < 0, the P0P1 is p1p2 after the P1 point is turned to the left.

if (p2-p0) x (p1-p0) = 0, then P0, p1, p2 three points collinear.

The specific situation may refer to the following figure:
  

Determine if the point is on a line segment:

The point q is the p1p2 of the line, and the basis of the judge points in the line is:
And Q is in the rectangle with the P1,P2 as the diagonal vertex. The former ensures that the Q-point is on a straight line p1p2, the latter is to ensure that the Q-point is not in the line p1p2 extension or reverse extension, for this step can be judged by the following procedure:

On-segment (PI,PJ,PK)

if min (xi,xj) <= xk <= Max (XI,XJ) and Min (yi,yj) <= yk <= Max (YI,YJ)

then RET  Urn true;

else return false;

In particular, the Min (xi,xj) <=xk<=max (XI,XJ) and Min (yi,yj) <=yk<=max (YI,YJ) are considered due to the need to consider two special cases of horizontal and vertical segments. Two conditions must be satisfied in order to return a truth value.

To determine whether two segments intersect:

We determine whether two segments intersect in two steps:

(1) Fast rejection test

Set the rectangle with the diagonal line p1p2 as R, set the rectangle with the diagonal line q1q2 as T, and if R and T do not intersect, it is obvious that the two segments do not intersect.

(2) Cross-state test

If two segments intersect, the two segments are bound to cross each other. If the P1P2 cross the Q1Q2, then the vector (P1-Q1) and (P2-Q1) are located on either side of the vector (Q2-Q1) (P1-Q1) x (q2-q1) * (P2-Q1) x (Q2-Q1) < 0 。 The upper type can be written in (P1-Q1) x (q2-q1) * (Q2-Q1) x (P2-Q1) > 0. When (P1-Q1) x (q2-q1) = 0 o'clock, description (P1-Q1) and (Q2-Q1) collinear, but because the fast rejection test has been passed, the P1 must be on the line q1q2; (q2-q1) x (p2-q1 = 0 indicates that P2 must be on the line q1q2. Therefore, the basis for judging p1p2 cross-Q1Q2 is: (p1-q1) x (q2-q1) * (Q2-Q1) x (P2-Q1) >= 0. The basis for judging Q1Q2 cross-P1P2 is: (Q1-P1) x (P2-P1) * (P2-P1) x (Q2-P1) >= 0. The details are shown in the following illustration:

In the same principle, the details of the implementation of this algorithm may differ from this, in addition to this process, you can also refer to the implementation of the introduction of algorithms.

To determine whether segments and lines intersect:

With the above foundation, this algorithm is very easy. If a segment p1p2 intersects a straight line q1q2, the P1P2 is Q1Q2, that is, (P1-Q1) x (q2-q1) * (Q2-Q1) x (P2-Q1) >= 0.

To determine whether a 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 and the upper and lower sides.

Determines whether a segment, polyline, or polygon is in a rectangle:

Because the rectangle is a convex set, it's OK to just judge if all the endpoints are in the rectangle.

Determines whether the rectangle is in the rectangle:

Just compare the left and right edges and the upper and lower boundaries.

To determine whether a circle is in a rectangle:

It is easy to prove that a circle is in a rectangle and the radius of the circle is less than the minimum of the distance between the center and the four edges of the rectangle.

Determine if the point is in a polygon:

To determine whether point P is a very basic but very important algorithm in the computation geometry in the polygon. With point P as the endpoint, to the left as the Ray L, because the polygon is bounded, so the left side of the Ray L must be outside the polygon, considering moving from left to right along L from Infinity, to the first intersection of the polygon, to the interior of the polygon, to the second intersection, to the polygon, ... So it's easy to see that when the number of intersections of L and polygons is odd, p is in the polygon and the even word p is outside the polygon.

But there are special circumstances to consider. As shown in figure (a) (b) (c) (d) below. In figure (a), l and the vertices of a polygon intersect, at which point the intersection can only compute one; in diagram (b), the intersection of L and polygon vertices should not be computed; in graphs (c) and (d), the edges of L and polygons overlap, and this edge should be ignored. If a side of the L and a polygon is coincident, this edge should be ignored.

For the sake of unification, when we compute the intersection of the X ray L and the polygon, 1. The horizontal edges of the polygon are not considered; 2. For the vertex and L intersection of polygons, if the vertex is a larger vertex on the side of which it belongs, the count is ignored; 3. For the case where P is on the edge of a polygon, it can be directly judged that p belongs to the multilateral line. The pseudo code for this algorithm is as follows:

count←0;
    Taking p as the endpoint, as the Ray L of the right and left; 
    For each side of the for polygon s do
     if p on the side S 
          then return true;
        If S is not
          a horizontal then if S is an endpoint on L
                 if the endpoint is a larger-ordinate endpoint at both ends of S
                   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 method of doing Ray L is to set the ordinate of P ' and P ', and the horizontal axis is positive infinity (a large positive number), then p and P ' determine the X-ray L.

The time complexity of the algorithm for determining whether the point is in a polygon is O (n).

Another algorithm is to use the symbolic area of the triangle and polygon area compared to the algorithm, because of the use of floating point arithmetic so will bring a certain error, do not recommend everyone to use.

To determine whether a segment is within a polygon:

A necessary condition of a segment in a polygon is that the two endpoints of the line are all within the polygon, but because the polygon may be concave, this cannot be a sufficient condition for judgment. If the segment and the polygon of a certain edge of the intersection (two line is the intersection of two segments and intersections are not two segments of the endpoint), because the edge of the polygon is divided between the left and right sides of the polygon and the different parts, so the segment must be part of the polygon (see Figure A) So we get the second necessary condition of the segment in the polygon: the line segment and all edges of the polygon are not handed in.

The line segments and polygons that intersect the ends of a segment do not affect whether the segment is within the polygon, but if one of the vertices and segments of a polygon intersects, it is also necessary to determine whether the line segment between the two adjacent intersections is contained within the polygon (see Figure B for the inverse example).

So we can first find all the intersection of the vertices of the polygon, and then in accordance with X-y coordinates (x coordinates small row in front, for the x coordinates of the same point, the Y coordinate small row in front, this sort of criterion is to ensure that the level and vertical situation is correct), Such two adjacent points are adjacent to the two intersections on the line, if the midpoint of any adjacent two points is also within the polygon, then the segment must be within the polygon.

Proved as follows:
 

Proposition 1:
If the line segment and the two adjacent intersection points of the polygon are P1, the midpoint p ' of the P2 is also within the polygon, then P1, all the dots between P2 are within the polygon.

Prove:
Suppose the P1,P2 contains points that are not inside the polygon, it may be advisable to set the point to Q, between P1, P ' because the polygon is a closed curve, so the inner and outer bounded, while the P1 belongs to the multilateral line, q belongs to the outside of the multilateral, p ' belongs to the multilateral interior, P1-q-p ' completely contiguous, so p1q and Qp ' Must cross the boundary of the polygon, so there are at least two intersection points between the line and the polygon in the P1,p ', which is contradictory to the p1p2, so the proposition is set up. Certificate of Completion.

By Proposition 1 direct inference can be drawn:
Inference 2:
The intersection point of the polygon and line PQ is P1,P2,...... Pn, where Pi and pi+1 are adjacent intersections, the necessary and sufficient condition of the line PQ in the polygon is: P,q within the polygon and for I =1, 2,......, n-1,pi, and the midpoint of the pi+1 is also within the polygon.

In actual programming, it is not necessary to compute all the intersections, first of all, it is necessary to determine whether the edges of segments and polygons are in-line, if the segments and polygons in a certain edge of the line segment must be outside the polygon; if the line segment and the polygon of each side are not in, then the intersection line and the polygon must be the end of the line or the Just determine if the point is on the line.

Now we have the following algorithm:

    The endpoints of If line-end PQ are not then return false in polygons 
      ;
    The point set pointset initialized to empty;
    An endpoint of the s do if segment of a for polygon is
      then on S
           to add the endpoint to Pointset;
         If an endpoint of else if S is then on the line PQ,
           the endpoint is added to the Pointset;
         else if s and the segment PQ intersect//This time it is definitely within the
           then return false;
    The points in the Pointset are sorted according to the X-y coordinate;
    For each two adjacent points in the for Pointset Pointset[i], pointset[i+1]
      , the midpoint of Pointset[i pointset[] is not in the polygon
           i+1 return false; return
    true;

In this process, because the number of intersections is certainly far less than the number of vertices of the polygon N, so most is the complexity of the constant level, almost negligible. Therefore, the time complexity of the algorithm is also O (n).

To determine whether a polyline is within a polygon:

Just determine whether each line segment of the polyline is within the polygon. The time complexity of the algorithm is O (m*n) If the polyline has an M line segment and the polygon has n vertices.

To determine whether a polygon is inside a polygon:

Just judge 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.

To determine whether a rectangle is inside a polygon:

Converts a rectangle into a polygon and then determines whether it is inside a polygon.

To determine whether a circle is within a polygon:

As long as the shortest distance between the center of the circle and each edge of the polygon is calculated, the circle is within the polygon if the distance is greater than or equal to the radius of the circle. The algorithm of calculating the shortest distance between the center of the circle and the polygon is described in the later article.

To determine whether a point is within a circle:

Calculates the distance from the center of the circle to that point, or within the circle if it is less than or equal to the radius.

To determine whether a segment, polyline, rectangle, or polygon is within a circle:

Because a circle is a convex set, you just have to judge whether each vertex is within a circle.

To determine whether a circle is within a circle:

Set two circle for O1,o2, the radius is R1, R2, to judge whether O2 in O1. First compare the size of the R1,R2, if R1

Calculates the nearest point to a line segment:

If the segment is parallel to the x-axis (y-axis), the point is the vertical line in which the line is located. Pedal is very easy to find, and then calculate the pedal, if the pedal on the line to return pedal, otherwise the end of the pedal near the endpoint; If the line line is not equal to the x-axis and not parallel to the Y axis, the slope exists and is not 0. The ends of the line segment are 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. The slope of the vertical is-1/k, and the vertical equation is: y = ( -1/k) * (x-point.x) + Point.y.

The two linear equations are obtained: 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 whether the pedal is on the line segment, If the pedal is returned on a line segment, if the distance from the ends to the pedal is not computed, select the endpoint that is closer to the pedal to return.

Calculates the point to the nearest point of a polyline, rectangle, or polygon:

As long as the nearest point to each line is computed, the nearest distance is recorded, and the nearest point is the smallest.

Calculates the nearest and intersection coordinates of a point to a circle:

Returns undefined if the point is at the center of the circle, because the distance between the center of the circle and any point in the circumference is equal.

The connection point P and center O, if the PO is parallel to the x-axis, calculates the nearest point's horizontal axis as Centerpoint.x-radius or centerpoint.x + radius, depending on whether P is on the left or right of O. If the PO is parallel to the Y axis, the ordinate of the nearest point is calculated according to p at the top or bottom of O centerpoint.y-+radius or Centerpoint.y-radius. If the PO is not on the x-axis and the Y axis, the PO slope is present and not 0, and the line po slope is k = (P.Y-O.Y)/(p.x-o.x). The equation for the line po is: y = k * (x-p.x) + p.y. The circle equation is as follows: (x-o.x) ^2 + (y-o.y) ^2 = R ^2, the two equations can solve the intersection point between the straight Po and the circle, and take the intersection points which are closer to the P spot.

Calculates the intersection point of a two-line line segment:

For two collinear line segments, the position relationship between them has several scenarios as shown in the following illustration. There is no intersection between the two segments in figure (a), the two segments in Fig (b) and (d) have an infinite focus, and the two segments in figure (c) have one intersection point. Set LINE1 is a long line of two line segments, Line2 is a shorter one, if the line1 contains the line2 two endpoints, then the case of graph (d), two segments have an infinite intersection; if the line1 contains only one endpoint of the line2, So if one of the endpoints of the line1 equals the endpoint of the line2 contained in the line1, is the case of figure (c), when the two segments have only one intersection point, otherwise is the case of Fig (b), two segments are also the intersection of Infinity; if line1 does not contain any endpoints of line2, then figure (a) , there is no intersection point between the two segments.

To compute the intersection of a segment or line with a line segment:

Set a line segment to L0 = P1p2, another line segment or line is L1 = Q1Q2, to calculate is the intersection of L0 and L1.

1. First of all, to determine whether L0 and L1 intersect (method has been discussed in the previous article), if there is no intersection, there is no point, otherwise the L0 and L1 must have intersection, the following will be L0 and L1 as a straight line to consider.

2. If the P1 is the same as the P2, the L0 is parallel to the Y axis.

A) If the L1 is also parallel to the Y axis,

I. If the ordinate of the P1 is the same as the ordinate of the Q1, explain L0 and L1 collinear, if L1 is a straight line, they have an infinite intersection, if the L1 is a line of words can be "calculate the intersection of two collinear line segments" algorithm to seek their intersection point (this method has been discussed in the previous article);
Ii. otherwise the L0 and L1 are parallel, they have no intersection point;

b If the L1 is not in the Y axis, then the horizontal axis of the intersection is P1, and the nodal ordinate can be computed by substituting the L1 linear equation;

3. If the P1 and P2 horizontal axes are different, but the Q1 and Q2 are the same, that is, the L1 parallel to the Y axis, the horizontal axis of the intersection is Q1, and the nodal ordinate can be computed in the linear equation of the L0.

4. If the P1 is the same as the P2 ordinate, that is, L0 parallel to the X axis

A) If the L1 is also parallel to the X axis,

I. If the horizontal axis of the P1 is the same as that of the Q1, explain L0 and L1 collinear, if L1 is a straight line, they have an infinite intersection, if the L1 is a line of words can be "calculate the intersection of two collinear line segments" algorithm to seek their intersection point (this method has been discussed in the previous article);
Ii. otherwise the L0 and L1 are parallel, they have no intersection point;

b If the L1 is not in the X axis, then the ordinate of the intersection is P1, and the intersection axis can be calculated by substituting the L1 linear equation.

5. If the P1 and P2 ordinate different, but Q1 and Q2 ordinate the same, that is, L1 parallel to the x axis, the intersection point ordinate to Q1 ordinate, substituting into the L0 linear equation can calculate the intersection axis;

6. The rest of the situation is that the slopes of L1 and L0 are present and not 0.

(a) Calculate the slope K1 of the L0 slope k0,l1;

b) If K1 = K2

I. If the Q1 on the L0, then the L0 and L1 collinear, if the L1 is a straight line there are infinite intersection, if the L1 is a line of words can be "calculate the intersection of two collinear line segments" algorithm to seek their intersection point (this method has been discussed in the previous article);
Ii. if Q1 is not on the L0, then L0 and L1 are parallel, they have no intersection point.

c) The equations with two straight lines can solve the intersection point to

This algorithm is not complicated, but it should be discussed in detail, especially when the two line line of the situation needs to be considered separately, so in the previous article will seek two collinear line segment algorithm alone write out. In addition, first of all, the use of vector fork to determine whether the segment and line segments (or lines) intersect, if the result is intersection, then you can consider all lines in the following line. Note that we can rewrite the line or segment equation as ax+by+c=0, so that some of the steps in the above process can be merged to shorten the length of the code, but this algorithm will take more time because of the first argument required.

To find the intersection point of a segment or line with a polyline, rectangle, or polygon:

The intersection of each edge can be obtained separately.

To find the intersection point of a segment or line with a circle:

Set the center of the Circle to O, the radius of the circle is R, the two points on the line (or segment) L are P1,P2.

1. If L is a line segment and the P1,P2 is contained within the circle O, there is no intersection, otherwise proceed to the next step.

2. If L is parallel to the Y axis,

A the distance dis is calculated from the center to L;
b if the dis > R then L and the Circle have no intersection points;
(c) Using the Pythagorean theorem, we can find two intersection coordinates, but we should pay attention to the tangent of L and the circle.

3. If L is parallel to the X axis, the procedure is similar to that of L parallel to the Y axis;

4. If l is neither parallel x axis nor parallel y axis, we can find the slope K of L, then list the point-oblique equation of L, and the circle equation can solve the two intersections of L and Circle immediately.

5. If L is a line segment, the intersection of the 2,3,4 is also to determine whether it belongs to the range of the segment.

The concept of convex package:

The convex package (convex hull) of a point set Q is a minimum convex polygon that satisfies the point in Q or on the edge of a polygon or therein. The polygon represented by the red segment in the following figure is the convex package of the point set Q={p0,p1,... P12}.

Convex bag of the method:

It has been proved that the time complexity of convex hull algorithm is O (N*LOGN), but when the vertex number of convex bundle is also considered, the Krikpatrick and Seidel pruning search algorithm can reach O (n*logh) and achieve optimal in the gradual sense. The most commonly used convex algorithm is the Graham scan method and the Jarvis step method. This article simply introduces the Graham scanning method, the proof of its correctness and the process of Jarvis step method, we can refer to the introduction of algorithm.

For a point set with three or more points, the process of Q,graham scanning is as follows:

Make P0 the smallest point in the order of y-x coordinates in Q
Sets the set of points to be sorted counterclockwise for the rest of the p0-centric polar angle (if there are multiple points that have the same polar angle, remove all except the point farthest from the P0)
Pressure p0 into the stack s
Pressure P1 into the stack s
Pressure P2 into the stack s
For I←3 to M
The next element of the top element of S's stack, the top element of S, and the folding segment of Pi are not turned to the left.
On the S-bomb stack
Pressure pi into stack s
return S;
 

After this procedure is executed, the element from bottom to top of the stack s is the sequence of points in a counterclockwise order of convex package vertices of Q. Note that when we sort the dots counterclockwise, we don't need to really find the polar angle, just ask for the order of any two points. This step can be achieved by using the vector cross product properties mentioned above.

Four, the conclusion

Although human research on geometry has not been interrupted since ancient times, the study of solving geometrical problems with the help of computers is still only at an early stage, and computational geometry is worthy of our careful study and application in terms of both application and development prospects. I hope this article will bring you into this colorful world.

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.