Help with drawing lines

juggernot

Well-known member
Joined
Sep 28, 2006
Messages
173
Programming Experience
Beginner
I'm making a graphing program. Right now I'm trying to setup a grid according to the information the user gave on the form before. The user decides the max, min and interval values for x and y. I then use that information, and translate it into a 1000X1000 pixel graph. That's the plan at least, but my code isn't working quite right. The number of lines, and the space between them, seems to be incorrect. Could you look over my code and tell me where I went wrong?

VB.NET:
 Dim point1 As New Point
        Dim point2 As New Point
        Dim graphic1 As System.Drawing.Graphics
        Dim pixelsY As Integer
        Dim pixelsX As Integer
        Dim Xnumlines As Integer
        Dim Ynumlines As Integer
        Dim intcounter As Integer
        Dim mypen As New System.Drawing.Pen(System.Drawing.Color.Black, 2)
'Determines the number of lines to draw
        Ynumlines = gintYmax - gintYmin \ gintYinterval
        Xnumlines = gintXmax - gintXmin \ gintXinterval
'Determines how many pixels between the lines
        pixelsY = 1000 / Ynumlines
        pixelsX = 1000 / Xnumlines
        graphic1 = Me.CreateGraphics
        For intcounter = 0 To Ynumlines Step 1
            point1.X = 0
            point1.Y = intcounter * pixelsY
            point2.X = 1000
            point2.Y = intcounter * pixelsY
            graphic1.DrawLine(mypen, point1, point2)
            intcounter += 1
        Next
 
Well, I've already noticed one problem with my code. I was incrementing my counter twice per loop. Also I changed my for next loop into a do until loop. For some reason the for next loop made one to many lines. Does anyone know why that would be?
 
For Next automatically increments the counter each loop, you added to this also in code (intcounter += 1).

One too many lines? Depends on how many lines you want to draw, currently you are drawing (Ynumlines+1) lines, because you start with 0, 0 is the first line, yNumlines is the last line you draw. The borders you set for For Next counter is inclusive.
 
I noticed the incrementing problem, but I didn't know why I was getting one too many lines. It's because my loop started at 0 huh? That makes sense. Thanks.
 
Back
Top