This program graphs the equation r = A + B * Cos(t). It starts by setting ScaleMode so the coordinates fit nicely on the PictureBox.
Next the program lets t (theta) run over the values needed for the curve in small increments. For this equation, 0 <= t <= 2 * PI. It uses the value of t to calculate r.
Next the program converts the polar coordinates (r, t) into cartesian coordinates (x, y) where:
x = r * Cos(t)
y = r * Sin(t)
The program connects the points to draw the curve.
|
Private Sub DrawCurve()
Const PI = 3.14159265
Dim A As Single
Dim B As Single
Dim x As Single
Dim y As Single
Dim r As Single
Dim t As Single
Dim dt As Single
picGraph.AutoRedraw = True
picGraph.Cls
A = CSng(txtA.Text)
B = CSng(txtB.Text)
' Set a convenient scale.
If A < B Then
picGraph.ScaleLeft = -1 - 0.25
picGraph.ScaleWidth = 1 + A + B + 0.5
Else
picGraph.ScaleLeft = B - A - 0.25
picGraph.ScaleWidth = 2 * A + 0.5
End If
picGraph.ScaleLeft = picGraph.ScaleLeft * 1.5
picGraph.ScaleWidth = picGraph.ScaleWidth * 1.5
picGraph.ScaleTop = picGraph.ScaleWidth / 2
picGraph.ScaleHeight = -picGraph.ScaleWidth
' Draw axes.
picGraph.Line (picGraph.ScaleLeft, _
0)-Step(picGraph.ScaleWidth, 0), vbBlue
x = Int(picGraph.ScaleLeft)
Do While x <= picGraph.ScaleLeft + picGraph.ScaleWidth
picGraph.Line (x, -0.2)-(x, 0.2), vbBlue
x = x + 1
Loop
picGraph.Line (0, picGraph.ScaleTop)-Step(0, _
picGraph.ScaleHeight), vbBlue
y = Int(picGraph.ScaleTop)
Do While y >= picGraph.ScaleTop + picGraph.ScaleHeight
picGraph.Line (-0.2, y)-(0.2, y), vbBlue
y = y - 1
Loop
picGraph.Line (0, -5)-(0, 5), vbBlue
For y = -5 To 5
picGraph.Line (-0.2, y)-(0.2, y), vbBlue
Next y
' Draw the parametric curve.
t = 0
dt = PI / 100
r = A + B * Cos(t)
picGraph.CurrentX = r * Cos(t)
picGraph.CurrentY = r * Sin(t)
Do While t <= 2 * PI
r = A + B * Cos(t)
x = r * Cos(t)
y = r * Sin(t)
picGraph.Line -(x, y)
t = t + dt
Loop
t = 0
r = A + B * Cos(t)
picGraph.CurrentX = r * Cos(t)
picGraph.CurrentY = r * Sin(t)
picGraph.Line -(x, y)
End Sub
|