lcpsky

导航

贝塞尔二次拟合工具类

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.lcp.dxf.base.Vector3f;


/**
 * @description(for bezier 贝塞尔曲线拟合  二维) 工具类
 * @author lcpsky  
 *
 */
public class BezierUtil {
	   private List<Vector3f> points;
	    public List<Vector3f> getPoints() {
		return points;
	}
		private static Random generator = new Random();
	    
	   public BezierUtil(List<Vector3f> points)
	    {
	        this.points = points;
	    }
	   public void initPoints(int num)
	    {
	            points = new ArrayList<>();
	            for(int i = 0; i < points.size() ; i++)
	            {
	                    double x = generator.nextDouble()*1000;
	                    double y = generator.nextDouble()*1000;
	                    points.set(i,new Vector3f(x, y, 0));
	            }
	    }
	   public Vector3f cubicBezier(double t, List<Vector3f> points)
	    {
		        List<Vector3f> temp =points;
	            for(int i=0; i< points.size(); i++)
	            {
	                    for(int j = 0; j < points.size()-i-1 ; j++)
	                    {
	                            double x = (1-t)*temp.get(j).getX() + t*temp.get(j+1).getX();
	                            double y = (1-t)*temp.get(j).getY()+ t*temp.get(j+1).getY();
	                            temp.set(j,new Vector3f(x, y, 0));
	                    }
	            }
	            return temp.get(0);
	    }
	 
}
测试
//进行贝塞尔拟合,
		BezierUtil bezierUtil = new BezierUtil(pointsRight);
		for (double t = 0; t < 1.0; t+=0.002) {
			Vector3f n1 = bezierUtil.cubicBezier(t, bezierUtil.getPoints());     //开始坐标
			Vector3f n2 = bezierUtil.cubicBezier(t+0.001, bezierUtil.getPoints());//结束坐标
			lines.add(buildLine(n1,n2,90));
		}



posted on 2017-07-29 12:54  lcpsky  阅读(55)  评论(0)    收藏  举报