How does a Line() command works:

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

How does a Line() command works:

Post by BackupUser »

Code updated For 5.20+

Restored from previous forum. Originally posted by MrVainSCL.

[== How does a Line() command works: ==]
---------------------------------------
Maybe some of you have asked yourself how does a Line() command work to get such a smooth drawed line. Some time ago i coded a small Line() replace to see how does it works... Here is the basical behind it. Note: You can´t test it, because PB2.80 dont support the Sgn() math command at the moment ;( But hey, it works! :wink:

Code: Select all

;--------------------------------------------
;
; PureBasic - How does a Line() command work
;
;--------------------------------------------
;
Procedure.l Sgn(x.l)
  If x>>31 = 1
    ProcedureReturn -1
  Else
    ProcedureReturn 1
  EndIf
EndProcedure
Procedure MyLine(x1,y1,x2,y2)
  ;
  x = x1
  y = y1
  StartDrawing(ScreenOutput())       ; ### START DRAWING ###
  Plot (x,y)                       ; Hopefully faster DXPlot() for DirectX support soon :wink:
  StopDrawing()                      ; ### STOP DRAWING ###         
  f = 0
  ;
  Repeat
    f1 = f + Abs(x2 - x1)
    f2 = f - Abs(y2 - y1)
    ;
    ;------------------------
    ;
    If Abs(f1) >= Abs(f2)
      f = f2
      x = x + Sgn(x2 - x1)       ; Still miss Sgn() math command as standard function in PureBasic!
    Else
      f = f1
      y = y + Sgn(y2 - y1)
    EndIf
    ;
    ;------------------------
    ;
    Plot (x,y)
  Until x = x2 And y = y2
  ;
EndProcedure
;   
;--------------------------------------------


PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win98SE + Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

Hi,

in the meantime, while we wait for the Sgn() command, we can use this procedure instead:

Code: Select all

Procedure.l Sgn(x.l)
If x>>31 = 1
  ProcedureReturn -1
Else
  ProcedureReturn 1
EndIf
EndProcedure
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Ok, you are right.... many thanks Pupil :wink:

PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win98SE + Updates...

greetz
MrVainSCL! aka Thorsten
Post Reply