How to draw spline curve?

avnersh

Member
Licensed User
Longtime User
Hello all
Is anybody can help with drawing spline curve?
In the canvas library their an option to draw polygon
but not smooth curve through several points .
Thanks,
Avner
 

thedesolatesoul

Expert
Licensed User
Longtime User
Here:

B4X:
Type PointType ( _
      x As Int, _
      y As Int)

Sub SetPoint(P As PointType,x1 As Int,y1 As Int)
   P.x = x1
   P.y = y1
End Sub

Sub CurveThePath2
'THE ORIGINAL FUNCTION
'Given 4 points on the spline P0 , P1 , P2 AND P3 , q(t) gives you a point on
'the Bit of the curve between P1 AND P2. q(0) Is always equal To P1 AND q(1)
'Is always equal To P2. It Is Not necessarily True that q(0.5) Is halfway 
'along the length of the curve, by distance.
'q(t) = 0.5 *(
'            (2 * P1) +
'              (-P0 + P2) * t +
'            (2*P0 - 5*P1 + 4*P2 - P3) * t^2 +
'            (-P0 + 3*P1- 3*P2 + P3) * t^3   )
         
   Dim p0, p1, p2, p3 As PointType
   Dim ControlPointCount  As Int
   Dim pr As PointType 
   Dim t As Double 
   
   
   'Check there are enough points to draw a spline
   If PathPointIndex = 1 Then 
      PathPoints(3) = PathPoints(1)
      PathPoints(2) = PathPoints(1)
      PathPoints(1) = PathPoints(0)
      PathPoints(0) = PathPoints(0)
      PathPointIndex = 3
   Else If (PathPointIndex = 2) Then
      ' Generate two emtpy points (at start and end)
      PathPoints(4) = PathPoints(2)
      PathPoints(3) = PathPoints(2)
      PathPoints(2) = PathPoints(1)
      PathPoints(1) = PathPoints(0)
      PathPoints(0) = PathPoints(0)
      PathPointIndex = 4
      
   Else If (PathPointIndex = 3) Then
      ' Generate 1 dummy point at end
      PathPoints(5) = PathPoints(3)
      PathPoints(4) = PathPoints(3)
      PathPoints(3) = PathPoints(2)
      PathPoints(2) = PathPoints(1)
      PathPoints(1) = PathPoints(0)
      PathPoints(0) = PathPoints(0)
      PathPointIndex = 5
      
   Else
      For i = PathPointIndex+1 To 1 Step -1
         PathPoints(i) = PathPoints(i-1)
      Next
      PathPoints(0) = PathPoints(1)
      PathPointIndex = PathPointIndex + 1
   End If
   

      'Get the first three  TLinks in the list of points. This algorithm is going to work by 
      'working out the first three points, then getting the last point at the start of the
      'while loop. After the curve section has been drawn, every point is moved along one,
      'and the TLink is moved to the next one so we can see if it's null, and then get the next
      'p3 from it if not.

   
   ControlPointCount = 0

   Do While (ControlPointCount+3 < PathPointIndex+1)
      p0 = PathPoints(ControlPointCount + 0)
      p1 = PathPoints(ControlPointCount + 1)
      p2 = PathPoints(ControlPointCount + 2)
      p3 = PathPoints(ControlPointCount + 3)
      
      For t=0 To 1 Step .02 
         pr.x  = .5 * ( (2 * p1.x) + (p2.x - p0.x) * t + (2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x) * t * t + (3 * p1.x - p0.x - 3 * p2.x + p3.x) * t * t * t)
         pr.y  = .5 * ( (2 * p1.y) + (p2.y - p0.y) * t + (2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y) * t * t + (3 * p1.y - p0.y - 3 * p2.y + p3.y) * t * t * t)
         SetPoint(CurvedPath((ControlPointCount*50)+(t*50)),pr.x,pr.y)
      Next
      ControlPointCount = ControlPointCount + 1
   Loop
   CurvedPathPoints = (ControlPointCount-1)*50 + 50
   PathComplete=True
End Sub
 
Upvote 0

GabrielHerrmann

Member
Licensed User
Longtime User
variable definition

Hello together,

I'm a new developer who is working with Basic 4 Adroid.

I have a problem with my application - I create a signature with an Canvas but I don't get all coordination data from drawing line.

I found this entry but I want to know all variable definitions inside the code sippet.

Can everybody help me to defined the variables:
- PathPointIndex
- ControlPointCount
- CurvedPathPoints?

Many thanks and best regards
Gabriel
 
Upvote 0
Top