Title | Graph a parametric function (X(t), Y(t)) in VB .NET |
Description | This example shows how to graph a parametric function (X(t), Y(t)) in VB .NET. It makes a variable t from 0 to 2 * Pi, connecting the points generated by the functions X(t) = r * Cos(t) and Y(t) = r * Sin(t), where r = 2 * Sin(5 * t). |
Keywords | graph, parametric, parametric equations, VB.NET |
Categories | VB.NET, Graphics |
|
Subroutine DrawCurve draws the parametric curve. It takes as a parameter the Graphics object on which to draw. This lets the program easily draw the curve in different locations and on different objects including the printer.
DrawCurve draws the X and Y axes with tick marks. It then make parametric variable t run from 0 to 2 * Pi, connecting the points generated by the functions X(t) = r * Cos(t) and Y(t) = r * Sin(t), where r = 2 * Sin(5 * t).
|
Private Sub DrawCurve(ByVal gr As Graphics)
Dim x As Double
Dim y As Double
Dim old_x As Double
Dim old_y As Double
Dim r As Double
Dim t As Double
Dim dt As Double
' Draw axes.
Dim axis_pen As New Pen(Color.Blue, 0)
gr.DrawLine(axis_pen, -2, 0, 2, 0)
For x = -2 To 2 Step 0.5
gr.DrawLine(axis_pen, CSng(x), -0.1, CSng(x), 0.1)
Next x
gr.DrawLine(axis_pen, 0, -2, 0, 2)
For y = -2 To 2 Step 0.5
gr.DrawLine(axis_pen, -0.1, CSng(y), 0.1, CSng(y))
Next y
' Draw the parametric curve.
t = 0
dt = PI / 100
old_x = 0
old_y = 0
Dim graph_pen As New Pen(Color.Black, 0)
Do While t <= 2 * PI
r = CSng(2 * Sin(5 * t))
x = r * Cos(t)
y = r * Sin(t)
gr.DrawLine(graph_pen, CSng(old_x), CSng(old_y), _
CSng(x), CSng(y))
old_x = x
old_y = y
t = t + dt
Loop
gr.DrawLine(graph_pen, CSng(old_x), CSng(old_y), 0, 0)
End Sub
|