Main Menu

Custom Draw Line Function

Started by kevin, November 26, 2019, 04:36:16 AM

Previous topic - Next topic

kevin

  Custom Draw Line Function

   This example is of some code to draw a line dot by dot for those situations where you might need.


  Video:

 




  Source Code:

 
PlayBASIC Code: [Select]
 setfps 10

Do
Cls 0
print fps()



ink rgb(255,255,255)
centertext 160,10,"Custom Line"
ink rgb(255,255,255)
centertext 400+160,10,"PB Line"



lockbuffer

for lines=0 to 50
x1=Rnd(400)
y1=Rnd(400)

x2=Rnd(400)
y2=Rnd(400)


Ink rndrgb()
DrawLine(x1,y1,x2,y2)



; Ink Rgb(255,0,0)
Line x1+400,y1,x2+400,y2

ink rgb(0,255,0)
Circle x1,y1,5
Circle x2,y2,5

Circle x1+400,y1,5
Circle x2+400,y2,5
next

unlockbuffer

Sync
loop

End





` *=---------------------------------------------------------------------=*
` > Custom Draw Line Function <<
` *=---------------------------------------------------------------------=*


Function DrawLine(xpos1,ypos1,xpos2,ypos2)
If Xpos1<Xpos2
X1=Xpos1
Y1=Ypos1
X2=Xpos2
Y2=Ypos2
Else
X1=Xpos2
Y1=Ypos2
X2=Xpos1
Y2=Ypos1
Endif

DX#=x2-x1
DY#=Y2-Y1

Height=Dy#
If Height<0 then Height=Height*-1

If Height<Dx#
If dx#=0 then dx#=0.1
g#=Dy#/Dx#

Width=Dx#-1
If Width>-1
For lp=0 to Width
Dot x1+lp,y1+(g#*lp)
next lp
; unlockbuffer

Endif
Else

If Dy#=>0
ys=1
Else
ys=-1
DX#=DX#*-1
Endif

g#=DX#/Dy#
Xpos#=x1
; lockbuffer

For lp=0 to Height-1
Dot Xpos#,y1
Y1=y1+Ys
Xpos#=Xpos#+g#
next lp
; unlockbuffer

Endif

EndFunction






Music by: https://bensound.com