#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
setMinimumSize(820,350);
setMaximumSize(820,350);
ui->setupUi(this);
pix = QPixmap(720,350);
pix.fill(Qt::gray);
Step1 = new QLabel(this);
Step1->setText("<font color = gray>第一步:画点</font>");
Step1->setGeometry(QRect(725,-20,100,100));
Step2 = new QLabel(this);
Step2->setText("<font color = gray>第二步:</font>");
Step2->setGeometry(QRect(725,20,100,100));
Step3 = new QLabel(this);
Step3->setText("<font color = gray>第三步:</font>");
Step3->setGeometry(QRect(725,150,100,100));
btClose = new QPushButton(this);
btClose ->setText("关闭");
btClose->setGeometry(QRect(730,320,80,25));
connect(btClose,SIGNAL(clicked()),this,SLOT(close()));
btRStart = new QPushButton(this);
btRStart ->setText("再画");
btRStart->setGeometry(QRect(730,290,80,25));
connect(btRStart,SIGNAL(clicked()),this,SLOT(RStart()));
btdraw = new QPushButton(this);
btdraw ->setText("生成路径");
btdraw->setGeometry(QRect(730,85,80,25));
connect(btdraw,SIGNAL(clicked()),this,SLOT(test()));
btMove = new QPushButton(this);
btMove ->setText("小球运动");
btMove->setGeometry(QRect(730,215,80,25));
connect(btMove,SIGNAL(clicked()),this,SLOT(test2()));
label = new QLabel("平滑1-20",this);
label->setGeometry(QRect(722,115,65,25));
lineEdit = new QLineEdit(this);
lineEdit->setGeometry(QRect(785,115,30,25));
lineEdit->setText("20");
label2 = new QLabel("曲率0-1",this);
label2->setGeometry(QRect(722,145,65,25));
lineEdit2 = new QLineEdit(this);
lineEdit2->setGeometry(QRect(785,145,30,25));
lineEdit2->setText("1");
label3 = new QLabel("速度20-50",this);
label3->setGeometry(QRect(718,245,70,25));
lineEdit3 = new QLineEdit(this);
lineEdit3->setGeometry(QRect(788,245,30,25));
lineEdit3->setText("30");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter pp(&pix);
if(TVT==1)
{
pp.drawEllipse(Point,3,3);
}
QBrush brush(Qt::blue);
QBrush brush2(Qt::gray);
if(QAQ==1)
{
s1=Spline;
path.moveTo(s1->x,s1->y);
for(draw=0;draw<=g*(count-1);draw++)
{
path.lineTo(s1->x,s1->y);
path2.addPath(path);
s1++;
}
pp.drawPath(path);
QAQ = 0;
QwQ = 0;
}
if(TWT==1)
{
pix.fill(Qt::gray);
pp.drawPath(path2);
pp.setBrush(brush);
pp.drawEllipse(s2->x-5,s2->y-5,20,20);
s2++;
}
QPainter painter(this);
painter.drawPixmap(0,0,pix);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if((event->button()==Qt::LeftButton)&&QwQ==1)
{
TVT = 1;
Point = event->pos();
x1[dot]=event->x();
y1[dot]=event->y();
dot++;
count++;
update();
}
}
void MainWindow::test()
{
str = lineEdit->text();
g = str.toInt();
str2 = lineEdit2->text();
t = str2.toDouble();
if((g>=0)&&(g<=20)&&(t>=0)&&(t<=1))
{
CSpline(x1,y1,g,t,count);
QAQ = 1;
QwQ = 0;
TVT = 0;
update();
}
}
void MainWindow::test2()
{
str3 = lineEdit3->text();
v = str3.toInt();
if((v>=20)&&(v<=50))
{
CSpline(x1,y1,v,t,count);
s2=s3=Spline;
TUT = 1;
timer = new QTimer;
connect(timer,SIGNAL(timeout()),this,SLOT(moveDot()));
timer->start(100);
}
}
void MainWindow::moveDot()
{
if((s2->x!=x1[count-1])&&(s2->y!=x1[count]-1)&&(TUT==1))
{
TWT = 1;
}
else
{
TWT = 0;
}
update();
}
void MainWindow::RStart()
{
QAQ = 0;
TWT = 0;
QwQ = 1;
TVT = 0;
dot = 0;
count = 0;
TUT = 0;
memset(x1,0,sizeof(x1));
memset(y1,0,sizeof(y1));
memset(s1,0,sizeof(s1));
memset(s2,0,sizeof(s2));
update();
}
void MainWindow::CSpline(double x[], double y[], int grain, double tension, int n)
{
int i, np;
n0 = n;
np = n0;
CPt jd[100];
CPt *knots;
knots0 = knots;
for(i=1; i<=np; i++){
jd[i].x = x[i-1];
jd[i].y = y[i-1];
}
jd[0].x = x[0];
jd[0].y = y[0];
jd[np+1].x = x[np-1];
jd[np+1].y = y[np-1];
np=np+2;
knots=jd;
CubicSpline(np,knots,grain,tension);
}
void MainWindow::CubicSpline(int n, CPT *knots, int grain, double tension)
{
CPt *s;
CPt *k0,*kml,*k1,*k2;
int i,j;
double alpha[50];
GetCardinalMatrix(tension);
for(i=0;i<grain;i++)
alpha[i] = ((double)i)/grain;
s = Spline;
kml = knots;
k0 = kml+1;
k1 = k0+1;
k2 = k1+1;
for(i=1;i<n-1;i++){
for(j=0;j<grain;j++){
s->x = Matrix(kml->x,k0->x,k1->x,k2->x,alpha[j]);
s->y = Matrix(kml->y,k0->y,k1->y,k2->y,alpha[j]);
s++;
}
k0++;kml++;k1++;k2++;
}
}
void MainWindow::GetCardinalMatrix(double a1)
{
m[0]=-a1; m[1]=2.-a1; m[2]=a1-2.; m[3]=a1;
m[4]=2.*a1; m[5]=a1-3.; m[6]=3.-2*a1; m[7]=-a1;
m[8]=-a1; m[9]=0.; m[10]=a1; m[11]=0.;
m[12]=0.; m[13]=1.; m[14]=0.; m[15]=0.;
}
double MainWindow::Matrix(double a, double b, double c, double d, double alpha)
{
double p0,p1,p2,p3;
p0=m[0]*a+m[1]*b+m[2]*c+m[3]*d;
p1=m[4]*a+m[5]*b+m[6]*c+m[7]*d;
p2=m[8]*a+m[9]*b+m[10]*c+m[11]*d;
p3=m[12]*a+m[13]*b+m[14]*c+m[15]*d;
return(p3+alpha*(p2+alpha*(p1+alpha*p0)));
}
- 1
- 2
前往页