Computational Geometry Basics

Source: Internet
Author: User

1. Basis of the Foundation

1. Point and Vector.

The dots and vectors are represented in X, y two coordinates, so we do not make a distinction on the program at the moment.

But note:

(1). dot + vector = point;

(2). vector + vector = vector (vector-vector = vector + (opposite vector));

(3). Point-point = vector;

(4). dot + dot no meaning;

where PT (point) Represents the DOT, VT (vector) represents the vector.

1 structpt{2     Doublex, y;3PointDoublex=0,Doubley=0): X (x), Y (y) {}4 };5 typedef PT VT;6Vtoperator+ (vt A,vt b) {returnVT (a.x+b.x,a.y+b.y); }7Vtoperator-(PT A,pt b) {returnVT (a.x-b.x,a.y-b.y); }8Vtoperator* (VT A,Doublex) {returnVT (a.x*x,a.y*y); }9Vtoperator/(VT A,Doublex) {returnVT (a.x/x,a.y/x); }
View Code

2. Comparison function of points

It will be used when some sort is needed.

1 BOOL operator < (const PT &a,constreturn a.x<b.x| | (A.X==B.X&&A.Y<B.Y); }
View Code

3. The function of judging the point equality and the three-state function of avoiding error

Floating-point errors always exist, so we use tri-state function dcmp to reduce the problem of precision.

1 Const Doubleeps=1e-8;2 intDCMP (Doublex) {if(Fabs (x) <eps)return 0;Else returnx<0?-1:1; }3 BOOL operator== (ConstPT &a,ConstPT &b) {returnDCMP (a.x-b.x) = =0&AMP;&AMP;DCMP (a.y-b.y) = =0; }
View Code

4. The polar angle of the vector

The polar angle of the vector represents the angle that is required to rotate from the positive half axis of the x-axis to the vector.

Using the ATAN2 function in the C standard library, the unit is radians.

1 atan2 (y,x);
View Code

2. Basic operations

1. dot product and cross product of vectors

$ $cross (A, b) =a.x*b.y-a.y*b.x$$

How did this come about? We compare the slope of the line where the two vectors are located, and if the slope of the B-vector line is large (the slope of two vectors exists), then there is $$\frac{b.y}{b.x}>\frac{a.y}{a.x}$$

Then go to the denominator $ $a. x*b.y>a.y*b.x$$

We notice that the line where the B vector is located is in the right-hand direction of the straight line of the A vector (a in the left-hand helix direction of B). If the slope of the line is smaller than the b vector, the B-vector line is in the left-hand helix direction of the line where a vector is located (a in the right-hand If the equal sign is the same as the slope, then the two lines parallel the vector, A, a, and a collinear line.

But this is the case where the slope of both lines exists. Let's discuss the situation where the slope does not exist:

(1). The slope of the line where a is located does not exist as \ (a.x=0\).

Wakahara inequality is still established as \[0>a.y*b.x\], and then discuss \ (a.y\) positive and negative, you can prove that the line B is still located in a straight line of the right hand helix direction.

(2). The slope of the line where B is located does not exist (b.x=0\).

The Wakahara inequality is still immediately $ $a. x*b.y>0$$, and then discuss \ (b.y\) The positive and negative, you can prove that the line B is still located in a straight line of the right hand helix direction.

(3). b The slope of the straight line does not exist, that is \ (a.x=b.x=0\).

At this time inequality is taken, parallel.

In conclusion, the corresponding conclusions of the original inequalities are always established. So we can use the cross product to judge the left and right positions of the two lines.

In addition, the geometrical meaning of the cross product is the parallelogram area of the adjacent edge of the two vectors (the usable cosine theorem proves), which is twice times the triangular area.

Why is there an area of direction? Because by definition we can easily find \ (cross (A, b) =-cross (b,a) \), so it will be signed!

1 Double return a.x*b.x+a.y*b.y;} 2 Double return a.x*b.y-a.y*b.x;} 3 Double return Cross (B-A,C-A); }
View Code

2. Modulus of the vector

1 Double return sqrt (dot (a,a)); }
View Code

3. Angle of two vectors

The cosine of the angle is computed by the dot product, and then a inverse trigonometric function is used.

1 Double return ACOs (dot (a)/length (a)/length (b)); }
View Code

4. Rotation of the vector.

Rotates a vector counterclockwise (\alpha\) radians around the starting point.

How does it work?

Let's start by translating the starting point of the vector to the origin of the coordinates. Write the polar coordinates of the end of the vector: $ $x =rcos\theta$$

$ $y =rsin\theta$$

The polar coordinates of the end point after the rotation are:

$ $x ' =rcos (\theta+\alpha) $$

$ $y ' =rsin (\theta+\alpha) $$

Open to get:

$ $x ' =r (Cos{\theta}cos{\alpha}-sin{\theta}sin{\alpha}) =xcos{\alpha}-ysin{\alpha}$$

$ $y ' =r (Sin{\theta}cos{\alpha}+cos{\theta}sin{\alpha}) =xsin{\alpha}+ycos{\alpha}$$

That

$ $x ' =xcos{\alpha}-ysin{\alpha}$$

$ $y ' =xsin{\alpha}+ycos{\alpha}$$

1 vt rotate (vt A,doublereturn VT (A.x*cos (RAD)-a.y*sin (RAD), A.x*sin (RAD) +a.y*cos (RAD))}
View Code

5. plural

Petition mentioned, but I don't know what to do ... Seemingly also relatively slow, FFT is handwritten, regardless of.

3. Points and Lines

1. Intersection of segments

Since this is the most frequently occurring problem, we put it in the first one.

First, define the canonical intersection: two segments intersect and the intersection is not any of the four endpoints.

In solving the problem of whether line segments intersect, it is most likely to think of analytic geometry. But the slope-related point oblique and oblique-cut type are not used, if the general or two-point type need to solve the equation, and then determine whether there is a solution, if there is a solution to determine whether in the two segments inside. .

We take a magical approach to the problem of not requiring the intersection coordinates to be obtained.

First lemma: two segments \ (ab\) and \ (cd\) specification intersect \ (\longleftrightarrow\) \ (\overrightarrow{ac}\) and \ (\overrightarrow{ad}\) respectively in \ (\ overrightarrow{ab}\) on both sides and \ (\overrightarrow{ca}\) and \ (\overrightarrow{cb}\) respectively on the left and right sides of \ (\overrightarrow{cd}\)

Computational Geometry Basics

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.