//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "BSplineEasy0.h"
#include <math.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
struct Vertex
{ int x, y;
void set(int nx, int ny)
{ x=nx; y=ny; }
void set(float nx, float ny)
{ x=nx; y=ny; }
};
bool getVertex = false;
bool pressing = false;
int upx, upy, dnx, dny;
class BSpline
{ protected:
int mPoints;
float t[4], n[4];
Vertex *mVertex;
public:
BSpline() { mVertex = NULL; }
void setLength(int len) { mPoints = len; }
void computeN();
void computeT(float param);
void assign(Vertex * av, int points) { mVertex=av; mPoints=points; }
void draw(TCanvas *canvas, int start=0);
void closeDraw(TCanvas *canvas);
};
void BSpline::closeDraw(TCanvas *canvas)
{ float s, x, y;
int k, p, c;
canvas->Pen->Mode = pmNot;
for (p=0; p<mPoints-3; p++)
{ x = (mVertex[p].x+4*mVertex[p+1].x+mVertex[p+2].x)/6.;
y = (mVertex[p].y+4*mVertex[p+1].y+mVertex[p+2].y)/6.;
canvas->MoveTo(x, y);
for (s=0.1; s<=1.05; s+=0.05)
{ computeT(s);
computeN();
x=y=0.0;
for (k=0; k<4; k++)
{ x += n[k]*mVertex[p+k].x;
y += n[k]*mVertex[p+k].y;
}
canvas->LineTo(x, y);
}
}
// for closing the path
for (c=0; c<3; c++)
{ x = (mVertex[(p)%mPoints].x+4*mVertex[(p+1)%mPoints].x+mVertex[(p+2)%mPoints].x)/6.;
y = (mVertex[(p)%mPoints].y+4*mVertex[(p+1)%mPoints].y+mVertex[(p+2)%mPoints].y)/6.;
canvas->MoveTo(x, y);
for (s=0.1; s<=1.05; s+=0.05)
{ computeT(s);
computeN();
x=y=0.0;
for (k=0; k<4; k++)
{ x += n[k]*mVertex[(p+k)%mPoints].x;
y += n[k]*mVertex[(p+k)%mPoints].y;
}
canvas->LineTo(x, y);
}
p++;
}
}
void BSpline::draw(TCanvas *canvas, int start)
{ float s, x, y;
int k;
canvas->Pen->Mode = pmNot;
x = (mVertex[start].x+4*mVertex[start+1].x+mVertex[start+2].x)/6.;
y = (mVertex[start].y+4*mVertex[start+1].y+mVertex[start+2].y)/6.;
canvas->MoveTo(x, y);
for (s=0.1; s<=1.0; s+=0.01)
{ computeT(s);
computeN();
x=y=0.0;
for (k=0; k<4; k++)
{ x += n[k]*mVertex[start+k].x;
y += n[k]*mVertex[start+k].y;
}
//Sleep(1);
canvas->LineTo(x, y);
}
}
void BSpline::computeT(float param)
{ t[0] = 1.0;
t[1] = param;
t[2] = param*param;
t[3] = t[2]*param;
}
void BSpline::computeN()
{ n[3] = t[3]/6.;
n[2] = t[3]*(-0.5) + (t[2]+t[1])*0.5 + 1./6.;
n[1] = t[3]*0.5 - t[2] + 4./6.;
n[0] = (-t[3]+3*(t[2]-t[1])+1)/6.;
}
const int Points = 10;
BSpline bs;
Vertex vt[Points];
int index=0;
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if ( !getVertex )
{ getVertex = true;
index = 0;
Button1->Caption = "Stop";
}
else
{ getVertex = false;
Button1->Caption = "Get control points";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
if ( getVertex )
{ if ( index<Points )
{ vt[index].x = X;
vt[index].y = Y;
Canvas->Brush->Style = bsSolid;
Canvas->Pen->Mode = pmNot;
Canvas->Ellipse(X-2, Y-2, X+2, Y+2);
index++;
if ( index >= Points )
{ getVertex = false;
Button1->Caption = "Get control points";
return;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{ static from=0;
bs.assign(vt, index);
bs.draw(Form1->Canvas, from);
from++;
if ( from > index-3-1 ) from = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
bs.assign(vt, index);
bs.closeDraw(Form1->Canvas);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
pressing = false;
}
//---------------------------------------------------------------------------
b-spline曲線範例程式
5星 · 超过95%的资源 需积分: 10 31 浏览量
2009-11-16
16:54:27
上传
评论
收藏 298KB RAR 举报
s2523154
- 粉丝: 0
- 资源: 1
最新资源
- OpenCv 使用fffffffff
- 正点原子开拓者FPGA多人表决器代码项目
- EOP-Last5Years.txt
- windows 32位、64位系统常见缺少的库
- 毕业设计基于springboot+vue实现的求职招聘类型网站源码+数据库(高分项目).zip
- 535springboot + vue 体质测试数据分析及可视化设计.zip(可运行源码+数据库文件+文档)
- python毕业设计-基于Django+OpenCV的二维码生成与识别系统源码.zip
- 基于springboot+vue实现的求职招聘类型网站源代码+数据库(优质毕设项目).zip
- iOS APP提审checklist
- 第十四届中北大学ACM程序设计竞赛.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