Android Code Snippet Bicubic curves

B4X:
public Sub Bicubic(P As Path, X1 As Int, Y1 As Int, aX1 As Int, aY1 As Int, X2 As Int, Y2 As Int, aX2 As Int, aY2 As Int, Increment As Float)
    Dim Percent As Float, X3 As Int, Y3 As Int, X4 As Int, Y4 As Int
    If Increment <= 0 Then Increment = 0.01
    MakePoint(P, X1, Y1)
    Do Until Percent > 1
        X3 = CalculatePoint(X1, aX1, Percent)
            Y3 = CalculatePoint(Y1, aY1, Percent)
     
            X4 = CalculatePoint(aX2, X2, Percent)
            Y4 = CalculatePoint(aY2, Y2, Percent)
     
            X3 = CalculatePoint(X3, X4, Percent)
            Y3 = CalculatePoint(Y3, Y4, Percent)
            MakePoint(P, X3, Y3)
        Percent=Percent+Increment
    Loop
   MakePoint(P, X2, Y2)
End Sub
Public Sub CalculatePoint(PT1 As Int, PT2 As Int, Percent As Float) As Int
    Return (PT2 - PT1) * Percent + PT1
End Sub
Sub MakePoint(P As Path, X As Int, Y As Int)
    If P.IsInitialized Then
        P.LineTo(X,Y)
    Else
        P.Initialize(X,Y)
    End If
End Sub

Usage: P is a path object, does not need to be initialized. It will initialize it automatically, or just add points to an existing path. After it's done, you can use a canvas to draw the path however you like

X1/Y1 is the first point, aX1/aY1 is it's anchor point
X2/Y2 is the second point, aX2/aY2 is it's anchor point
The curves will start from X1/Y1 and go to X2/Y2, using the anchor points to manipulate the shape of the curve
This is the same method photoshop uses for it's vector curves, and uses less math than bezier curves

Increment is a fraction between 0 and 1 that defines how many points in the curve there will be (1/fraction=number of points)
 
Last edited:
Top