Liberty BASIC Programming Discussions >> Novice >>

Post by

How can I produce dotted lines at graphicbox?

#1.hrp "size 1; down; color red"

#1.hrp "place "; X(0); " "; Y(0)

for x= 0 to 360

#1.hrp "goto "; X(x); " "; Y(x)

next x

Post by

There are probably API way of doing this

Or you can draw dots manually. like here

nomainwin open "test" for graphics as #gr #gr "trapclose [quit]" #gr "down" cx=150 cy=150 r=130 for a = 0 to 2*acs(-1) step acs(-1)/6 x=cx+r*cos(a) y=cy+r*sin(a) #gr dottedLine$(cx,cy,x,y) next #gr "flush" wait [quit] close #gr end function dottedLine$(x1, y1, x2, y2) dx=x2-x1 dy=y2-y1 cmd$="" if abs(dx)>=abs(dy) then 'by x h = 2: if dx <0 then h = -2 for x = x1 to x2 step h y=y1+dy*(x-x1)/dx cmd$=cmd$; "set ";x;" ";y;";" next else 'by y h = 2: if dy <0 then h = -2 for y = y1 to y2 step h x=x1+dx*(y-y1)/dy cmd$=cmd$; "set ";x;" ";y;";" next end if dottedLine$=cmd$ end function

Post by

(Ninja'd by Anatoly)

Here are two examples, each drawing a set of radial lines. One places characters (each with a rectangular backgrount, which makes a bit of a mess), the other draws using 'draw' repeatedly for a series of line segments. View the images at full size to see the detail..

Actually I can think of few occasions I've wanted such lines...

nomainwin WindowWidth =800: WindowHeight =600 open "Dot & dashed lines" for graphics_nsb as #w #w "trapclose quit" #w "color 50 200 50" #w "goto 400 300 ; down ; circle 250 ; size 2" #w "font courier 10 bold" for theta =0 to 360 step 5 call dottedLine 400, 300, 400 +250 *cos( theta *3.14159265 /180), 300 +250 *sin( theta *3.14159265 /180) scan next theta #w "flush" wait sub dottedLine x1, y1, x2, y2 ' need atn2 and cos/sin here? dx =x2 -x1 dy =y2 -y1 L =int( ( dx^2 +dy^2)^0.5) th =atan2( dy, dx) count =0 for radius =0 to L step 12 count =( count +1) mod 2 if count =1 then #w "color red" else #w "color 50 50 200" end if X0 =x1 +( radius /L *dx)' *sin( th) Y0 =y1 +( radius /L *dy)' *cos( th) #w "goto "; X0 -5; " "; Y0+5; " ; down" #w "\" +"@" #w "up" next radius end sub function atan2( y, x) pi =atn( 1) *4 if x <>0 then arctan = atn( y /x) select case case x >0 atan2 =arctan case y >=0 and x <0 atan2 =pi +arctan case y <0 and x <0 atan2 =arctan -pi case y >0 and x =0 atan2 =pi /2 case y <0 and x =0 atan2 =pi /-2 end select end function sub quit h$ close #h$ end end sub

nomainwin WindowWidth =800: WindowHeight =600 open "Dot & dashed lines" for graphics_nsb as #w #w "trapclose quit" #w "size 8; color red" #w "goto 400 300 ; size 2 ; down ; circle 250" for th =0 to 350 step 10 cx =400 +250 *cos( th *3.14159265 /180) cy =300 +250 *sin( th *3.14159265 /180) call dottedLine 400, 300, cx, cy next th #w "flush" scan wait sub dottedLine x1, y1, x2, y2 ' need atn2 and cos/sin here? dx =x2 -x1 dy =y2 -y1 L =int( ( dx^2 +dy^2)^0.5) th =atan2( dy, dx) count =0 for radius =0 to L step 4 count =( count +1) mod 2 if count =0 then #w "color red" else #w "color 50 50 200" s0 =x1 + radius /L *dx t0 =y1 + radius /L *dy s1 =x1 +( radius +1) /L *dx t1 =y1 +( radius +1) /L *dy #w "line "; s0; " "; t0; " "; s1; " "; t1 next radius end sub function atan2( y, x) pi =atn( 1) *4 if x <>0 then arctan = atn( y /x) select case case x >0 atan2 =arctan case y >=0 and x <0 atan2 =pi +arctan case y <0 and x <0 atan2 =arctan -pi case y >0 and x =0 atan2 =pi /2 case y <0 and x =0 atan2 =pi /-2 end select end function sub quit h$ close #w end end sub

Post by

Thank you, tsh73 and tenochtitlanuc!

I understand that there is no direct command to produve dotted lines, I have to produce them "manually".

Anyway I am happy with "graphicbox" at LB.

Best greetings from Tyrol!

Post by

Here's line with 16-bit mask, much like in QBasic.

Try different masks.

EDIT

mask = 65535 ' FFFF, ................ 2^16-1 solid line 'mask = 255 '00FF ........ mask = 3855 '0F0F .... .... 'mask = 13107 '3333 .. .. .. .. 'mask = 43690 'AAAA . . . . . . . .

print hexdec("FF") nomainwin open "test" for graphics as #gr #gr "trapclose [quit]" #gr "down" cx=150 cy=150 r=130 mask = hexdec("FFFF") 'solid line 'mask = hexdec("00FF") mask = hexdec("0F0F") 'mask = hexdec("3333") 'mask = hexdec("AAAA") for a = 0 to 2*acs(-1) step acs(-1)/6 x=cx+r*cos(a) y=cy+r*sin(a) #gr maskedLine$(cx,cy,x,y, mask) next #gr "flush" wait [quit] close #gr end function maskedLine$(x1, y1, x2, y2, mask) ' mask is 16 bit integer dx=x2-x1 dy=y2-y1 cmd$="" if abs(dx)>=abs(dy) then 'by x h = 1: if dx <0 then h = -1 for x = x1 to x2 step h if mask and (2^maskpos) then y=y1+dy*(x-x1)/dx cmd$=cmd$; "set ";int(x);" ";int(y);";" end if maskpos= (maskpos+1) mod 16 next else 'by y h = 1: if dy <0 then h = -1 for y = y1 to y2 step h if mask and (2^maskpos) then x=x1+dx*(y-y1)/dy cmd$=cmd$; "set ";int(x);" ";int(y);";" end if maskpos= (maskpos+1) mod 16 next end if maskedLine$=cmd$ end function

Post by

If you are comfortable making calls to the operating system, you can create a dotted pen and select it into the device context of the graphicbox. It will then be used as the drawing pen for native commands.

http://lbpe.wikispaces.com/PensBrushes

The ways demonstrated in the previous responses are native LB commands and easier to use and understand if you do not have a background in calling Windows API.

If you are new to Liberty BASIC, you might be interested in a couple of online repositories of code, articles and examples.

http://lbpe.wikispaces.com/

http://rosettacode.org/wiki/Category:Liberty_BASIC