You are on page 1of 2

Public Class Form1

Dim intXOrigin, intYOrigin As Integer


Dim sngN, sngC As Single
Dim blnPlot As Boolean = False

Private Sub PicDraw_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs)


Handles PicDraw.Paint

Dim gPaint As Graphics = e.Graphics 'creates the graphics object

Select Case True


Case blnPlot 'when btnPlot is clicked
Call DrawAxes(gPaint) 'draw axes
Dim sngStep As Single = 0.2 'drawing interval, not 1 since there will be gaps, you may
try changing this

Dim sngY As Single


If sngN Mod 2 = 1 Then 'For odd roots
For sngX As Single = 0 To (PicDraw.Width / 2) Step sngStep
sngY = (sngC * sngX) ^ (1 / sngN)
gPaint.DrawRectangle(Pens.LightBlue, intXOrigin + sngX, intYOrigin - sngY, 1, 1)
gPaint.DrawRectangle(Pens.LightBlue, intXOrigin - sngX, intYOrigin + sngY, 1, 1)
Next
Else 'for even roots
For sngX As Single = 0 To (PicDraw.Width / 2) Step sngStep
sngY = (sngC * sngX) ^ (1 / sngN)
gPaint.DrawRectangle(Pens.LightBlue, intXOrigin + sngX, intYOrigin - sngY, 1, 1)
Next
End If
Case Else 'when btnPlot is unclicked
gPaint.Clear(Color.Black) 'clearing of all graphics
Call DrawAxes(gPaint) 'drawing of axes
End Select
End Sub

Private Sub DrawAxes(ByVal g As Graphics)


'Note that intXOrigin and intYOrigin is declared as a field variable (class level)
intXOrigin = PicDraw.Width \ 2 'Calculates the x-origin, half of PicDraw Width
intYOrigin = PicDraw.Height \ 2 'Claculates the y-origin, half of PicDraw Height

Dim myfont As New Font("Arial", 8, FontStyle.Regular)

g.Clear(Color.Black)
g.DrawLine(Pens.Gray, 0, intYOrigin, PicDraw.Width, intYOrigin) 'draw x-axis
g.DrawLine(Pens.Gray, intXOrigin, 0, intXOrigin, PicDraw.Height) 'draw y-axis
g.DrawString("(0, 0)", myfont, Brushes.Green, intXOrigin, intYOrigin) 'add label to origin
If blnPlot = False Then
g.DrawString("This program plots the general function" _
& vbNewLine & "y = (Cx)^(1/N)" _
& vbNewLine & "Please input the appropirate values of C and N" _
& vbNewLine & "to the respective textboxes", myfont, Brushes.Green, 0, 0) 'add
instruction
Else
g.DrawString("graph of the function" _
& vbNewLine & "y = (" & sngC & "x)^(1/" & sngN & ")", _
myfont, Brushes.Green, 0, 0) 'adds function label to the graph
End If
End Sub
Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
PicDraw.Refresh() 'for resizing
End Sub

Private Sub btnPlot_Click(sender As System.Object, e As System.EventArgs) Handles


btnPlot.Click
Try
sngN = txtN.Text
sngC = txtConstant.Text
If sngC < 0 Then 'just a checker for C
MessageBox.Show("The constant, C, should be greater than or equal to zero.")
Else 'if everything is right
blnPlot = True
PicDraw.Refresh()
End If
Catch ex As Exception 'oops, something went wrong. ^_^
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles


btnReset.Click
'Just to reset everything
blnPlot = False
PicDraw.Refresh()
txtN.Clear()
txtConstant.Clear()
End Sub
End Class

You might also like