Hi!

I was experimenting with the midmpoint algorythm to draw lines. I'm not a C++ programmer, but I managed to make the code sample work in VB. The only problem is that it only works under some conditions... and I need some help to make it work properly.

Of course I cheated by initially assuming that the line increased from left to right and had a gradient in the range 0 to 1. However, a similar algorithm can be derived for all other cases. A real implementation of line drawing would first test the direction and gradient of the line and then call one of a number of subroutines according to the line type. It would also handle the special cases of horizontal, vertical and 45° lines.
(I tried doing that but I can't work out how!)

Code:
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim X1 As Long
    Dim Y1 As Long
    Dim X2 As Long
    Dim Y2 As Long
    
    X1 = Picture1.Width \ 2
    Y1 = Picture1.Width \ 2
    X2 = X
    Y2 = Y
    
    Dim cX As Long, G As Long, DeltaG1 As Long, DeltaG2 As Long
    Dim a As Long, b As Long
    Dim ScrX As Long, ScrY As Long
    
    a = Y2 - Y1
    b = X2 - X1
    G = 2 * a - b
    DeltaG1 = 2 * (a - b)
    DeltaG2 = 2 * a
    
    For cX = X1 To X2
        If G > 0 Then
            G = G + DeltaG1
            ScrX = ScrX + 1
            ScrY = ScrY + 1
        Else
            G = G + DeltaG2
            ScrX = ScrX + 1
        End If
        
        Picture1.PSet (X1 + ScrX, Y1 + ScrY), vbBlack
    Next cX
End Sub
Can you please help me? I really need this.

Thanks!