Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
'http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
#Compiler PBWin 10
#Compile Exe
#Dim All
#Debug Error On
#Debug Display On
%Unicode = 1
#Include "Win32API.inc"
Enum Equates Singular
IDC_Button
IDC_Graphic
End Enum
Global hDlg As Dword, C() As Long, bmp$
Function PBMain() As Long
Dialog New Pixels, 0, "PowerBASIC",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_Button,"Push", 10,10,100,20
Control Add Graphic, hDlg, %IDC_Graphic, "", 10,35,150,150
Graphic Attach hDlg, %IDC_Graphic
Graphic Set Overlap
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local w,h As Long
Select Case Cb.Msg
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_Button
Graphic Get Bits To bmp$
Control Get Size hDlg, %IDC_Graphic To w,h
ReDim C(w-1,h-1) At StrPtr(bmp$)+8
DrawLine 62,75, 10,20, Bgr(%Red) '<--- C() needs BGR colors
Graphic Set Bits bmp$
Graphic Line (62,72)-(10,17), %Green
End Select
End Select
End Function
Sub DrawLine(x0 As Long,y0 As Long,x1 As Long,y1 As Long, clr As Long)
Local e,e2,x,y,dx,dy,sx,sy,D As Long
dx = Abs(x1-x0)
dy = Abs(y1-y0)
If x0 < x1 Then sx=1 Else sx=-1
If y0 < y1 Then sy=1 Else sy=-1
e = dx-dy
Do
C(x0,y0) = clr
If x0 = x1 And y0 = y1 Then Exit Loop
e2 = 2*e
If e2 > -dy Then
e = e - dy
x0 = x0 + sx
End If
If e2 < dx Then
e = e + dx
y0 = y0 + sy
End If
Loop
End Sub
'gbs_01286
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm