貝塞爾二次擬合工具類

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));
		}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章