• 三次贝塞尔曲线生成 C++ 代码


    
    void createCurve(point2D *originPoint,int originCount,std::vector<point2D> &curvePoint){
        //控制点收缩系数 ,经调试0.6较好,CvPoint是opencv的,可自行定义结构体(x,y)
        float scale = 0.6;
        point2D midpoints[originCount];
        //生成中点
        for(int i = 0 ;i < originCount ; i++){
            int nexti = (i + 1) % originCount;
            midpoints[i].x = (originPoint[i].x + originPoint[nexti].x)/2.0;
            midpoints[i].y = (originPoint[i].y + originPoint[nexti].y)/2.0;
        }
    
        //平移中点
        CvPoint extrapoints[2 * originCount];
        for(int i = 0 ;i < originCount ; i++){
             int nexti = (i + 1) % originCount;
             int backi = (i + originCount - 1) % originCount;
             CvPoint midinmid;
             midinmid.x = (midpoints[i].x + midpoints[backi].x)/2.0;
             midinmid.y = (midpoints[i].y + midpoints[backi].y)/2.0;
             int offsetx = originPoint[i].x - midinmid.x;
             int offsety = originPoint[i].y - midinmid.y;
             int extraindex = 2 * i;
             extrapoints[extraindex].x = midpoints[backi].x + offsetx;
             extrapoints[extraindex].y = midpoints[backi].y + offsety;
             //朝 originPoint[i]方向收缩
             int addx = (extrapoints[extraindex].x - originPoint[i].x) * scale;
             int addy = (extrapoints[extraindex].y - originPoint[i].y) * scale;
             extrapoints[extraindex].x = originPoint[i].x + addx;
             extrapoints[extraindex].y = originPoint[i].y + addy;
    
             int extranexti = (extraindex + 1)%(2 * originCount);
             extrapoints[extranexti].x = midpoints[i].x + offsetx;
             extrapoints[extranexti].y = midpoints[i].y + offsety;
             //朝 originPoint[i]方向收缩
             addx = (extrapoints[extranexti].x - originPoint[i].x) * scale;
             addy = (extrapoints[extranexti].y - originPoint[i].y) * scale;
             extrapoints[extranexti].x = originPoint[i].x + addx;
             extrapoints[extranexti].y = originPoint[i].y + addy;
    
        }
    
        CvPoint controlPoint[4];
        //生成4控制点,产生贝塞尔曲线
        for(int i = 0 ;i < originCount ; i++){
               controlPoint[0] = originPoint[i];
               int extraindex = 2 * i;
               controlPoint[1] = extrapoints[extraindex + 1];
               int extranexti = (extraindex + 2) % (2 * originCount);
               controlPoint[2] = extrapoints[extranexti];
               int nexti = (i + 1) % originCount;
               controlPoint[3] = originPoint[nexti];
               float u = 1;
               while(u >= 0){
                   int px = bezier3funcX(u,controlPoint);
                   int py = bezier3funcY(u,controlPoint);
                   //u的步长决定曲线的疏密
                   u -= 0.005;
                   CvPoint tempP = cvPoint(px,py);
                   //存入曲线点
                   curvePoint.push_back(tempP);
               }
        }
    }
    //三次贝塞尔曲线
    double CurvePlot::bezier3funcX(double _t,point2D *controlP){ // cal x coor
       double part0 = controlP[0].x * _t * _t * _t;
       double part1 = 3 * controlP[1].x * _t * _t * (1 - _t);
       double part2 = 3 * controlP[2].x * _t * (1 - _t) * (1 - _t);
       double part3 = controlP[3].x * (1 - _t) * (1 - _t) * (1 - _t);
       return part0 + part1 + part2 + part3;
    }
    double CurvePlot::bezier3funcY(double _t,point2D *controlP){    // cal y coor
       double part0 = controlP[0].y * _t * _t * _t;
       double part1 = 3 * controlP[1].y * _t * _t * (1 - _t);
       double part2 = 3 * controlP[2].y * _t * (1 - _t) * (1 - _t);
       double part3 = controlP[3].y * (1 - _t) * (1 - _t) * (1 - _t);
       return part0 + part1 + part2 + part3;
    }
    double CurvePlot::cal_angle(double _t, point2D *controlP){ // cal dy / dx angle
        double _dx_1= 3 * controlP[0].x * _t * _t;
        double _dx_2= 3 * controlP[1].x * (_t * 2 - 3 * _t * _t);
        double _dx_3= 3 * controlP[2].x * (1 - 4 * _t + 3 * _t * _t);
        double _dx_4= - 3 * controlP[3].x * (1 - _t) * (1 - _t);
        double _dy_1= 3 * controlP[0].y * _t * _t;
        double _dy_2= 3 * controlP[1].y * (_t * 2 - 3 * _t * _t);
        double _dy_3= 3 * controlP[2].y * (1 - 4 * _t + 3 * _t * _t);
        double _dy_4= - 3 * controlP[3].y * (1 - _t) * (1 - _t);
        return std::atan2(_dy_1 + _dy_2 + _dy_3 + _dy_4 , _dx_1 + _dx_2 + _dx_3 + _dx_4);
    }
    

    cal_angle 求分辨率为 _resolution 的时候每一点的切线方向。

  • 相关阅读:
    Android新手之旅(5) 网络资源
    Android新手之旅(10) 嵌套布局
    vertest
    Android新手之旅(4) 通过HTTP访问web
    Android新手之旅(6) 与.Net不同之处
    Android新手之旅(7) RadioButton的自定义
    Android新手之旅(9) 自定义的折线图
    Android新手之旅(8) ListView的使用
    ECLIPSE 安装及与CDT 的使用 多线程编程
    Eclipse的代码提示背景是黑色
  • 原文地址:https://www.cnblogs.com/walnuttree/p/10603331.html
Copyright © 2020-2023  润新知