卡斯特里

首页 » 常识 » 诊断 » 贝塞尔曲线二
TUhjnbcbe - 2021/8/25 2:11:00
北京哪里治疗白癜风专业 http://m.39.net/baidianfeng/a_4251012.html

这篇主要以代码为主,先看一下德卡斯特里奥算法公式。

这里有一个推导过程,上面的i就是几阶贝塞尔曲线,而j就是你想要的坐标点,根据我的理解,如果你想要最终的贝塞尔曲线的坐标,j都为0,不管你是多少阶,因为最终只有2条线段确定你的坐标点。

根据上图推导能得出p(i,j)=(1-t)*p(i-1,j)+t*p(i-1,j+1);下面是具体的代码实现。

/***describe:高阶贝塞尔曲线*date:/8/:27下午*author:Simon*/publicclassBezierViewextendsView{//贝塞尔曲线画笔privatePaintmPaint;//坐标点和直线画笔privatePaintmLinePointPaint;//贝塞尔曲线路径privatePathmPath;//坐标点privateListPointFmControlPoint;publicBezierView(Contextcontext){super(context);init();}/***初始化参数*/privatevoidinit(){mPaint=newPaint();mPaint.setColor(Color.RED);mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(4);mLinePointPaint=newPaint();mLinePointPaint.setColor(Color.GRAY);mLinePointPaint.setStyle(Paint.Style.STROKE);mLinePointPaint.setStrokeWidth(4);mPath=newPath();mControlPoint=newArrayList();initData();}/***随机生成8个坐标点*/privatevoidinitData(){mControlPoint.clear();Randomrandom=newRandom();for(inti=0;i8;i++){//+保证坐标不为0intx=random.nextInt()+;inty=random.nextInt()+;PointFpointF=newPointF(x,y);mControlPoint.add(pointF);}}

OverridepublicbooleanonTouchEvent(MotionEventevent){//当手指按下时重新初始化8个坐标,并重绘界面if(event.getAction()==MotionEvent.ACTION_DOWN){initData();invalidate();}returnsuper.onTouchEvent(event);}

OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);intsize=mControlPoint.size();PointFpointF;for(inti=0;isize;i++){pointF=mControlPoint.get(i);//绘制折线if(isize-1){mLinePointPaint.setColor(Color.GRAY);PointFpointFNext=mControlPoint.get(i+1);canvas.drawLine(pointF.x,pointF.y,pointFNext.x,pointFNext.y,mLinePointPaint);}//绘制起点、终点、途经点圆形if(i==0){mLinePointPaint.setColor(Color.RED);}elseif(i==size-1){mLinePointPaint.setColor(Color.BLUE);}else{mLinePointPaint.setColor(Color.GRAY);}canvas.drawCircle(pointF.x,pointF.y,10,mLinePointPaint);}//曲线连接buildBezierPoints();canvas.drawPath(mPath,mPaint);}/***贝塞尔曲线计算**

return返回坐标点*/privateArrayListPointFbuildBezierPoints(){mPath.reset();//曲线坐标点ArrayListPointFpoints=newArrayList();//阶数intorder=mControlPoint.size()-1;//把曲线分成分分数越多,越平滑floatdelta=1.0f/;for(floatt=0;t=1;t+=delta){//贝塞尔点集PointFpointF=newPointF(deCasteljau(order,0,t,true),deCasteljau(order,0,t,false));points.add(pointF);//如果是第一个点需要先移动到开始位置if(points.size()==1){mPath.moveTo(pointF.x,pointF.y);}else{//如果不是首点,就画直线mPath.lineTo(pointF.x,pointF.y);}}returnpoints;}/***德卡斯特里奥算法p(i,j)=(1-t)*p(i-1,j)+t*p(i-1,j+1);*原文地址*
1
查看完整版本: 贝塞尔曲线二