This is a function I wrote intended to replace the built-in VB6 Line method. With the internal Line method, in addition to all the intermediate pixels of a line, the first pixel is also drawn. But there's a problem, the last pixel is never drawn. So if you want a complete line between 2 points, you will need to use both the line and pset commands. Another problem is that it doesn't draw anything if the line has no length (the first pixel is the same as the last pixel). The problem with the internal Line method, it sees a line with the first and last points being the same as having a length of 0, and a line with a length of 1 being a line who's last pixel is just adjacent to the first pixel.
My function fixes these problems. A line with the first pixel being the same as the last pixel, using my function, is seen by my function as a line with a length of 1 (though it has a length, this line has no direction, because the first and last pixels are the same), so it draws a single pixel. If the last pixel is adjacent the first pixel, then the line has a length of 2. The length is simply equal to the number of pixels drawn, by my definition. Therefore, a single pixel has a length of 1 (seems counterintuitive, because in real life, a length must also have a direction, but real life is analog, while pixels are quantized, so that's why it's different here). The return value of the function is the length of the line, which is the number of pixels drawn by the function. This is another advantage of my function over the internal built-in Line method, which has no way to return the number of pixels drawn.
Here's the code for my DrawLine function.
My function fixes these problems. A line with the first pixel being the same as the last pixel, using my function, is seen by my function as a line with a length of 1 (though it has a length, this line has no direction, because the first and last pixels are the same), so it draws a single pixel. If the last pixel is adjacent the first pixel, then the line has a length of 2. The length is simply equal to the number of pixels drawn, by my definition. Therefore, a single pixel has a length of 1 (seems counterintuitive, because in real life, a length must also have a direction, but real life is analog, while pixels are quantized, so that's why it's different here). The return value of the function is the length of the line, which is the number of pixels drawn by the function. This is another advantage of my function over the internal built-in Line method, which has no way to return the number of pixels drawn.
Here's the code for my DrawLine function.
Code:
Private Function DrawLine(ByVal x1 As Long, ByVal y1 As Long, ByVal x2 As Long, ByVal y2 As Long) As Long
Dim x As Long
Dim y As Long
Dim m As Single
Dim PixelCount As Long
If (x2 = x1) And (y2 = y1) Then
PSet (x1, y1)
PixelCount = 1
Else
If Abs(x2 - x1) > Abs(y2 - y1) Then
m = (y2 - y1) / (x2 - x1)
If x2 < x1 Then
For x = x1 To x2 Step -1
y = (x - x1) * m + y1
PSet (x, y)
PixelCount = PixelCount + 1
Next x
Else
For x = x1 To x2
y = (x - x1) * m + y1
PSet (x, y)
PixelCount = PixelCount + 1
Next x
End If
Else
m = (x2 - x1) / (y2 - y1)
If y2 < y1 Then
For y = y1 To y2 Step -1
x = (y - y1) * m + x1
PSet (x, y)
PixelCount = PixelCount + 1
Next y
Else
For y = y1 To y2
x = (y - y1) * m + x1
PSet (x, y)
PixelCount = PixelCount + 1
Next y
End If
End If
End If
DrawLine = PixelCount
End Function