计算几何基础(c++语言) 角度转弧度 弧度转角度 角度制求三角函数

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

const double PI=acos(-1.0);
const double eps=1e-8;
double Deg2Rad(double deg){return (deg*PI/180.0);}//角度转弧度
double Rad2Deg(double rad){return (rad*180.0/PI);}//弧度转角度
double Sin(double deg){return sin(Deg2Rad(deg));}//角度制求三角函数
double Cos(double deg){return cos(Deg2Rad(deg));}
double ArcSin(double val){return Rad2Deg(asin(val));}
double ArcCos(double val){return Rad2Deg(acos(val));}
double Sqrt(double val){return sqrt(val);}
inline int sgn(double x){
    return (x>eps)-(x<-eps);
}
inline int dcmp(double x){
    if(fabs(x)<eps)return 0;
    else return x<0?-1: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 read(){
        scanf("%lf%lf",&x,&y);
    }//输入点
    double length()const{
        return sqrt(x*x+y*y);
    }//向量的长度
    Point operator+(const Point &rhs)const{
        return Point(x+rhs.x,y+rhs.y);
    }//求和
    Point operator-(const Point &rhs)const{
        return Point(x-rhs.x,y-rhs.y);
    }//求差
    Point operator*(double t)const{
        return Point(x*t,y*t);
    }//乘法
    Point operator/(double t)const{
        return Point(x/t,y/t);
    }//除法
    bool operator==(const Point &b)const{
        return dcmp(x-b.x)==0&&dcmp(y-b.y)==0;
    }
    Point unit()const{
        double l=length();
        return *this/l;
    }//使向量长度变为1
    double angle()const{
        return atan2(y,x);
    }//返回当前点的反正切值
    double get_tan()const{
        return atan2(y,x);
    }//返回当前向量的
};
double dis(const Point &a,const Point &b) {
    return (a-b).length();
}//求a到b之间的距离
Point Rotate(const Point &p,double angle,const Point &o=Point(0,0)){
    Point t=p-o;
    double x=t.x*cos(angle)-t.y*sin(angle);
    double y=t.y*cos(angle)+t.x*sin(angle);
    return Point(x, y) + o;
}//p绕着原点旋转angle度
double Cross(const Point &a,const Point &b){
    return a.x*b.y-a.y*b.x;
}//求两向量之间的叉积
double Cross(const Point &sp,const Point &ep,const Point&op){
    return Cross(sp-op,ep-op);
}//求解三点之间的叉积op是交点
double Dot(Point a,Point b){
    return a.x*b.x+a.y*b.y;
}//两个向量的点积
double Angle(Point a,Point b){
    return acos(Dot(a,b)/a.length()/b.length());
}//向量a和b组成的三角形的面积
Point GLI(Point p,Point v,Point q,Point w){//v,w是两直线的方向向量,p,q是两直上的点
    Point u=p-q;
    double t=Cross(w,u)/Cross(v,w);
    return p+v*t;
}//求解两条直线交点
double DTL(Point p,Point a,Point b){
    Point v1=b-a,v2=p-a;
    return fabs(Cross(v1,v2))/v1.length();
}//求点p到直线a,b的交点

0

Leave a Reply

Your email address will not be published.