# Fastest way to decide which pixel to paint in order to draw a straight line

edited September 2008
(after I saw "Bresenhams circle algorithm" in development forum, I decided to ask this in here)

in fact, I want to accelerate a car towards a direction.

car's position is x1,y1, and target position is x2,y2.

I'm using some trigonometric calculation to find the course, and decide to move the car to next pixel. since target changes position in every iteration, so a new path/pixel must be decided every iteration. But what is the fastest method to do this in basic?
Post edited by Arda on

• edited September 2008
1 LET a=0: LET b=0: LET x=100: LET y=125
10 PLOT x,y
20 PLOT a,b
21 IF x>=a THEN LET a=a+1: GO TO 23
22 LET a=a-1
23 IF y>=b THEN LET b=b+1: GO TO 25
24 LET b=b-1
25 GO TO 20
• edited September 2008
chop983 wrote: »
1 LET a=0: LET b=0: LET x=100: LET y=125
10 PLOT x,y
20 PLOT a,b
21 IF x>=a THEN LET a=a+1: GO TO 23
22 LET a=a-1
23 IF y>=b THEN LET b=b+1: GO TO 25
24 LET b=b-1
25 GO TO 20

thanks chop,
but I want to draw a "straight" line.
• edited September 2008
From the ZX81 manual ( Chapter 18 ) on WOS
```10 LET a=0: LET b=0: LET c=255: LET d=175: GO SUB 1000: STOP

100 REM This subroutine draws a (fairly) straight line
110 REM from the pixel (A,B) to the pixel (C,D).
120 REM  Use it as part of some main program that supplies the value of A, B, C & D.
130 REM (If you have not got a memory expansion board then you'll
140 REM probably need to omit the REM statements.)

1000 LET U=C-A
1005 REM U SHOWS HOW MANY STEPS ALONG WE NEED TO GO
1010 LET V=D-B
1015 REM V SHOWS HOW MANY STEPS UP
1020 LET D1X=SGN U
1030 LET D1Y=SGN V
1035 REM (D1X,D1Y) IS A SINGLE STEP IN A DIAGONAL DIRECTION
1040 LET D2X=SGN U
1050 LET D2Y=0
1055 REM (D2X,D2Y) IS A SINGLE STEP LEFT OR RIGHT
1060 LET M=ABS U
1070 LET N=ABS V
1080 IF M>N THEN GOTO 1130
1090 LET D2X=0
1100 LET D2Y=SGN V
1105 REM NOW (D2X,D2Y) IS A SINGLE STEP UP OR DOWN
1110 LET M=ABS V
1120 LET N=ABS U
1130 REM M IS THE LARGER OF ABS U & ABS V, N IS THE SMALLER
1140 LET S=INT (M/2)
1145 REM WE WANT TO MOVE FROM (A,B) TO (C,D) IN M STEPS USING N UP- DOWN OR RIGHT-LEFT STEPS D2, & M-N DIAGONAL STEPS D1, DISTRIBUTED AS EVENLY AS POSSIBLE
1150 FOR I=0 TO M
1160 PLOT A,B
1170 LET S=S+N
1180 IF S<M THEN GOTO 1230
1190 LET S=S-M
1200 LET A=A+D1X
1210 LET B=B+D1Y
1215 REM A DIAGONAL STEP
1220 GOTO 1250
1230 LET A=A+D2X
1240 LET B=B+D2Y
1245 REM AN UP-DOWN OR RIGHT-LEFT STEP
1250 NEXT I
1260 RETURN
```

Tested in BASin
• edited September 2008
I have this code to simulate a homming missile, see if it helps
```5 LET ve=1
10 LET px=10: LET py=88
20 LET mx=245: LET my=88
40 PLOT px,py: PLOT mx,my
100 LET fx=mx-px: LET fy=my-py
110 IF fx>0 THEN LET sx=ve
113 IF fx>0 THEN GO TO 120
115 LET sx=-ve
120 IF fy>0 THEN LET sy=ve
130 IF fy>0 THEN GO TO 180
140 LET sy=-ve
180 IF fx=0 THEN LET sx=0
190 IF fy=0 THEN LET sy=0
200 LET mx=mx-sx: LET my=my-sy
210 PLOT mx,my
220 IF INKEY\$="q" THEN LET py=py+2
221 IF INKEY\$="a" THEN LET py=py-2
222 IF INKEY\$="o" THEN LET px=px-2
223 IF INKEY\$="p" THEN LET px=px+2
224 IF INKEY\$="g" THEN STOP
226 PLOT px,py
230 GO TO 100

```
Tested in Basin
• edited September 2008
thanks geoff, this is the code i want, but there is a problem:

I modified the code to make it like rtunes' code, but it draws jagged lines if I make 1 pixel decisions. if I let for..next to run couple of turns, it gives smooth corners.

here:
```10 LET a=0: LET b=0: LET c=255: LET d=175:
11 LET z\$=INKEY\$
12 IF z\$="q" THEN LET d=d+8
13 IF z\$="a" THEN LET d=d-8
14 IF z\$="o" THEN LET c=c-8
15 IF z\$="p" THEN LET c=c+8
16 PLOT c,d
30 GO SUB 1000
40 GO TO 11
1000 LET U=C-A
1010 LET V=D-B
1020 LET D1X=SGN U
1030 LET D1Y=SGN V
1040 LET D2X=SGN U
1050 LET D2Y=0
1060 LET M=ABS U
1070 LET N=ABS V
1080 IF M>N THEN GO TO 1130
1090 LET D2X=0
1100 LET D2Y=SGN V
1110 LET M=ABS V
1120 LET N=ABS U
1140 LET S=INT (M/2)
1150 FOR i=0 TO 0
1170 LET S=S+N
1180 IF S<M THEN GO TO 1230
1190 LET S=S-M
1200 LET A=A+D1X
1210 LET B=B+D1Y
1220 GO TO 1250
1230 LET A=A+D2X
1240 LET B=B+D2Y
1250 PLOT A,B
1255 NEXT i
1256 PLOT OVER 1;c,d
1260 RETURN
```

as you see, the function does not draw a straight line to the target, it draws cornered turns.

if you increase iteration at line 1150 (say, 4) it draws smoother curves, but this lowers the interactivity as we have to let the function run by itself for a long time.

btw, use qaop to move the target pixel...
• Say ... am I missing something or is line 1080 going off into Nowhereland?
What now?
• F_Clowder wrote: »
Say ... am I missing something or is line 1080 going off into Nowhereland?

It's actually the program that's missing something: the original listing has a REM comment at line 1130. It doesn't actually alter the program execution though; since if you jump to a non-existent line, program flow continues at the next line that does exist after the destination; in this case 1140.
Comp.Sys.Sinclair Crap Games Competition 2017
Everyone has a crap game inside them, let yours out!