Plotting basic x_y graphs using arrays.

aman_VB

Member
Joined
Apr 27, 2010
Messages
23
Programming Experience
1-3
I am trying to plot a simple x-y graph based on two arrays. But I can't seem to get it write. Any help would be appreciated.


VB.NET:
Imports System.Drawing.Drawing2D

Public Class Form1
    Dim i, j As Integer

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim x_data(10)
        For i = 0 To x_data.Length - 1
            x_data(i) = 10 * i
        Next

        Dim y_data(10)
        For i = 0 To y_data.Length - 1
            y_data(i) = 20 * i
        Next

        'Plot the data
        Dim plotData As PointF() = {x_data(i), y_data(i)}
        For i = 0 To plotData.Length - 1
            e.Graphics.DrawCurve(Pens.Red, plotData)
        Next
    End Sub
End Class
 
Your code doesn't follow any sensible algorithm. If you don;t know what you're actually trying to accomplish then the chances of writing code to accomplish it are very slim. So, before you write any code, write out a set of steps that you need to take to achieve your aim. Pretend that it's not necessarily for a computer but another person is going to use the steps to perform the function physically. Only when you can do that do you have a full understanding of the process. Then it's time to write code to implement those steps.
 
The code works for when I enter the indexes of array manually. but it breaks down when I tried to loop through. The code below is part of the above and it does work, i just need help with looping through the array(0) through array(10), instead of writing the array out manually. This codes below works fine.

VB.NET:
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim x_data(10) 'Array length x
        For i = 0 To x_data.Length - 1
            x_data(i) = 100 * i
        Next
        Dim y_data(10) ' Array lenth of y
        For i = 0 To y_data.Length - 1
            y_data(i) = 100 * i ^ (1 / 3)
        Next


        Dim my_data0 As New PointF(x_data(0), y_data(0))
        Dim my_data1 As New PointF(x_data(1), y_data(1))
        Dim my_data2 As New PointF(x_data(2), y_data(2))


        Dim plotData As PointF() = {my_data0, my_data1, my_data2}

        For i = 0 To plotData.Length - 1
            e.Graphics.DrawCurve(Pens.Red, plotData)
        Next
    End Sub
 

Latest posts

Back
Top