Home Search   What's New Index Books Links Q & A Newsletter Banners   Feedback Tip Jar MSDN Visual Basic Community

Title Draw a cycloid using VB .NET cycloid, curve Graphics

This cycloid is represented by the following parametric functions as t ranges from 0 to 14 * Pi.

' The parametric function X(t).
Private Function X(ByVal t As Single) As Single
X = (2000 * (27 * Cos(t) + 15 * Cos(t * 20 / 7)) / 42) _
/ 20
End Function

' The parametric function Y(t).
Private Function Y(ByVal t As Single) As Single
Y = (2000 * (27 * Sin(t) + 15 * Sin(t * 20 / 7)) / 42) _
/ 20
End Function

In the form's Paint event handler, the program calls the DrawCurve subroutine to connect points defined by these functions.

Note that DrawCurve places the points it will draw in an array and then uses the DrawLines method once. This is faster than calling DrawLine multiple times.

' Redraw the curve.
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
System.Windows.Forms.PaintEventArgs) Handles _
MyBase.Paint
DrawCurve(e.Graphics, _
Me.Width \ 2, Me.Height \ 2, _
0, 14 * PI, 0.1)
End Sub

' Draw the curve on the indicated Graphics object.
Private Sub DrawCurve(ByVal gr As Graphics, ByVal cx As _
Integer, ByVal cy As Integer, ByVal start_t As Single, _
ByVal stop_t As Single, ByVal dt As Single)
Dim num_pts As Integer
Dim pts() As PointF
Dim pt As Integer
Dim t As Single
Dim px As Single
Dim py As Single

' Allocate room for the points.
' Note that this allocates an array
' with indices from 0 to num_pts.
num_pts = (stop_t - start_t) / dt
ReDim pts(num_pts)

' Find the center of the drawing area.
px = cx + X(start_t)
py = cy + Y(start_t)

' Find the points.
t = start_t
For pt = 0 To num_pts - 1
pts(pt).X = cx + X(t)
pts(pt).Y = cy + Y(t)
t += dt
Next pt

' Close to the first point.
pts(num_pts) = pts(0)

' Draw the lines.
gr.DrawLines(New Pen(Color.Black), pts)
End Sub

Click here to compare this code to the version used for VB 6.

See my book Visual Basic Graphics Programming for more information on drawing cycloids and other curves normally or transformed (stretched, squashed, or rotated). This book uses VB 5/6 not VB .NET.