【definition】
[] Polar angle polar coordinate system phi, it can be directly regarded as the straight line slope, since the angle of the slope may be expressed as
Computational Geometry is the use of computer code to simulate the real value and the geometric calculation process, so you need to create the corresponding elements, and the most basic element is the point, and the point position can be expressed in two-dimensional space
In the same two-dimensional space, storage methods and vectors point similar (but actually very different meaning)
struct V
{
double x, y;
double the;
double angle() {// polar angle is obtained
return atan2(y, x);
}
V(double X = 0, double Y = 0) { //初始化 x = X, y = Y; ang = atan2(y, x); } bool operator ==(const V &b) { return cmp(x - b.x) && cmp(y - b.y); } };
By renaming make some points and vectors coincide on some usage
typedef V P;
Defined operation (calculation points and vectors of the individual have the coincident)
V operator +(V a, V b) { return V(a.x + b.x, a.y + b.y); }
V operator -(V a, V b) { return V(a.x - b.x, a.y - b.y); }
V operator *(V a, double b) { return V(a.x *b, a.y*b); }
V operator /(V a, double b) { return V(a.x / b, a.y / b); } // cross product
double cross(V a, V b)
{
return a.x*b.y - a.y*b.x; } //点积 double dot(V a, V b) { return a.x*b.x + a.y*b.y; }
According to the knowledge of geometry in high school, we know two points can be represented by a line segment
So there are segments defined
struct L
{
P s, t;
double the;
L(P X = V(), P Y = V())
{
s = X, t = Y, ang = (Y - X) .angle ();
}
};
Through their respective operations are a variety of requirements intersection, parallel, cross-product, dot product obtaining complete
(Proved relatively simple)
By comparing the slope of the line size //
bool operator <(const L &a, const L &b)
{
double r = reasonable.The - B;
if (cmp(r) != 0) return cmp(r) == -1;
// same polar angle, the greater the default partial
return cmp(cross(a.t - a.s, b.t - a.s)) == -1;
}
Analyzing line parallel //
bool is_parallel(L a, L b)
{
return cmp(cross(a.t - a.s, b.t - b.s)) == 0; } //查找交点 P intersection(L a, L b) { return a.s + (a.t - a.s)*(cross(b.t - b.s, a.s - b.s)) / cross(a.t - a.s, b.t - b.s); }
Determined surface may have a plurality of segments or a plurality of points determined
Polygon area [seeking]
Is the cross product can be represented by: two line segments into a parallelogram area
Then one by one is obtained by the polygon edge and an area surrounded origin
Because the cross product of both positive and negative,
This principle can be obtained by polygon area
double area(P *p, int n)
{
double res = 0;
p[n + 1] = p[1];
for (int i = 1; i <= n; i++)
res += cross(p[i], p[i + 1]);
return fabs(res / 2); }
There are a variety of operations between these elements is determined and
Point is in the linear side
bool is_right(L a, P b)
// determines whether the point b (and even into the starting point and the linear vector direction opposite to the vector direction) on the right side of a straight line
{
return cmp(cross(a.t - a.s, b - a.s)) < 0;
}
Seeking whether the points within the polygon
By calculating the number of sides is equal on both sides of the point, if they are equal then the points within the polygon
Meanwhile, the point is calculated on a straight line, find the point where the straight line
Seeking two vectors intersect
Through the above two knowledge points:
Possible to find two polygons intersect