zoukankan      html  css  js  c++  java
  • 计算几何经典操作

    不完整代码O(∩_∩)O~,提供几种常用操作的模板

    这里写代码片
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    
    using namespace std;
    
    const double eps=1e-8;
    const int N=10000;
    struct node{  //为了方便,通常将向量设成结构体
        double x,y;
        node (double xx=0,double yy=0)  //新向量的建立 
        {
            x=xx; y=yy;
        }
    }; 
    node po[N];
    
    //重载运算符 
    node operator +(const node &a,const node &b)
    {
        return node(a.x+b.x,a.y+b.y);
    } 
    node operator -(const node &a,const node &b)
    {
        return node(a.x-b.x,a.y-b.y);
    }
    node operator *(const node &a,const double &b)  //向量与实数的运算
    {
        return node(a.x*b,a.y*b);   
    } 
    node operator /(const node &a,const double &b)
    {
        return node(a.x/b,a.y/b);
    }
    
    //比较实数的时候先做差再用dcmp比较
    int dcmp(double x)
    {
        if (fabs(x)<eps) return 0;
        else if (x>0) return 1;
        else return -1; 
    }
    
    double Dot(node a,node b)//点积
    {
        return a.x*b.x+a.y*b.y;
    } 
    
    double Cross(node a,node b)//叉积
    {
        return a.x*b.y-a.y*b.x;
    } 
    
    /*
    两向量的夹角为α 
    两向量垂直,点积为0 (cosα)
    两向量共线(平行),叉积为0 (sinα)
    如果a b夹角大于90度,点积为负,小于90度,点积为正
    如果a旋转到b是逆时针,则叉积为正,否则叉积为负
    */
    
    //向量v(x,y)逆时针旋转a弧度(到v'),则
    //x'=xcosa-ysina=x*(Dot(a,b))-y(Cross(a,b))
    //y'=xsina+ycosa=x*(Cross(a,b))-y(Dot(a,b))
    
    double SS(int n)  //多边形面积 
    {
        int i;
        double area=0;
        for (i=2;i<n;i++) //原理:把多边形拆成三角形,叉积有正负
            area+=fabs(Cross(po[i]-po[1],po[i+1]-po[1]));
        return area/2;
    }
    
    int main()
    {
        scanf("%d",&n);
        for (i=1;i<=n;i++)
           scanf("%lf%lf",&po[i].x,&po[i].y);
        return 0;
    }
  • 相关阅读:
    leecode4:寻找两个正序数组的中位数
    leecode3:无重复字符的最长子串
    leecode2:两数相加
    KMP字符串模式匹配
    01迷宫问题
    汉诺塔问题
    微服务-基于Grpc的进程通信-Grpc异常捕获RpcException(4-4)
    React-Antd Pro增删改查
    HTTP 请求
    创业路上-1
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673623.html
Copyright © 2011-2022 走看看