Authors
Publication
Pub Details
Date
Pages
BASIC Terms
One of the fundamental concepts of computer graphics programming is that of viewport clipping, or eliminating all lines of a display outside the screen.
Type in PROGRAM #1 and run it to see what happens. Notice that when any line attempts to go beyond the screen bounds you get a “B Integer out of range” error.
But using a clipping algorithm, every line segment is checked to see if all or part of it will go off the screen. If so, the program will recalculate the line segment and only DRAW the part lying within the screen.
Now, erase lines 100 and 230 of PROGRAM #1 and add PROGRAM #2. The second program is the clipping algorithm and line 220 does the plotting. Note that the algorithm considers one line segment at a time using xs and ys as the starting coordinates for the X and Y points, and xe, ye as the line end point coordinates.
Once these are set, the algorithm goes to work to clip any part of the line which goes off the screen. Since the first part of this program is only a demo, you can adapt any of your graphics programs to work with this algorithm. You’ll need to study it though, to make it work for you.
The screen viewport is 256 X 176 pixels. Note that the midpoint of the screen bounds are set at line 100. The start and end coordinates must be absolute values, eg. 128,88 or 44,105, and not relative values, eg. -10,25 or -50,-100.
Using “-PEEK23677” and “-PEEK 23678” in line 220 converts the absolute values to relative values because that’s what the DRAW keyword requires. But that’s another story.
This program is compilable using NovelSoft’s “TIMACHINE“. Play with the value in line 10 for other shapes.
Program 1
10 LET n=6
20 FOR b=100 TO 590 STEP 70
30 FOR c=0 TO 810 STEP 19
40 LET r=b*ABS (SIN (n*c)-(COS(n*c)))
50 LET xe=128+r/6*COS c
60 LET ye=88+r/6*SIN c
70 IF c=0 THEN GO SUB 230
80 GO SUB 100: NEXT c: NEXT b
90 PLOT 0,0: DRAW 255,0: DRAW 0,175: DRAW -255,0: DRAW 0,-175:STOP
100 PLOT xs,ys: DRAW xe-PEEK 23677,ye-PEEK 23678
230 LET xs=xe: LET ys=ye: RETURN
Program 2
100 LET mx=127.5: LET my=87.5
110 LET x1=xs-mx: LET y1=ys-my
120 LET x2=xe-mx: LET y2=ye-my
130 GO SUB 300: GO SUB 330
140 IF NOT (s1 OR s2 OR t1 OR t2) THEN GO TO 220
150 IF NOT (s1*s2-1)*(t1*t2-1) THEN GO TO 230
160 IF s1 THEN LET y1=FN y(s1):LET x1=mx*s1: GO SUB 300
170 IF t1 THEN LET x1=FN x(t1):LET y1=my*t1
180 IF s2 THEN LET y2=FN y(s2):LET x2=mx*s2: GO SUB 330
190 IF t2 THEN LET x2=FN x(t2):LET y2=my*t2
200 GO SUB 300: GO SUB 330
210 IF (s1 OR t1 OR s2 OR t2) THEN GO TO 230
220 PLOT x1+mx,y1+my: DRAW x2+mx-PEEK 23677,y2+my-PEEK 23678
230 LET xs=xe: LET ys=ye
240 RETURN
300 LET s1=FN z(x1,mx)
310 LET t1=FN z(y1,my)
320 RETURN
330 LET s2=FN z(x2,mx)
340 LET t2=FN z(y2,my)
350 RETURN
500 DEF FN x(s)=x1+(my*s-y1)*(x2-x1)/(y2-y1)
510 DEF FN y(s)=y1+(mx*s-x1)*(y2-y1)/(x2-x1)
520 DEF FN z(a,m)=SGN a*(ABS a>m)
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.