2013-03-11 190 views
2

我想創建更多平滑曲線,而不僅是線條角度。這是圖片我此刻是如何得出:繪製平滑曲線

enter image description here

這裏是我的代碼:

case FREEHAND: 
    float[] pts; 
    float[] ptk; 
    ptk = new float[2]; 
    imageMatrix.invert(inv);    
    if (mCurrentShape == null) {     
     mCurrentShape = new Line(); 
     pts = new float[2]; 
     ((Line) mCurrentShape).setBegin(mDownPoint); 
     pts[0] = (float)((Line) mCurrentShape).getBegin().getX(); 
     pts[1] = (float)((Line) mCurrentShape).getBegin().getY(); 
     inv.mapPoints(pts); 
     ((Line) mCurrentShape).getPath().moveTo(pts[0], pts[1]); 
    } 
    ptk[0] = (float)currentPoint.getX(); 
    ptk[1] = (float)currentPoint.getY(); 
    inv.mapPoints(ptk); 
    ((Line) mCurrentShape).getPath().lineTo(ptk[0], ptk[1]); 
    break; 

寫意代碼:

package aa.bb.cc; 
import java.util.ArrayList; 
import android.graphics.Path; 

public class FreeHand extends Shape{ 
    private ArrayList<Path>_graphics; 

    public FreeHand(){ 
     super(); 
     _graphics = new ArrayList<Path>(); 
    } 

    public ArrayList<Path> getGraphicsPath(){ 
     return _graphics; 
    } 
} 
+1

看看這個:http://corner.squareup.com/2012/07/smoother-signatures.html它不是一個完整的解決方案,但它給你一個你可以做什麼的想法。 – 2013-03-11 12:00:00

+0

這篇關於貝塞爾曲線的文章可能很有用: http://devmag.org.za/2011/04/05/bzier-curves-a-tutorial/ – stan0 2013-03-11 13:47:13

回答

-1

我有一個代碼,使用二次貝塞爾線平滑點。這真的很好。它也非常有效。使用以下類構建平滑路徑。每次有新點時,請使用addPoint(x, y)添加它。完成後,您可以撥打constructPath()以獲取平滑路徑。與評論中的解決方案比較,比如「corner.squareup.com/2012/07/smoother-signatures.html」,我的解決方案避免了原始路徑非常尖銳的角落處的人爲現象。

public class BezierCurveConstructor { 
Path path; 

PointF previousPoint; 
int pointCounter = 0; 

public BezierCurveConstructor() { 
    reset(); 
} 


/** 
* reset the path 
*/ 
public void reset() { 
    path = new Path(); 
    pointCounter = 0; 
} 


public void addPoint(float x, float y) { 

    pointCounter ++; 
    if (pointCounter == 1) { 
     path.moveTo(x, y); 
     previousPoint = new PointF(x, y); 
     return; 
    } 


    PointF mid = new PointF((x + previousPoint.x)/2.0f, (y + previousPoint.y)/2.0f); 

    if (pointCounter < 3) { 
     path.lineTo(mid.x, mid.y); 
    } else { 
     path.quadTo(previousPoint.x, previousPoint.y, mid.x, mid.y); 
    } 
    previousPoint = new PointF(x, y); 
} 

/** 
* construct path by points 
* 
* @return 
*/ 
public Path constructPath() { 
    return path; 
} 

}

此路徑包含二次貝塞爾曲線經過前點的所有中間點和你接觸的下一個點去的名單。請注意,此路徑具有不變的二階導數,但一階導數在膠粘點處匹配。我們可以證明爲什麼如此通過下面的推導:

Denote the list of points as p_0, p_1, ... p_n, (these can be points in R^n) let c_i = (pi+p_{i_1})/2 as the middle point of pi and p_{i-1}. We can model the quadratic Bezier line as following, here t is a number between 0 and 1: G_i(t)=(1-t)^2c_i +2(1-t)tp_i + t^2 c_{i+1} Consider the derivative against t: G_i'(t) = -2(1-t)c_i + 2(1-t-t)p_i + 2t c_{i+1}, and G_i''(t) = 2c_i -4p_i + 2c_{i+1} = 2p_{i-1} + 2 p_{i+1} Thus the second derivatives are constants. To show that the first derivatives match at each c_i, we can plug in t=0, and t=1. G_i'(0) = 2p_i-2c_i = p_i - p_{i-1} G_{i-1}'(1) = -2p_{i-1}+2c_i = p_i - p_{i-1} = G_i'(0)

順便說一句,你可以看看維基貝塞爾曲線的明確定義https://en.wikipedia.org/wiki/B%C3%A9zier_curve

結帳此鏈接的原始代碼: https://github.com/lyang36/codeNotes/blob/master/BezierCurveConstructor.java