< Continued from page 1
The complete code for our program is shown below. To code it yourself, Add a Panel control named PaintTrig and three Button controls: SineButton, CosineButton, and TangentButton. Then copy and paste the code into your VB.NET code window.
Imports System.Drawing.Drawing2D
Imports System.Math
Public Class TrigGraphs
Dim TrigPoints(359) As PointF
Dim Counter As Integer
Dim TrigBoxInterval As Double
Dim TrigBoxMidPoint As Double
Dim P As Pen = New Pen(Color.DarkSalmon, 3)
Private Sub SineButton_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles SineButton.Click
For Counter = 0 To 359
TrigPoints(Counter) = _
New Point(Round( _
Counter * TrigBoxInterval), _
TrigBoxMidPoint - TrigBoxMidPoint _
* Sin(PI * Counter / 180))
Next
PaintTrig.Refresh()
End Sub
Private Sub CosineButton_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles CosineButton.Click
For Counter = 0 To 359
TrigPoints(Counter) = _
New Point(Round( _
Counter * TrigBoxInterval), _
TrigBoxMidPoint - TrigBoxMidPoint _
* Cos(PI * Counter / 180))
Next
PaintTrig.Refresh()
End Sub
Private Sub TangentButton_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles TangentButton.Click
Dim myTan As Double
For Counter = 0 To 359
Try
myTan = Tan(PI * Counter / 180) * 4
TrigPoints(Counter) = _
New Point(Round( _
Counter * TrigBoxInterval), _
TrigBoxMidPoint - myTan)
Catch
If Tan(PI * Counter / 180) * 4 > _
TrigBoxMidPoint Then
TrigPoints(Counter) = _
New Point( _
Round(Counter * TrigBoxInterval), _
PaintTrig.Height)
Else
TrigPoints(Counter) = _
New Point( _
Round(Counter * TrigBoxInterval), 0)
End If
End Try
Next
PaintTrig.Refresh()
End Sub
Private Sub TrigGraphs_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
TrigBoxInterval = PaintTrig.Width / 360
TrigBoxMidPoint = CInt(PaintTrig.Height / 2 - 1)
End Sub
Private Sub PaintTrig_Paint( _
ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PaintTrig.Paint
e.Graphics.DrawCurve(P, TrigPoints)
MyBase.OnPaint(e)
End Sub
End Class
The complete code for our program is shown below. To code it yourself, Add a Panel control named PaintTrig and three Button controls: SineButton, CosineButton, and TangentButton. Then copy and paste the code into your VB.NET code window.
Imports System.Drawing.Drawing2D
Imports System.Math
Public Class TrigGraphs
Dim TrigPoints(359) As PointF
Dim Counter As Integer
Dim TrigBoxInterval As Double
Dim TrigBoxMidPoint As Double
Dim P As Pen = New Pen(Color.DarkSalmon, 3)
Private Sub SineButton_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles SineButton.Click
For Counter = 0 To 359
TrigPoints(Counter) = _
New Point(Round( _
Counter * TrigBoxInterval), _
TrigBoxMidPoint - TrigBoxMidPoint _
* Sin(PI * Counter / 180))
Next
PaintTrig.Refresh()
End Sub
Private Sub CosineButton_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles CosineButton.Click
For Counter = 0 To 359
TrigPoints(Counter) = _
New Point(Round( _
Counter * TrigBoxInterval), _
TrigBoxMidPoint - TrigBoxMidPoint _
* Cos(PI * Counter / 180))
Next
PaintTrig.Refresh()
End Sub
Private Sub TangentButton_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles TangentButton.Click
Dim myTan As Double
For Counter = 0 To 359
Try
myTan = Tan(PI * Counter / 180) * 4
TrigPoints(Counter) = _
New Point(Round( _
Counter * TrigBoxInterval), _
TrigBoxMidPoint - myTan)
Catch
If Tan(PI * Counter / 180) * 4 > _
TrigBoxMidPoint Then
TrigPoints(Counter) = _
New Point( _
Round(Counter * TrigBoxInterval), _
PaintTrig.Height)
Else
TrigPoints(Counter) = _
New Point( _
Round(Counter * TrigBoxInterval), 0)
End If
End Try
Next
PaintTrig.Refresh()
End Sub
Private Sub TrigGraphs_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
TrigBoxInterval = PaintTrig.Width / 360
TrigBoxMidPoint = CInt(PaintTrig.Height / 2 - 1)
End Sub
Private Sub PaintTrig_Paint( _
ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PaintTrig.Paint
e.Graphics.DrawCurve(P, TrigPoints)
MyBase.OnPaint(e)
End Sub
End Class
SHARE