Graphic Line Equivalent

Category: Drawing

Date: 02-16-2022

Return to Index


 
'Compilable Example:  (Jose Includes)
'http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
#Compiler PBWin 10
#Compile Exe
#Dim All
%Unicode = 1
#Include "Win32API.inc"
 
Enum Equates Singular
   IDC_Button
   IDC_Graphic
End Enum
 
Global hDlg As Dword
 
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, %WS_Border
   Graphic Attach hDlg, %IDC_Graphic
   Graphic Set Overlap
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Select Case Cb.Msg
      Case %WM_Command
         Select Case Cb.Ctl
            Case %IDC_Button
               DrawLine 62,75, 10,20, %Red
               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
     Graphic Set Pixel (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_01291
'Date: 05-11-2013


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm