VB Tutor VB.NET 2022 Tutorial VB2019 Tutorial VB6 Tutorial VB Sample Code About Us
Visual Basic Sample Code

Cubic Function Graph Plotter

Visualize cubic functions with interactive demo and VB code examples


This program enables users to input the coefficients of a cubic function and visualize its graph. The cubic function takes the form: f(x) = ax3 + bx2 + cx + d.

In Visual Basic, we need to handle coordinate transformations since computer screen coordinates start from the top left. To center the graph, we use transformations like (5 - x) to start plotting from the center.

The graph is drawn using Visual Basic's built-in PSet method, which plots individual points to form the curve.

Interactive Graph Demo

Enter coefficients below to visualize the cubic function in real-time:

VB6 Code Example

This VB6 implementation creates a cubic graph plotter with a simple interface:

Dim a, b, c, d As Integer
Dim x As Double

Private Function f(x As Variant)
    ' Transform coordinates to start from center
    f = a * (5 - x) ^ 3 + b * (5 - x) ^ 2 + c * (5 - x) + d
End Function

Private Sub cmd_Clear_Click()
    pic_graph.Cls
    txt_a.Text = ""
    txt_b.Text = ""
    txt_c.Text = ""
    txt_d.Text = ""
End Sub

Private Sub cmd_draw_Click()
    Dim y As Double
    Dim w As Double
    
    a = Val(txt_a.Text)
    b = Val(txt_b.Text)
    c = Val(txt_c.Text)
    d = Val(txt_d.Text)
    
    ' Using a scale of 0.5 cm to represent 1 unit
    ' Transform coordinates as VB starts from top left
    For w = 0 To 10 Step 0.0001
        y = f(w)
        pic_graph.PSet (w, 5 - y)
    Next w
End Sub

Note: The VB6 code uses a PictureBox control named pic_graph for drawing and four text boxes for coefficient input.

VB.NET Code Example

This modern VB.NET implementation uses GDI+ for more sophisticated graphing:

Private a, b, c, d As Double

Private Function f(x As Double) As Double
    ' Calculate cubic function value
    Return a * Math.Pow(x, 3) + b * Math.Pow(x, 2) + c * x + d
End Function

Private Sub btnDraw_Click(sender As Object, e As EventArgs) Handles btnDraw.Click
    ' Parse input values
    If Not Double.TryParse(txtA.Text, a) OrElse Not Double.TryParse(txtB.Text, b) _
        OrElse Not Double.TryParse(txtC.Text, c) OrElse Not Double.TryParse(txtD.Text, d) Then
        MessageBox.Show("Please enter valid numeric coefficients")
        Return
    End If
    
    ' Create bitmap and graphics object
    Dim bmp As New Bitmap(picGraph.Width, picGraph.Height)
    Using g As Graphics = Graphics.FromImage(bmp)
        g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        g.Clear(Color.White)
        
        ' Draw axes
        Dim penAxis As New Pen(Color.Black, 1)
        Dim centerX As Integer = picGraph.Width \ 2
        Dim centerY As Integer = picGraph.Height \ 2
        g.DrawLine(penAxis, 0, centerY, picGraph.Width, centerY) ' X-axis
        g.DrawLine(penAxis, centerX, 0, centerX, picGraph.Height) ' Y-axis
        
        ' Draw graph
        Dim penGraph As New Pen(Color.Blue, 2)
        Dim scale As Double = 50 ' Pixels per unit
        Dim prevPoint As PointF? = Nothing
        
        ' Plot the function
        For x As Double = -5 To 5 Step 0.01
            Dim y As Double = f(x)
            Dim px As Single = centerX + x * scale
            Dim py As Single = centerY - y * scale
            
            If prevPoint IsNot Nothing Then
                g.DrawLine(penGraph, prevPoint.Value.X, prevPoint.Value.Y, px, py)
            End If
            prevPoint = New PointF(px, py)
        Next
    End Using
    
    ' Display the graph
    picGraph.Image = bmp
End Sub

Tip: The VB.NET version uses GDI+ for smoother graphics and includes axis drawing for better visualization.

Visual Basic Programming with Code Examples

VISUAL BASIC PROGRAMMING WITH CODE EXAMPLES

A Practical Guide authored by Dr. Liew Voon Kiong for Beginners and Intermediate Developers

Unlock the power of Visual Basic with this comprehensive guide that bridges the gap between classic VB6 and modern VB.NET programming. Learn to create mathematical visualization tools like this cubic graph plotter and many other practical applications.

  • ✅ Core Concepts Made Easy: Data types, control structures, graphics programming, and more
  • ✅ Hands-On Application Building: Create mathematical tools, games, database applications
  • ✅ Dual-Code Format: Learn both VB6 and VB.NET with side-by-side examples
Buy Now on Amazon Kindle Edition

Practice Exercises

Test your Visual Basic skills with these graphing exercises:

Exercise 1: Function Transformation

Modify the cubic graph plotter to include function transformations:

  • Add options to shift the graph horizontally and vertically
  • Implement scaling controls to zoom in/out on the graph
  • Add checkboxes to toggle the display of derivative and integral curves

Exercise 2: Multi-Function Plotter

Extend the program to plot multiple functions simultaneously:

  • Allow users to enter up to 3 different functions
  • Display each function in a different color
  • Add intersection point detection and display coordinates
  • Implement a legend to identify each function

About Us

Dr. Liew Voon Kiong, the founder and webmaster of VBTutor.net, holds a Bachelor's Degree in Mathematics, a Master's Degree in Management, and a Doctorate in Business Administration from the University of South Australia.

With decades of experience in software development and education, Dr. Liew created the renowned Visual Basic Tutorial website in 1996, which has become a top resource for VB learners worldwide. He has authored multiple best-selling books on Visual Basic programming.