版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38013346/article/details/82218556
浮点精度
const double eps = 1e-8;
const double inf = 1e20;
const double pi = acos(-1.0);
const int maxp = 1010;
int sgn(double x) {
if(fabs(x) < eps) return 0;
if(x < 0) return -1;
return 1;
}
叉积
// 叉积
double operator ^ (const Point &b)const {
return x*b.y - y*b.x;
}
定义
向量叉积:
模长:
方向:
性质
点积
// 点积
double operator * (const Point &b)const {
return x*b.x + y*b.y;
}
定义
计算 pa 和 pb 的夹角
// 测试LightOJ1203
double rad(Point a,Point b) {
Point p = *this;
return fabs(atan2(fabs((a-p)^(b-p)),(a-p)*(b-p)));
}
化为长度为r的向量
Point trunc(double r) {
double l = len();
if(!sgn(l)) return *this;
r /= l;
return Point(x*r,y*r);
}
用途
已知直线向量。求出直线上任意一点
向量绕点P旋转angle
Point rotate(Point p,double angle) {
Point v = (*this) - p;
double c = cos(angle),s = sin(angle);
return Point(p.x + v.x*c - v.y*s,p.y+v.x*s + v.y*c);
}
Code
#include<bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double inf = 1e20;
const double pi = acos(-1.0);
const int maxp = 1010;
int sgn(double x) {
if(fabs(x) < eps) return 0;
if(x < 0) return -1;
return 1;
}
inline double sqr(double x) {return x*x;}
struct Point {
double x,y;
Point() {}
Point(double _x,double _y) {
x = _x;
y = _y;
}
void input() {
scanf("%lf%lf",&x,&y);
}
void output() {
printf("%.2f %.2f\n",x,y);
}
bool operator == (Point b)const {
return sgn(x-b.x) == 0 && sgn(y-b.y) == 0;
}
bool operator < (Point b)const {
return sgn(x-b.x) == 0 ? sgn(y-b.y)<0 : sgn(x-b.x)<0;
}
Point operator - (const Point &b)const {
return Point(x-b.x,y-b.y);
}
// 叉积
double operator ^ (const Point &b)const {
return x*b.y - y*b.x;
}
// 点积
double operator * (const Point &b)const {
return x*b.x + y*b.y;
}
// 返回长度
double len() {
return hypot(x,y); // 库函数
}
//
double len2() {
return x*x + y*y;
}
double distance(Point p) {
return hypot(x-p.x,y-p.y);
}
Point operator + (const Point &b)const {
return Point(x+b.x,y+b.y);
}
Point operator * (const double &k)const {
return Point(x*k,y*k);
}
Point operator / (const double &k)const {
return Point(x/k,y/k);
}
// 计算 pa 和 pb 的夹角
// 测试LightOJ1203
double rad(Point a,Point b) {
Point p = *this;
return fabs(atan2(fabs((a-p)^(b-p)),(a-p)*(b-p)));
}
// 化为长度为r的向量
Point trunc(double r) {
double l = len();
if(!sgn(l)) return *this;
r /= l;
return Point(x*r,y*r);
}
// 逆时针旋转90度(绕原点)
Point rotleft() {
return Point(-y,x);
}
// 顺时针旋转90度(绕原点)
Point rotright() {
return Point(y,-x);
}
// 绕着p点逆时针旋转angle
Point rotate(Point p,double angle) {
Point v = (*this) - p;
double c = cos(angle),s = sin(angle);
return Point(p.x + v.x*c - v.y*s,p.y+v.x*s + v.y*c);
}
};
int main()
{
}