Straight Line Subroutine

Authors

Publication

Pub Details

Date

Pages

See all articles from SyncWare News v1

This subroutine draws an unbroken line segment between pixel (X1,Y1) and (X2,Y2). It is based on the equation for a straight line, Y=M*X+B where M is the slope and B is the Y-intercept.

First the slope is evaluated in line 1020, after making sure that (X2-X1) is non-zero (division by zero is a no-no). If the slope is less than one, the line is drawn by incrementing X, otherwise the line is drawn by incrementing Y, resulting in an unbroken line in either case.

100 INPUT X1
110 INPUT Y1
120 INPUT X2
130 INPUT Y2
140 GOSUB 1000
150 RUN
1000 IF X2-XT=NOT PI THEN LET X2=X2+.1
1005 LET M=(Y2-Y2)/(X2-X1)
1010 IF ABS M>1 THEN GOTO 1100
1020 LET I=Y1-M*X1
1030 FOR B=X1 TO X2 STEP SGN (X2-X1)
1040 LET C=INT (M*B+I+.5)
1050 PLOT B,C
1060 NEXT B
1070 RETURN
1100 FOR C=Y1 TO Y2 STEP SGN (Y2-Y1)
1110 LET B=INT(C+M*X1-Y1)/M
1115 PLOT B,C
1120 NEXT C
1130 RETURN

Enter RUN, then the values of X1, Y1, X2, and Y2. The program draws lines until you fill the screen too far and run out of memory.

If you have a 2K machine (or greater) you can use the subroutine to graph the vectors obtained in the PLR-CART program. Enter or LOAD PLR-CART, and add the subroutine. Now enter the following lines:

295 IF INKEY$="G" THEN GOTO 500
500 CLS
505 FOR B=1 TO 31
510 PRINT AT 11,B;","
520 IF B<22 THEN PRINT AT B,15;"."
530 NEXT B
540 PRINT AT 0,0;"2";TAB 15;"+Y";TAB 31;"1";AT 11,0;"-";TAB 30;"+X";AT 21,0;"3";TAB 15;"-";TAB 31;"4"
550 LET X1=31
560 LET X2=X+X1
570 LET Y1=20
580 LET Y2=Y+Y1
590 GOSUB 1000
595 GOTO 295

After results have been printed, press “G” to draw graph of vector/phasor. Line 540 – display looks best if quadrant markers 1 – 4 are in inverse video.

Beware of vectors that go off the-screen; if you go off to the right or top of the screen, the program stops – enter RETURN to get back to the program. If you go off to the left or the bottom, the vector reflects back – an interesting oddity on the ZX81/TS1000 (PLOT ignores the signs of the argument.) Maximum X value is +/-30, max. Y value is +/-20.

When the display is complete, press any key but BREAK for another problem.

Products

 

Downloadable Media

 
Scroll to Top