-
Line algorithm
Hi,
This is the best place where I can post this, I think
Say, I have a subroutine called drawline, which accepts as parameters:
x1, y1, x2, y2
We have a screen, and on point (x1,y1) we start drawing our line to (x2,y2). Each pixel on the line should be determined by an algorithm, and than colored, say gree. Does anybody know an algorithm?
-
Most graphics systems have support the LineTo verb since PostScript came out with it in 1984. Windows version of drawing a line with LineTo:
Code:
'This project needs:
'- picture box
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, lpPoint As POINTAPI) As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Const Yellow = &HFFFF&
Private Sub Form_Load()
Dim Cnt1 As Byte, Cnt2 As Byte, Point As POINTAPI
Me.AutoRedraw = True
Me.ScaleMode = vbPixels
Picture1.ForeColor = Yellow
Picture1.ScaleMode = vbPixels
Picture1.AutoRedraw = True
Cnt1=100
Cnt2=100
'Set the start-point's coördinates
Point.X = Cnt1: Point.Y = Cnt2
'Move the active point
MoveToEx Picture1.hDC.hdc, Cnt1, Cnt2, Point
'Draw a line from the active point to the given point
LineTo Picture1.hdc, 200, 200
Picture1.Refresh
End Sub
-
I think he wanted to know how LineTo drew the line, and how it knows which pixels to change.
-
Yes, that was the point.
It is, however, for a C project, but VB code is nice too, I know both.
-
Ok. My bad.
You asked. So- It's called Bresnham's Algorithm. It's all over the net.
Here is a pretty good explanation, if you can do basic algebra:
http://www.cs.helsinki.fi/group/goa/...s/bresenh.html
-
Kinda nice stuff for the math lessons :D
-
Yer, thanx for that link Jim.