程序帝进,谁能帮我编个最简单的贝塞尔曲线,基于c++的

 我来答
杜爷1号
2015-07-18 · 知道合伙人软件行家
杜爷1号
知道合伙人软件行家
采纳数:4973 获赞数:9186
毕业于福建农林大学,本科学士学位。从事IT行业3年,曾参与过多个大型项目的需求调研、软件研发。

向TA提问 私信TA
展开全部
[cpp]
// 三次贝塞尔.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;
}
// 三次贝塞尔.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;
}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式