Introduction to algorithms-Computational Geometry)

Source: Internet
Author: User

Computational Geometry is a very interesting topic. Unfortunately, I am not a major in mathematics/computer science, and I have little knowledge of this field. The first time I came into contact with this word was in the topcoder's algorithm tutorial, which attracted me by its clever algorithm. Unfortunately, I have no chance to learn more. This article introduces only the simplest 2D Geometric algorithms. It is a required course for developers engaged in CAD software development, graphic development, statistics, and game development. There is a well-known entry book in this field: Mark de Berg () <computational geometry: algorithms and application 2nd Edition> (published in China, computational Ry-Algorithm and Application version 2, translated by Deng Junhui ).

The objects in the calculation ry can be point sets, line segment sets, and surface sets. If the endpoint of a line segment is ordered, we convert this line segment into a directed line segment ). If the start point P1 of the directed line P1P2 is at the coordinate origin, we can call it vector/vector (vector) P2.

Vector operations:

Vector operations are often used in geometric calculation. If two-dimensional vectors P = (x1, Y1) and q = (X2, Y2) are set, the vector addition is defined as p + q = (X1 + X2, Y1 + y2 ), similarly, vector Subtraction is defined as p-q = (x1-X2, Y1-Y2 ). Obviously, p + q = q + p, p-q =-(Q-P ).

Dot Product)It is also called inner product ). set two vectors in 3D space, P (A1, A2, A3), Q (B1, B2, B3), P · q = a1b1 + A2B2 + a3b3. ry meaning: | p | q | cos (PQ angle ).
When the point multiplication of two vectors is 0, that is, the two vectors are perpendicular to each other.

Cross Product)If the vector p = (x1, Y1), q = (X2, Y2) is set, the vector cross product is defined as (0, 0) the signed area of the parallelogram consisting of P1, P2, and P1 + P2, that is, p x q = x1 * Y2-X2 * Y1, the result is a scalar (for more information about the cross product of the higher dimension, see the mathematical book). The geometric meaning is | p | q | sin (PQ angle ). Obviously, the properties include P x q =-(q x P) and P x (-q) =-(P x q ). In general, all vertices in the following algorithms are considered as vectors without any explanation. The addition and subtraction of two vertices is the addition and subtraction of vectors, and the multiplication of vertices is considered as the cross product of vectors.

A very important property of the cross product is that it can be used to determine the clockwise relationship between the two vectors. If p × q> 0, P is in the clockwise direction of Q. If it is less than 0, it is counter-clockwise. If it is equal to 0, it may be in the same direction/reverse direction.

Determining Point online segments

Set the vertex to Q, and the line segment to AB. The basis for determining the vertex Q in a straight line is ∠ QAB = 0 or 180: (q-a) × (B-a) = 0. Then determine whether the vertex is in the online segment. Min (XA, XB) <= XQ <= max (XA, XB) and min (ya, Yb) <= yq <= max (ya, Yb ). (Considering Vertical and horizontal line segments, the two conditions must be set at the same time ).

Line Segment / Straight line intersectionSet the two-point coordinate p1 (x1, Y1) and P2 (X2, Y2) of a line segment. The linear equation composed of this line segment can be expressed: Ax + by = C WhereA = y2-y1
B = x1-x2
C = A * X1 + B * Y1 so two line segments can determine two linear equations. A1x + b1y = C1 A2x + b2y = c2After resolving the linear equation, you can find the intersection of the straight lines, det = A1 * B2-A2 * b1if (det = 0) {Line Segment parallel} else // obtain the intersection coordinate {x = b2 * C1-B1 * C2/det y = A1 * C2-A2 * C1/det} for line segments, you only need to determine whether the intersection coordinates are in the two line segments.

Distance from point to line segment

Let us assume that the distance from line AB to C to AB is the height of the triangle CAB. Assume that the projection of C on the AB or extended line is H. We know that there are two ways to calculate the Triangle Area: (1) 1/2 (AB * Ch), or 1/2 AC * sin × cab = 1/2 | AC × AB |. therefore, the distance is Ch = | AC × AB |/| AB |.

Depth of the point online segment

