2D Computational Geometry Fundamentals

Computation in computational geometry is based on coordinate operations in vectors, and in computational geometry we need to define some concepts. Since it is a coordinate representation, points in computational geometry can also represent vectors. For each point and vector, we record their coordinates. The operations of addition, subtraction, multiplication and division are also the same as in mathematics.

The dcmp in it solves the precision problem.

The following code shows the basic definition of vector operations.

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm> 
using namespace std;
const int MAXN=50005;
struct Point{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){};
};
typedef Point Vector;
Vector operator +(Vector A,Vector B){return Vector(A.x+B.x,A.y+B.y);}
Vector operator -(Point A,Point B){return Vector(A.x-B.x,A.y-B.y);}
Vector operator *(Vector A,double p){return Vector(A.x*p,A.y*p);}
Vector operator /(Vector A,double p){return Vector(A.x/p,A.y/p);}
bool operator <(const Point& a,const Point& b){
    return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
const double eps=1e-10;
int dcmp(double x){
    if(fabs(x)<eps) return 0; else return x<0?-1:1;
}
bool operator ==(const Point& a,const Point &b){
    return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
bool operator <=(const Point& a,const Point &b){
    return a<b||a==b;
}

Then we will look at the basic operations in computational geometry.

1. Dot product

dot product, i.e. a · b = | a | | b | c O s a , which is still consistent with the method of operation in mathematics

double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}

Through the dot product operation, we can calculate some things about the vector, such as finding the length of the vector

double Length(Vector A){return sqrt(Dot(A,A));}

Furthermore, the angle between the two vectors can be found

double Angle(Vector A,Vector B){return acos(Dot(A,B)/Length(A)/Length(B));}

2. Cross product

The cross product, that is, twice the directed area of ​​the triangle formed by the two vectors, is the directed area of ​​the parallelogram formed by them. Note that it is directed.

double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}

The cross product is often used to judge the positional relationship of two straight lines or points, so it is worth mentioning the positive and negative issues of the cross product.
For Cross(v,w), when w is to the left of v, the cross product is positive, and when w is to the right of v, the cross product is negative. That is, look at the relationship between the first vector and the second vector. The second vector is positive when it is on the left, and the left is positive and the right is negative. Be sure to remember it well.

The cross product can be used to calculate the area of ​​a triangle, which is twice the area of ​​the triangle

double Area2(Point A,Point B,Point C){return Cross(B-A,C-A);}

The following are some commonly used calculations

rotate

Vector Rorate(Vector A,double rad){
    return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}

Calculate the normal of a vector

Vector Normal(Vector A){
    double L=Length(A);
    return Vector(-A.y/L,A.x/L);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324951936&siteId=291194637