计算几何基础1
写在前面
建议不要看这篇文章,因为:
- 这是本juruo的第一篇blog
我自己还没学完呢- 这原来只是我为了提升自己水平写的(众所周知给别人讲解可以吸收90%的芝士)
因此,请划走,不要浪费你宝贵的时间。
在学习计算几何学之前,我们首先需要了解高中的几何知识。接下来默认大家都已经学完高中数学了。
几何中的基本元素
点与向量
点的存储
我们都知道,平面直角坐标系中的点可以由有序数对来表示。因此,我们可以得到表示点的结构体:
struct Point
{
double x,y;
Point() {x=y=0;}
void read() {cin >> x >> y;}
void print() {cout << x << ' ' << y << endl;}
};
点的判等
由于我们的 Point 存储的是浮点数,因此在判断两个点是否相等时,不能直接使用==;正确的做法是设一个极小值 EPS ,当两个点的x坐标差的绝对值(y坐标同理)小于 EPS 时,便认为它们相等。
代码如下:
#define EPS (1e-10)
//...
struct Point
{
double x,y;
Point() {x=y=0;}
void read() {cin >> x >> y;}
void print() {cout << x << ' ' << y << endl;}
bool equal(Point t) {return (abs(x-t.x)<EPS && abs(y-t.y)<EPS);}
};
向量的存储
向量有很多表示方法。为了方便,我们将向量考虑为平面直角坐标系中原点
向量的判等与点的判等类似,都是看是否小于 EPS 。
表示向量的结构体如下:
struct Vector
{
double x,y;
Vector() {x=y=0;}
void read() {cin >> x >> y;}
void print() {cout << x << ' ' << y << endl;}
bool equal(Vector t) {return (abs(x-t.x)<EPS && abs(y-t.y)<EPS);}
};
向量的加减法
根据 Vector() 结构体内部):
Vector add(Vector t)
{
t.x=x+t.x;
t.y=y+t.y;
return t;
}
Vector sub(Vector t)
{
t.x=x-t.x;
t.y=y-t.y;
return t;
}
向量乘标量
我们知道,Vector() 结构体内部):
Vector mult(double k)
{
Vector p;
p.x=x*k;
p.y=y*k;
return p;
}
向量的范数与模
我们发现,若
由于范数是表示向量的模的平方,因此先求出范数更为简便。
代码如下(它们都位于 Vector() 结构体内部):
double norm() {return (x*x+y*y);}
double length() {return sqrt(norm());}
向量基本运算代码
struct Point
{
double x,y;
Point() {x=y=0;}
void read() {cin >> x >> y;}
void print() {cout << x << ' ' << y << endl;}
bool equal(Point t) {return (abs(x-t.x)<EPS && abs(y-t.y)<EPS);}
};
struct Vector
{
double x,y;
Vector() {x=y=0;}
void read() {cin >> x >> y;}
void print() {cout << x << ' ' << y << endl;}
Vector add(Vector t)
{
t.x=x+t.x;
t.y=y+t.y;
return t;
}
Vector sub(Vector t)
{
t.x=x-t.x;
t.y=y-t.y;
return t;
}
Vector mult(double k)
{
Vector p;
p.x=x*k;
p.y=y*k;
return p;
}
double norm() {return (x*x+y*y);}
double length() {return sqrt(norm());}
bool equal(Vector t) {return (abs(x-t.x)<EPS && abs(y-t.y)<EPS);}
};
bool operator==(Point a,Point b) {return a.equal(b);}
Vector operator+(Vector a,Vector b) {return a.add(b);}
Vector operator-(Vector a,Vector b) {return a.sub(b);}
Vector operator*(Vector a,double k) {return a.mult(k);}
bool operator==(Vector a,Vector b) {return a.equal(b);}
Point makePoint(double x,double y)
{
Point p;
p.x=x;p.y=y;
return p;
}
Vector makeVector(double x,double y)
{
Vector p;
p.x=x;p.y=y;
return p;
}
向量的内积
自己画的,凑活着看吧。
根据余弦定理,我们可以发现:
把它带入向量的内积(点乘)公式中,
计算向量内积的代码如下(它不在 Vector() 结构体内部)。注意,dot() 的返回值为实数而非向量。
double dot(Vector a,Vector b)
{
return a.x*b.x+a.y*b.y;
}
向量的外积
我们知道,两个向量的外积也是一个向量。但是,这个向量是存在于空间中的,我们无法存储它。因此,我们的 cross() 函数的返回值是一个实数,它只计算
虽然最后计算出的是一个实数,但我们过程中还是要把
且
x、y、z轴上的单位距离分别为
接下来就可以计算
由于我们要计算的只是它在二维平面上的模,即
又
计算向量外积的函数如下。仍需注意,cross() 的返回值为
double cross(Vector a,Vector b)
{
return a.x*b.y-a.y*b.x;
}
其他二维物体
线段和直线
我们知道,两点确定一条线段/直线。因此,存储线段/直线的结构体如下:
struct Segment
{
Point p1,p2;
void read() {p1.read();p2.read();}
void print() {p1.print();p2.print();}
};
struct Line
{
Point p1,p2;
void read() {p1.read();p2.read();}
void print() {p1.print();p2.print();}
};
有关线段、直线的东西还有很多,下一篇再讲。(链接在最后)
圆与多边形
圆可以由一个圆心和一个半径确定;多边形可以由点的序列表示。
struct Circle
{
Point c;
double r;
Circle() {r=0;}
void read() {c.read();cin >> r;}
void print() {c.print();cout << r << endl;}
};
struct Polygon
{
int sides;
Point pts[105];
Polygon() {sides=0;}
void read(){cin >> sides;for(int i=1;i<=sides;i++) pts[i].read();}
void print(){cout << sides << endl;for(int i=1;i<=sides;i++) pts[i].print();}
};
同样的,关于圆与多边形的还有很多,暂时先不讲了。
平面几何基本运算完整代码
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#define EPS (1e-10)
using namespace std;
struct Point
{
double x,y;
Point() {x=y=0;}
void read() {cin >> x >> y;}
void print() {cout << x << ' ' << y << endl;}
bool equal(Point t) {return (abs(x-t.x)<EPS && abs(y-t.y)<EPS);}
};
struct Vector
{
double x,y;
Vector() {x=y=0;}
void read() {cin >> x >> y;}
void print() {cout << x << ' ' << y << endl;}
Vector add(Vector t)
{
t.x=x+t.x;
t.y=y+t.y;
return t;
}
Vector sub(Vector t)
{
t.x=x-t.x;
t.y=y-t.y;
return t;
}
Vector mult(double k)
{
Vector p;
p.x=x*k;
p.y=y*k;
return p;
}
double norm() {return (x*x+y*y);}
double length() {return sqrt(norm());}
bool equal(Vector t) {return (abs(x-t.x)<EPS && abs(y-t.y)<EPS);}
};
struct Segment
{
Point p1,p2;
void read() {p1.read();p2.read();}
void print() {p1.print();p2.print();}
};
struct Line
{
Point p1,p2;
void read() {p1.read();p2.read();}
void print() {p1.print();p2.print();}
};
struct Circle
{
Point c;
double r;
Circle() {r=0;}
void read() {c.read();cin >> r;}
void print() {c.print();cout << r << endl;}
};
struct Polygon
{
int sides;
Point pts[105];
Polygon() {sides=0;}
void read(){cin >> sides;for(int i=1;i<=sides;i++) pts[i].read();}
void print(){cout << sides << endl;for(int i=1;i<=sides;i++) pts[i].print();}
};
bool equals(double x,double y) {return (abs(x-y)<EPS);}
bool operator==(Point a,Point b) {return a.equal(b);}
Vector operator+(Vector a,Vector b) {return a.add(b);}
Vector operator-(Vector a,Vector b) {return a.sub(b);}
Vector operator*(Vector a,double k) {return a.mult(k);}
bool operator==(Vector a,Vector b) {return a.equal(b);}
Point makePoint(double x,double y)
{
Point p;
p.x=x;p.y=y;
return p;
}
Vector makeVector(double x,double y)
{
Vector p;
p.x=x;p.y=y;
return p;
}
double dot(Vector a,Vector b)
{
return a.x*b.x+a.y*b.y;
}
double cross(Vector a,Vector b)
{
return a.x*b.y-a.y*b.x;
}
int main()
{
return 0;
}
Update
2023-4-10 开坑,写完“向量的内积”。
2023-4-11 完成本文。
后续
如果你觉得本文过于简单,可以来看续集。