C++ 產生三次貝茲路徑

來源:互聯網
上載者:User

 

// 三次貝塞爾.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <stdio.h>#include <iostream>#include <vector>#define NUM_STEPS 30 //越大,曲線越密,越逼近using namespace std;class CvPoint{public:float x;float y;CvPoint(){x=0.0;y=0.0;}CvPoint(float a,float b){x=a;y=b;}}; void curve4(vector<CvPoint> &p,  double x1, double y1,   //Anchor1  double x2, double y2,   //Control1  double x3, double y3,   //Control2  double x4, double y4)   //Anchor2  {  CvPoint tmp0(x1,y1);p.push_back(tmp0); double dx1 = x2 - x1;  double dy1 = y2 - y1;  double dx2 = x3 - x2;  double dy2 = y3 - y2;  double dx3 = x4 - x3;  double dy3 = y4 - y3;  double subdiv_step  = 1.0 / (NUM_STEPS + 1);  double subdiv_step2 = subdiv_step*subdiv_step;  double subdiv_step3 = subdiv_step*subdiv_step*subdiv_step;  double pre1 = 3.0 * subdiv_step;  double pre2 = 3.0 * subdiv_step2;  double pre4 = 6.0 * subdiv_step2;  double pre5 = 6.0 * subdiv_step3;  double tmp1x = x1 - x2 * 2.0 + x3;  double tmp1y = y1 - y2 * 2.0 + y3;  double tmp2x = (x2 - x3)*3.0 - x1 + x4;  double tmp2y = (y2 - y3)*3.0 - y1 + y4;  double fx = x1;  double fy = y1;  double dfx = (x2 - x1)*pre1 + tmp1x*pre2 + tmp2x*subdiv_step3;  double dfy = (y2 - y1)*pre1 + tmp1y*pre2 + tmp2y*subdiv_step3;  double ddfx = tmp1x*pre4 + tmp2x*pre5;  double ddfy = tmp1y*pre4 + tmp2y*pre5;  double dddfx = tmp2x*pre5;  double dddfy = tmp2y*pre5;  int step = NUM_STEPS;  while(step--)  {  fx   += dfx;  fy   += dfy;  dfx  += ddfx;  dfy  += ddfy;  ddfx += dddfx;  ddfy += dddfy;  CvPoint tmp1(fx,fy);p.push_back(tmp1);  }  CvPoint tmp2(x4,y4);p.push_back(tmp2); }  int _tmain(int argc, _TCHAR* argv[]){CvPoint point[4];point[0].x=1.0;point[0].y=4.0;point[1].x=2.2;point[1].y=5.0;point[2].x=6;point[2].y=3;point[3].x=8;point[3].y=9;vector<CvPoint> curvePoint;curve4(curvePoint,point[0].x,point[0].y,point[1].x,point[1].y,point[2].x,point[2].y,point[3].x,point[3].y);int i=0;for(;i<curvePoint.size();i++){cout<<"("<<curvePoint[i].x<<","<<curvePoint[i].y<<")";if((i+1)%2==0)cout<<endl;}cout<<endl<<"點的個數:"<<i<<endl;system("pause");return 0;}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.