Connect to a topic and find the coordinates of H. When the line segment AB determines the straight line px + Qy = R, the vertical line equation is-qx + py = T. take the coordinates of point C into the value of t to get the equation, and then find the intersection of the straight line. If you want to obtain the symmetric point S of the AB, S = H-(c-h)

Point to the nearest point of a line segment

The nearest point from point C to line AB should first judge whether the online segment AB is on C, and if so, the closest point is C. Then judge which side of AB is C. The Method for Determining the angle by Using Point multiplication. If (AB · BC> = 0) // returns ABC blunt angle {on side B, closest point: B} If (Ba · AC> = 0) // sort BAC blunt angle {on side A, closest point is a} else // H in AB {closest point is vertical foot H, calculate h according to the above method}

Line Segment turning Problem

Set the line segment ABC and seek the turn. You can use the cross multiplication function to determine the direction. If the value of AC × AB is greater than 0, the line segment is right-turned, and the value is less than 0, and the value is equal to 0.

Polygon Area

A sequence of point sets is given, and each point is connected in order to combine a polygon to obtain the polygon area. A polygon can be divided into the sum of several triangle areas. When the distance from the point to the straight line is obtained, the area of the triangle can be calculated using vector cross-multiplication. Because the line segment turns to change the positive and negative signs of the Cross multiplication, and the number of turns of the closed image must be an even number, this method is also applicable to the concave polygon (positive and negative offset, readers can prove it by themselves ). Assuming that a polygon is connected by the ai sequence of the point set, the polygon area S = | Σ (AI + 1-ai) × (AI + 2-ai + 1) |/2. that is, start with the first vertex, and multiply the adjacent vectors in sequence (pay attention to the vector direction, and then subtract the previous vertex from the next vertex) and then sum. For (I from 0 ~ N) {vecp <-- A [I + 1]-A [I] vecq <-- A [I + 2]-A [I + 1] Cross <-- vecp × vecq area <-- area + cross}

Points within a polygon

Determine whether point P is within the polygon determined by the sequence Point Set AI. A line segment long enough (far longer than any side) to simulate a ray of a point over P can generate a large enough Q. Determines the number of times the PQ and the polygon edges intersect. If the even number is, P is outside the polygon, and the odd number is within the polygon (as proved by the reader ). We also need to determine whether point P is on the edge.

Q <-- random () * 1000 + 1000 for (I from 0 ~ N) {VEC = A [I + 1]-A [I] If (isonsegment (p, VEC) = true) // {return edge} on the online segment if (intersect (PQ, VEC) = true) // intersection {CNT <-- CNT + 1 }}// end loop if (CNT is an even number) {return outside} If (CNT is an odd number) {return inside}

Convex Hull(Convex hull)

An ordered point set AI is used to find the smallest Convex Polygon and include all vertices. This polygon is called a convex hull ). There are many methods for finding convex packets. Here we only introduce one method. For a set of Q with three or more points, the process of Graham scanning is as follows:
Set <P1, P2 ,... PM> This is the point set that is obtained by sorting the rest of the points alphabetically by the Polar Angle centered on P0 (if multiple points have the same polar angle, all except the Farthest Points from P0 are removed
Press P0 into Stack s
Press P1 into Stack s
Press P2 into Stack s
For I need 3 to m
Do While consists of the next element of S's stack top element, S's stack top element, and PI line segment without turning to the left
Stack s
Press PI into Stack s
Return s; after this process is executed, the elements from the bottom to the top of stack s are the point sequence of the convex packet vertices of Q arranged counterclockwise. Note that we do not need to find the polar angle in the order of any two points when we sort the points alphabetically by the polar angle. This step can be achieved by the aforementioned vector cross product.

Other topics

1. Orthogonal Region Search (database query ). The database seems to be different from that of ry. In fact, we can map the database record field to a dimension. Each record is considered as a point in the K-dimensional space. Assume that the data table has two fields, which constitute a 2D space. The orthogonal interval query is to specify a condition interval to find records that meet the conditions. In essence, it is a square generated based on conditions and vertices in the Square (if there is 3D space, it is a cube). The method is to construct a search tree. For details, see the computation ry book. 2. Triangle. It is widely used to split areas into triangle sets. For example, a routing algorithm used in a wireless sensor network.

 

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.