--- title: "Polygons" type: "article" slug: "polygons" url: "http://localhost/article/polygons/" markdown_url: "http://localhost/article/polygons.md" published_at: "2020-10-27T17:15:16+00:00" modified_at: "2026-07-27T09:16:13+00:00" featured_image: url: "http://localhost/wp-content/uploads/2022/04/20230809-041332.jpg" excerpt: "One of the best features of the T/S 2068 computer is its ability to create and display high resolution graphics. There are commands in TS BASIC to PLOT points, DRAW lines, and even make CIRCLEs and arcs. This article will present subroutines to draw equalateral trianges, squares, penta-hexa-hepta- octa-nona-deca- and other polygons. Polygon shapes have…" category: - name: "Sinclair Timex User Group Newsletter" slug: "sinclair-timex-user-group-newsletter" taxonomy: "category" url: "http://localhost/category/periodicals/sinclair-timex-user-group-newsletter/" post_tag: - name: "Best of Timex/Sinclair 2068 Articles and Documents" slug: "ts2068best" taxonomy: "post_tag" url: "http://localhost/tag/ts2068best/" - name: "TS 2068" slug: "ts2068" taxonomy: "post_tag" url: "http://localhost/tag/ts2068/" - name: "Type-in program" slug: "type-in-program" taxonomy: "post_tag" url: "http://localhost/tag/type-in-program/" model: - name: "Timex/Sinclair 2068" slug: "ts-2068" taxonomy: "model" url: "http://localhost/model/ts-2068/" indiv: - name: "John Kemeny" slug: "john-kemeny" taxonomy: "indiv" url: "http://localhost/indiv/john-kemeny/" publication: "Sinclair Timex User Group Newsletter" publication_r: id: 21538 title: "Sinclair Timex User Group Newsletter" type: "periodical" url: "http://localhost/periodical/sinclair-timex-user-group-newsletter/" authors: "John Kemeny" authors_r: - name: "John Kemeny" slug: "john-kemeny" taxonomy: "indiv" url: "http://localhost/indiv/john-kemeny/" volume: "3" issue: "3" issues_articles: - id: 33972 title: "Sinclair Timex User Group Newsletter v3 n3" type: "issue" url: "http://localhost/issue/sinclair-timex-user-group-newsletter-v3-n3/" pages: "1, 6-8" pubdate: "March 1984" archive_link: false volumeissue: "v3n3" gallery: - url: "http://localhost/wp-content/uploads/2020/10/kemeny-polygons-1.png" - url: "http://localhost/wp-content/uploads/2020/10/kemeny-polygons-4.png" - url: "http://localhost/wp-content/uploads/2020/10/kemeny-polygons-3.png" - url: "http://localhost/wp-content/uploads/2020/10/kemeny-polygons-2.png" --- # Polygons One of the best features of the T/S 2068 computer is its ability to create and display high resolution graphics. There are commands in TS BASIC to PLOT points, DRAW lines, and even make CIRCLEs and arcs. This article will present subroutines to draw equalateral trianges, squares, penta-hexa-hepta- octa-nona-deca- and other polygons. Polygon shapes have equal sides and equal angles. In addition, we hope to demonstate in this article something about developing programs and making them more efficient. Let’s start with the inputs (see figure 1). We need to know the coordinates for the center, O, and the radius, OA. ``` 10 INPUT "CENTER'S COORDINATES"'X,Y 20 INPUT "RADIUS"'R ``` The x-coordinate should be between 0 and 255, and the y between 0 and 175. Also, the radius should be small enough so that the polygon won’t go off the screen, i.e., we won’t do what’s known as “clipping.” To enforce our “shoulds” we can add the following line: ``` 25 IF X-R<0 OR X+R>255 OR Y-R<0 OR Y+R>175 THEN GOTO 10 ``` We also need to know the number of sides. ``` 30 INPUT "NUMBER OF SIDES"'N ``` Finally, if we want to be able to draw polygons in any orientation, e.g., a 4-gon as either a square or a diamond, we need an initial orientation: ``` 40 INPUT "ORIENTATION"'P ``` For angles, computers generally use radians instead of degrees, that’s what the Sinclair-Timex’ expects for their trig functions. There are 2*PI radians in 360 degrees. If we want to enter the orientation in degrees, it is a simple matter to convert it. ``` 45 LET P=P*PI/180 ``` From now on we’ll consider all angles in radians. We can develop our algorithm using a little trigonometry. (If you want to create computer graphics, you shouldn’t have slept through those trig classes in high school.) We’ll draw N sides, so: ``` 100 FOR I=1 TO N ``` From figure 2 we can see how to PLOT the initial point, A. ``` 110 PLOT X+R*COS P,Y+R*SIN P ``` To get from A to B we have to draw the vector B-A. Since we have N +slices in our polygon “pizza” and 2*PI radians per “pie” (2 pi’s in 1?), each slice, like angle BOA, has 2*PI/N radians. Thus drawing vector B-A is: ``` 120 LET Q=P+2*PI/N 130 DRAW R*(COS Q-COS P), R*(SIN Q-SIN P) ``` To continue drawing the polygon we need to repeat (iterate) this step using the point B as our starting point. We can accomplish this by changing the “old” initial angle P. ``` 140 LET P=Q 150 NEXT I ``` This is our first algorithm. Stop and try it. Enter 80 and 80 for the center, 60 for the radius, 8 for the number of sides, and PI/8 radians (or 22.5 degrees) for the orientation. This algorithm is straightforward, but not very efficient. To make it run faster we can try to shorten the work done inside the loop. This may lead to a longer initialization, but it saves execution time because the work done inside the loop isn’t repeated. For example, let’s add line 50 and change 120: ``` 50 LET V=2*PI/N 120 LET Q=P+V ``` This saves some calculation time. But our real problem is that each iteration of the loop requires six trig functions (SIN and COS) to be evaluated. Can improve on this? Look at figure 3. Note that the length of each vector is the same. Call this length S. Also note that the angle of the vectors, call it T, keeps changing by 2*PI/N radians in each iteration (going counterclockwise). Let’s modify our algorithm by changing line 130 and adding 135: ``` 130 DRAW S*COS T, S*SIN T 135 LET T=T+V ``` We must remember to initialize S and T (derivations are left as exercises for the reader). ``` 60 LET S=2*R*SIN(V/2) 70 LET T=P+PI/2+V/2 ``` In addition, since Q is no longer used in line 130, we can consolidate by deleting line 140 and changing line 120 to: ``` 120 LET P=P+V ``` There is a second, faster, algorithm. Let’s look next at moving the PLOT statement outside the loop. In theory, a new side starts where the previous one left off. But the fact that we are working on a high, yet finite, resolution computer, and not an ideal mathematical plane, can get us in trouble. Because in practice a roundoff may cause our point to be one tiny pixel off. Unfortunately, this condition is cummulative over the sides, so the net result can be a polygon that doesn’t close properly. But do not abandon hope! For small values of N the effect is negligible; and polygons with many sides aren’t really distinguishable from circles anyway. So, to create this third algorithm, move line 110 to 80 and delete line 120. Notice how little is left in the loop. Further speedup is possible. For example, we could replace the FOR/NEXT control structure with an IF THEN and GOTO. Another idea is to use table lookup methods to compute the trig functions. Finally, we could always resort to machine code. Having identified and boiled down the bottleneck, i.e. the loop, greatly simplifies using machine code for the problem. Let’s finish with a flourish. Add a new input and change line 50 to: ``` 49 INPUT "HOPS"'H 50 LET V=2*PI*H/N ``` Try center 80 and 80, radius 60, sides 5, orientation 18 degrees, and 2 hops. Why do you think we called it “hops”? ## Final Polygon Algorithm ``` 10 INPUT "CENTER'S COORDINATES"'X,Y 20 INPUT "RADIUS"'R 25 IF X-R<0 OR X+R>255 OR Y-R<0 OR Y+R>175 THEN GOTO 10 30 INPUT "NUMBER OF SIDES"'N 40 INPUT "ORIENTATION"'P 45 LET P=P*PI/180 49 INPUT "HOPS"'H 50 LET V=2*PI*H/N 60 LET S=2*R*SIN(V/2) 70 LET T=P+PI/2+V/2 80 PLOT X+R*COS P, Y+R*SIN P 100 FOR I=1 TO N 130 DRAW S*COS T, S*SIN T 135 LET T=T+V 150 NEXT I ```