Click here to Skip to main content
15,884,388 members
Articles / Multimedia / GDI+

Interpolation of BezierSplines and Cubic Splines

Rate me:
Please Sign up or sign in to vote.
4.83/5 (16 votes)
5 Jan 2008CPOL2 min read 98.3K   3.1K   63  
Algorithms to compute the Y - Value at an X - Position of a curve, constructed by some support-points
''' <summary>
''' Polygon does the most primitive form of interpolation: draw a straight line
''' Its derivates BezierSpline and CubicSpline perform higher level of drawing and 
''' interpolating (if more than 2 support-points available)
''' </summary>
Public Class Polygon : Inherits DrawObjectBase

   Protected _Points(-1) As PointF

   Public Sub UpdatePath(ByVal Points() As PointF)
      _Points = Points
      DrawPath.Reset()
      If _Visible Then
         Select Case Points.Length
            Case Is < 2
            Case 2 '2 points only? - its a line, not a spline!
               'make shure not to call PreparePath-overrides
               MyClass.PreparePath()
            Case Else
               Me.PreparePath()
         End Select
      End If
      MyBase.Invalidate(True)
   End Sub

   Protected Overrides Sub PreparePath()
      With DrawPath
         .AddLines(_Points)
         'add some decoration
         Const R As Single = 2, _2R As Single = 2 * R
         For Each Pt In _Points
            .AddEllipse(Pt.X - R, Pt.Y - R, _2R, _2R)
         Next
      End With
   End Sub

   Public Function GetPointOnCurve(ByVal X As Single) As PointF?
      ' carefully read manuals about BinarySearch() - note: I Xor it immediately
      Dim Indx = _Points.BinarySearch(New PointF(X, 0), Function(P1, P2) P1.X.CompareTo(P2.X)) Xor -1
      Select Case Indx
         Case Is < 0 'exact match (nearly never occurs)
            Return _Points(Indx Xor -1) 'return match uninterpolated (re-Xor Indx)

         Case 0, _Points.Count 'X is out of range
            Return Nothing

         Case Else
            If _Points.Length = 2 Then ' its a line, not a spline!
               'make shure not to call InterpolateSegment()-overrides
               Return MyClass.InterpolateSegment(X, Indx)
            End If
            Return InterpolateSegment(X, Indx) 'execute overridden interpolating, if available

      End Select
   End Function

   Protected Overridable Function InterpolateSegment(ByVal X As Single, ByVal Indx As Integer) As PointF
      'linear interpolation
      'note: Indx references the point *after* X
      Dim Pt = _Points(Indx - 1)
      With _Points(Indx).Subtract(Pt)
         Return New PointF(X, Pt.Y + (X - Pt.X) * .Y / .X)
      End With
   End Function

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions