home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH6 / SRC / BSPLINE2.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-04-01  |  10.6 KB  |  361 lines

  1. VERSION 4.00
  2. Begin VB.Form BsplineForm 
  3.    Caption         =   "B-spline"
  4.    ClientHeight    =   5430
  5.    ClientLeft      =   2175
  6.    ClientTop       =   930
  7.    ClientWidth     =   4830
  8.    Height          =   6120
  9.    Left            =   2115
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   362
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   322
  14.    Top             =   300
  15.    Width           =   4950
  16.    Begin VB.CheckBox ShowTCheck 
  17.       Caption         =   "Show t Values"
  18.       Height          =   255
  19.       Left            =   1680
  20.       TabIndex        =   8
  21.       Top             =   300
  22.       Width           =   1755
  23.    End
  24.    Begin VB.TextBox KText 
  25.       Height          =   285
  26.       Left            =   1140
  27.       TabIndex        =   6
  28.       Text            =   "3"
  29.       Top             =   45
  30.       Width           =   375
  31.    End
  32.    Begin VB.CommandButton CmdNew 
  33.       Caption         =   "New"
  34.       Enabled         =   0   'False
  35.       Height          =   375
  36.       Left            =   4320
  37.       TabIndex        =   5
  38.       Top             =   0
  39.       Width           =   495
  40.    End
  41.    Begin VB.CommandButton CmdGo 
  42.       Caption         =   "Go"
  43.       Default         =   -1  'True
  44.       Enabled         =   0   'False
  45.       Height          =   375
  46.       Left            =   3600
  47.       TabIndex        =   4
  48.       Top             =   0
  49.       Width           =   495
  50.    End
  51.    Begin VB.CheckBox ControlCheck 
  52.       Caption         =   "Show Control Points"
  53.       Height          =   255
  54.       Left            =   1680
  55.       TabIndex        =   3
  56.       Top             =   0
  57.       Value           =   1  'Checked
  58.       Width           =   1755
  59.    End
  60.    Begin VB.TextBox DtText 
  61.       Height          =   285
  62.       Left            =   240
  63.       TabIndex        =   2
  64.       Text            =   "0.05"
  65.       Top             =   45
  66.       Width           =   615
  67.    End
  68.    Begin VB.PictureBox Canvas 
  69.       AutoRedraw      =   -1  'True
  70.       Height          =   4815
  71.       Left            =   0
  72.       ScaleHeight     =   317
  73.       ScaleMode       =   3  'Pixel
  74.       ScaleWidth      =   317
  75.       TabIndex        =   0
  76.       Top             =   600
  77.       Width           =   4815
  78.    End
  79.    Begin VB.Label Label1 
  80.       Caption         =   "K"
  81.       Height          =   255
  82.       Index           =   0
  83.       Left            =   960
  84.       TabIndex        =   7
  85.       Top             =   60
  86.       Width           =   255
  87.    End
  88.    Begin VB.Label Label1 
  89.       Caption         =   "dt"
  90.       Height          =   255
  91.       Index           =   1
  92.       Left            =   0
  93.       TabIndex        =   1
  94.       Top             =   60
  95.       Width           =   255
  96.    End
  97.    Begin VB.Menu mnuFile 
  98.       Caption         =   "&File"
  99.       Begin VB.Menu mnuFileExit 
  100.          Caption         =   "E&xit"
  101.       End
  102.    End
  103. Attribute VB_Name = "BsplineForm"
  104. Attribute VB_Creatable = False
  105. Attribute VB_Exposed = False
  106. Option Explicit
  107. Const PI = 3.14159
  108. Const GAP = 3
  109. ' The endpoints are points 1 and 4. The control
  110. ' points are points 2 and 3.
  111. Dim MaxPt As Integer
  112. Dim PtX() As Single
  113. Dim PtY() As Single
  114. Dim MakingNew As Boolean
  115. ' The index of the point being dragged.
  116. Dim Dragging As Integer
  117. Dim OldMode As Integer
  118. ' Kvalue determines the smoothness of the curve.
  119. Dim Kvalue As Integer
  120. ' t runs between 0 and MaxPt - Kvalue + 2.
  121. Dim MaxT As Single
  122. ' ************************************************
  123. ' Recursively compute the blending function.
  124. ' ************************************************
  125. Function Blend(i As Integer, k As Integer, t As Single) As Single
  126. Dim numer As Single
  127. Dim denom As Single
  128. Dim value1 As Single
  129. Dim value2 As Single
  130. Dim newt As Single
  131.     If i > 0 Then
  132.         newt = t - i + MaxPt + 1
  133.         Do While newt >= MaxPt + 1
  134.             newt = newt - (MaxPt + 1)
  135.         Loop
  136.         Do While newt < 0
  137.             newt = newt + (MaxPt + 1)
  138.         Loop
  139.         Blend = Blend(0, k, newt)
  140.         Exit Function
  141.     End If
  142.     ' Base case for the recursion.
  143.     If k = 1 Then
  144.         If Knot(i) <= t And t < Knot(i + 1) Then
  145.             Blend = 1
  146.         ElseIf t = MaxT And Knot(i) <= t And t <= Knot(i + 1) Then
  147.             Blend = 1
  148.         Else
  149.             Blend = 0
  150.         End If
  151.         Exit Function
  152.     End If
  153.     denom = Knot(i + k - 1) - Knot(i)
  154.     If denom = 0 Then
  155.         value1 = 0
  156.     Else
  157.         numer = (t - Knot(i)) * Blend(i, k - 1, t)
  158.         value1 = numer / denom
  159.     End If
  160.     denom = Knot(i + k) - Knot(i + 1)
  161.     If denom = 0 Then
  162.         value2 = 0
  163.     Else
  164.         numer = (Knot(i + k) - t) * Blend(i + 1, k - 1, t)
  165.         value2 = numer / denom
  166.     End If
  167.     Blend = value1 + value2
  168. End Function
  169. ' ************************************************
  170. ' Draw the curve on the indicated picture box.
  171. ' ************************************************
  172. Sub DrawCurve(pic As PictureBox, start_t As Single, stop_t As Single, dt As Single)
  173. Dim x1 As Single
  174. Dim y1 As Single
  175. Dim t As Single
  176.     x1 = X(start_t)
  177.     y1 = Y(start_t)
  178.     pic.Cls
  179.     pic.CurrentX = x1
  180.     pic.CurrentY = y1
  181.     t = start_t + dt
  182.     Do While t < stop_t
  183.         x1 = X(t)
  184.         y1 = Y(t)
  185.         pic.Line -(x1, y1)
  186.         t = t + dt
  187.     Loop
  188.     x1 = X(stop_t)
  189.     y1 = Y(stop_t)
  190.     pic.Line -(x1, y1)
  191. End Sub
  192. ' ************************************************
  193. ' Return the ith knot value.
  194. ' ************************************************
  195. Function Knot(i As Integer) As Integer
  196.     Knot = i
  197. End Function
  198. ' ************************************************
  199. ' The parametric function Y(t).
  200. ' ************************************************
  201. Function Y(t As Single) As Single
  202. Dim i As Integer
  203. Dim value As Single
  204.     For i = 0 To MaxPt
  205.         value = value + PtY(i) * Blend(i, Kvalue, t)
  206.     Next i
  207.     Y = value
  208. End Function
  209. ' ************************************************
  210. ' The parametric function X(t).
  211. ' ************************************************
  212. Function X(t As Single) As Single
  213. Dim i As Integer
  214. Dim value As Single
  215.     For i = 0 To MaxPt
  216.         value = value + PtX(i) * Blend(i, Kvalue, t)
  217.     Next i
  218.     X = value
  219. End Function
  220. ' ************************************************
  221. ' Use DrawCurve to draw the Bezier curve.
  222. ' ************************************************
  223. Private Sub DrawBspline()
  224. Const DOTTED = 2
  225. Dim dt As Single
  226. Dim i As Integer
  227. Dim oldstyle As Integer
  228.     If MaxPt < 0 Then Exit Sub
  229.     MousePointer = vbHourglass
  230.     Kvalue = CInt(KText.Text)
  231.     dt = CSng(DtText.Text)
  232.     MaxT = MaxPt + 1
  233.     DrawCurve Canvas, 0, MaxT, dt
  234.     If ControlCheck.value = vbChecked Then
  235.         ' Draw the control points.
  236.         For i = 0 To MaxPt
  237.             Canvas.Line _
  238.                 (PtX(i) - GAP, PtY(i) - GAP)- _
  239.                 Step(2 * GAP, 2 * GAP), , BF
  240.         Next i
  241.         
  242.         ' Connect the control points.
  243.         oldstyle = Canvas.DrawStyle
  244.         Canvas.DrawStyle = DOTTED
  245.         Canvas.CurrentX = PtX(MaxPt)
  246.         Canvas.CurrentY = PtY(MaxPt)
  247.         For i = 0 To MaxPt
  248.             Canvas.Line -(PtX(i), PtY(i))
  249.         Next i
  250.         Canvas.DrawStyle = oldstyle
  251.     End If
  252.     ' Mark the t values if desired.
  253.     If ShowTCheck.value = vbChecked Then
  254.         For dt = 0 To MaxT Step 1#
  255.             Canvas.Line (X(dt), Y(dt) - 5)-Step(0, 10)
  256.             Canvas.Line (X(dt) - 5, Y(dt))-Step(10, 0)
  257.         Next dt
  258.     End If
  259.     MousePointer = vbDefault
  260. End Sub
  261. ' ************************************************
  262. ' Either collect a new point or select a point and
  263. ' start dragging it.
  264. ' ************************************************
  265. Private Sub Canvas_MouseDown(button As Integer, Shift As Integer, X As Single, Y As Single)
  266. Dim i As Integer
  267.     ' If we are selecting points, do so now.
  268.     If MakingNew Then
  269.         MaxPt = MaxPt + 1
  270.         ReDim Preserve PtX(0 To MaxPt)
  271.         ReDim Preserve PtY(0 To MaxPt)
  272.         PtX(MaxPt) = X
  273.         PtY(MaxPt) = Y
  274.         Canvas.Line _
  275.             (X - GAP, Y - GAP)- _
  276.             Step(2 * GAP, 2 * GAP), , BF
  277.         
  278.         If MaxPt >= 3 Then CmdGo.Enabled = True
  279.         
  280.         Exit Sub
  281.     End If
  282.     ' Otherwise start dragging a point.
  283.     ' Find a close point.
  284.     For i = 0 To MaxPt
  285.         If Abs(PtX(i) - X) <= GAP And _
  286.            Abs(PtY(i) - Y) <= GAP Then Exit For
  287.     Next i
  288.     If i > MaxPt Then Exit Sub
  289.     Dragging = i
  290.     OldMode = Canvas.DrawMode
  291.     Canvas.DrawMode = vbInvert
  292.     PtX(Dragging) = X
  293.     PtY(Dragging) = Y
  294.     Canvas.Line _
  295.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  296.         Step(2 * GAP, 2 * GAP), , BF
  297. End Sub
  298. ' ************************************************
  299. ' Continue dragging a point.
  300. ' ************************************************
  301. Private Sub Canvas_MouseMove(button As Integer, Shift As Integer, X As Single, Y As Single)
  302.     If Dragging < 0 Then Exit Sub
  303.     Canvas.Line _
  304.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  305.         Step(2 * GAP, 2 * GAP), , BF
  306.     PtX(Dragging) = X
  307.     PtY(Dragging) = Y
  308.     Canvas.Line _
  309.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  310.         Step(2 * GAP, 2 * GAP), , BF
  311. End Sub
  312. ' ************************************************
  313. ' Finish the drag and redraw the curve.
  314. ' ************************************************
  315. Private Sub Canvas_MouseUp(button As Integer, Shift As Integer, X As Single, Y As Single)
  316.     If Dragging < 0 Then Exit Sub
  317.     Canvas.DrawMode = OldMode
  318.     PtX(Dragging) = X
  319.     PtY(Dragging) = Y
  320.     Dragging = -1
  321.     DrawBspline
  322. End Sub
  323. Private Sub CmdGo_Click()
  324.     MakingNew = False
  325.     CmdNew.Enabled = True
  326.     DrawBspline
  327. End Sub
  328. ' ************************************************
  329. ' Prepare to get new points.
  330. ' ************************************************
  331. Private Sub CmdNew_Click()
  332.     MaxPt = -1
  333.     CmdGo.Enabled = False
  334.     CmdNew.Enabled = False
  335.     MakingNew = True
  336.     Canvas.Cls
  337. End Sub
  338. Private Sub ControlCheck_Click()
  339.     DrawBspline
  340. End Sub
  341. Private Sub Form_Load()
  342.     MakingNew = True
  343.     MaxPt = -1
  344.     Dragging = -1
  345. End Sub
  346. ' ************************************************
  347. ' Make the canvas as big as possible.
  348. ' ************************************************
  349. Private Sub Form_Resize()
  350.     Canvas.Move 0, Canvas.Top, _
  351.         ScaleWidth, ScaleHeight - Canvas.Top
  352.         
  353.     DrawBspline
  354. End Sub
  355. Private Sub mnuFileExit_Click()
  356.     Unload Me
  357. End Sub
  358. Private Sub ShowTCheck_Click()
  359.     DrawBspline
  360. End Sub
  361.