--- title: "Straight Line Subroutine" type: "article" slug: "straight-line-subroutine" url: "http://localhost/article/straight-line-subroutine/" markdown_url: "http://localhost/article/straight-line-subroutine.md" published_at: "2025-08-09T13:12:52+00:00" modified_at: "2026-06-21T23:26:15+00:00" featured_image: url: "http://localhost/wp-content/uploads/2025/08/SyncWare-News-v1n1-6_0077-1.jpg" excerpt: "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…" category: - name: "SyncWare News" slug: "syncware-news" taxonomy: "category" url: "http://localhost/category/periodicals/syncware-news/" post_tag: - name: "Full Text" slug: "fulltext" taxonomy: "post_tag" url: "http://localhost/tag/fulltext/" - name: "TS 1000" slug: "ts1000" taxonomy: "post_tag" url: "http://localhost/tag/ts1000/" - name: "Type-in program" slug: "type-in-program" taxonomy: "post_tag" url: "http://localhost/tag/type-in-program/" model: - name: "Timex/Sinclair 1000" slug: "ts-1000" taxonomy: "model" url: "http://localhost/model/ts-1000/" indiv: - name: "Fred Nachbaur" slug: "fred-nachbaur" taxonomy: "indiv" url: "http://localhost/indiv/fred-nachbaur/" publication_r: id: 10245 title: "SyncWare News" type: "periodical" url: "http://localhost/periodical/syncware-news/" authors_r: - name: "Fred Nachbaur" slug: "fred-nachbaur" taxonomy: "indiv" url: "http://localhost/indiv/fred-nachbaur/" volume: "1" issues_articles: - id: 38991 title: "SyncWare News v1" type: "issue" url: "http://localhost/issue/syncware-v1/" pages: "76" pubdate: "1984" archive_link: false --- # Straight Line Subroutine 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](http://localhost/article/polar-rectangular-conversion/). 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.