Posts: 488
Threads: 19
Joined: Sep 2005
My program requires a way to draw a line, but by PSET because I need limits without changing the x and y start and end coordinates. Also, if the pset is isolated, I can just add 3 other ones to create the 2x zoomed version, unless you want to include that, but I want to figure something out.
Posts: 602
Threads: 27
Joined: Oct 2004
google for bresenham, or if you want an easier variant DDA
quote="NecrosIhsan"]
[/quote]
Posts: 2,771
Threads: 96
Joined: Oct 2003
I made this a loooong time ago. Ive modifed it to include a length param.
Im not sure exactly how you need the limits defined, however, This uses 0-100 percent of the line.
Also, I havent tested this with the alteration, although Im sure it will work.
er, second thoughts it would crash if the x1,y1 and x2,y2 coords were identical. Guess I did make this a long time ago :d
Code: 'DarkLine
'--------
'(c) 2004 <Dark>
'DarkLine x1, y1, x2, y2, col, thinkness, step, length
'The arguments:
'***x1, y1, x2, y2***
'-starting and ending coords
'***col***
'-colour of the line
'***thickness***
'-Need I say more? Thickess of 1 is normal size
'***step***
'-the step of the line (1 = full, 2 = dotted, etc)
'***length***
'-the amount of the line to draw in percent. 50 draws half, 100 draws a whole, etc
SUB darkline (x1, y1, x2, y2, linecol, thick, stp, length)
thick = thick - 1
dist = CINT(SQR((x2 - x1) ^ 2 + (y2 - y1) ^ 2))
xstep = (x2 - x1) / dist
ystep = (y2 - y1) / dist
linex = x1
liney = y1
FOR dl = 1 TO dist * (length / 100) STEP stp
linex = linex + xstep * stp
liney = liney + ystep * stp
LINE (linex, liney)-(linex + thick, liney + thick), linecol, BF
NEXT
END SUB
Posts: 488
Threads: 19
Joined: Sep 2005
Well I used the Bresenham Algorythm and modified it to this:
Code: SUB BLine (x%, y%, x2%, y2%, c%)
i% = 0: steep% = 0: e% = 0
IF (x2% - x%) > 0 THEN sx% = 1: ELSE sx% = -1
dx% = ABS(x2% - x%)
IF (y2% - y%) > 0 THEN sy% = 1: ELSE sy% = -1
dy% = ABS(y2% - y%)
IF (dy% > dx%) THEN
steep% = 1
SWAP x%, y%
SWAP dx%, dy%
SWAP sx%, sy%
END IF
e% = 2 * dy% - dx%
FOR i% = 0 TO dx% - 1
IF steep% = 1 THEN PSET (y%+39, x%+1), c%: ELSE PSET (x%+39, y%+1), c%
IF steep% = 1 THEN PSET (y%*2, x%*2), c%: ELSE PSET (x%*2, y%*2), c%
IF steep% = 1 THEN PSET (y%*2+1, x%*2), c%: ELSE PSET (x%*2+1, y%*2), c%
IF steep% = 1 THEN PSET (y%*2, x%*2+1), c%: ELSE PSET (x%*2, y%*2+1), c%
IF steep% = 1 THEN PSET (y%*2+1, x%*2+1), c%: ELSE PSET (x%*2+1, y%*2+1), c%
WHILE e% >= 0
y% = y% + sy%: e% = e% - 2 * dx%
WEND
x% = x% + sx%: e% = e% + 2 * dy%
NEXT
PSET (x2%+39, y2%+1), c%
PSET (x2%*2, y2%*2), c%
PSET (x2%*2+1, y2%*2), c%
PSET (x2%*2, y2%*2+1), c%
PSET (x2%*2+1, y2%*2+1), c%
END SUB
Now it works perfectly and I am glad to say that, my program is pretty much completed and maybe now I'll just add a few more commands like drawing a box filled or unfilled.
|