点与点 点与线的关系

来源:互联网 发布:mac如何关闭所有程序 编辑:程序博客网 时间:2024/04/30 06:34
////  main.cpp//  Richard////  Created by 邵金杰 on 16/8/5.//  Copyright © 2016年 邵金杰. All rights reserved.////常用常数double PI=acos(-1);double INF=1e20;double EPS=1e-6;typedef pair<double,double> CPoint;//点typedef pair<CPoint,CPoint> CLine;//线struct CPoint{//点    double x,y;    CPoint(double x,double y): x(x),y(y) {}};struct CLine{//线    CPoint a,b;    CLine(CPoint a,CPoint b): a(a),b(b) {}};struct CVector{    double x,y;    CVector(double x,double y): x(x),y(y) {}};CVector operator * (double k,CVector p){//常数乘矢量    return CVector(k*p.x,k*p.y);}double operator * (CVector p,CVector q){//矢量点乘    return p.x*q.x+p.y*q.y;}double length(CVector p){//求矢量的模长    return sqrt(p*p);}double operator ^ (CVector p,CVector q){//矢量叉积    return p.x*q.y-p.y*q.x;}CVector unit(CVector p){//单位向量    return 1/length(p)*p;}double dot(CVector p,CVector q){//矢量点乘    return p.x*q.x+p.y*q.y;}double project(CVector p,CVector q){//投影    return dot(p,unit(q));}bool IsZero(double x){//判断一个数是否为0(用于浮点数)    return x<EPS&&x>-EPS;}bool FLarger(double x,double y){//判断x是否大于y    return x-y>EPS;}bool FLess(double x,double y){//判断x是否小于y    return y-x>EPS;}CVector operator - (CPoint b,CPoint a){//点a到点b的向量AB,用B-A表示    return CVector(b.x-a.x,b.y-a.y);}CPoint operator + (CPoint a,CVector p){//将点a沿矢量p方向平移矢量p的长度得到点b    return CPoint(a.x+p.x,a.y+p.y);}double dist(CPoint p,CPoint q){//求两点间线段的长度    return length(p-q);}double vertical_dist(CPoint p,CLine l){//求直线外一点p到直线l的距离    return fabs((p-l.a)^(l.b-l.a))/length(l.b-l.a);}CPoint rotate(CPoint b,CPoint a,double alpha){//把点b绕点a旋转alpha度到点c,并返回c    CVector p=b-a;    return CPoint(a.x+(p.x*cos(alpha)-p.y*sin(alpha)),a.y+(p.x*sin(alpha)+p.y*cos(alpha)));}int SideOfLine(CPoint a,CPoint b,CPoint p){//判断p在直线a,b的哪一侧    double result=(b-a)^(p-a);    if(IsZero(result)) return 0;//p在直线ab上    else if(result>0) return 1;//p在直线ab左侧    else return -1;//p在直线ab右侧}CLine Vertical(CPoint p,CLine l){//过点p作直线l的垂线    return CLine(p,p+(rotate(l.b,l.a,PI/2)-l.a));}CPoint foot(CPoint p,CLine l){//求垂足    return CPoint(l.a+project(p-l.a,l.b-l.a)*unit(l.b-l.a));}

0 0