Simple but nice BASIC code examples I decided to create a thread for those simple but nice / elegant BASIC codes I see on other plaforms and possible to transfer here, just the spread the greatness of the (Plus/4) BASIC
1) This code is from Ed Solie, on Applesoft BASIC. It was a one-liner. A little late, but still nice (the star could be improved a little)
10 GRAPHIC1,1 20 COLOR4,1:COLOR0,1:COLOR1,10,7 30 FOR I=0 TO 70 STEP 0.1 40 DRAW1,I*COS(I)+160,20-I*(SIN(I)*0.2-2) 50 NEXT I 60 DRAW1,156,20 TO 160,12 TO 164,20 TO 154,16 TO 166,16 TO 156,20 70 GETKEY A$ 80 COLOR1,1:COLOR0,2:GRAPHIC0,1
Re: Simple but nice BASIC code examples 100 REM ARCHIMEDES SPIRAL 110 REM 129 REM ANALOG MAGAZINE Issue 07, 1982-09 130 REM 140 graphic1,1:color4,1:color0,1:color1,6,4:SCNCLR 150 XP=144:XR=4.71238905 :XF=XR/XP 160 FOR ZI=-64 TO 64 170 ZT=ZI*2.25:ZS=ZT*ZT 180 XL=INT(SQR(20736-ZS)+0.5) 190 FOR XI=0-XL TO XL 200 XT=SQR(XI*XI+ZS)*XF 210 YY=(SIN(XT)+SIN(XT*3)*0.4)*56 220 X1=XI+ZI+160:Y1=90-YY+ZI 230 DRAW 1,X1, Y1 240 DRAW0,X1,Y1+1: DRAW0 TO X1, 191 250 NEXT XI:NEXT ZI 260 GOTO 260
Re: Simple but nice BASIC code examples Thank you for sharing the great code! It worked really well.
Yesterday I found a new BASIC dialect, called ugBASIC, and it is a compiler too. Till July 2023 it had no floating point support, but since then it is able to create and compile fast hires gfx, including this. Our compilers (including Austrospeed) cannot improve the speed of hires drawing routines I will download it, and check out how much faster is it compared to our built-in BASIC. Maybe the era of fast and easy development is here? :-)
(strange... why my links never work as intended? I use the exact format given in the Forum Help)
Re: Simple but nice BASIC code examples 3) An other nice example of compact, recursive, fractal drawing routine. UPDATE: strange, but when I edited, it was a correct legit line, but after saving it, the end of long line disappeared. Frankly speaking I dunno what's happening :-) Thanks George for the warning. I try to correct it somehow.
UPDATE2: yeah, it seems the forum is a "little too smart" creating control characters from the code. I added spaces everywhere. If you still cannot get it to work, please refer to the Program code JPG I attach to the end...
It is made for C64 in Simon's BASIC. It is a slightly faster dialect than V3.5, but very similar logic. I had a feeling Commodore took some ideas from Simons' BASIC (released in 1983) as it is also released by Commodore on cartridge. I believe the cartridge should have been added to the C64C version free next to the machine in 1986, it extended the access to sound and graphics of the C64 machine so much.
That 62'er has a lot of goodies for Commodore 16 too, memory maps, handling of windows, etc. Too bad my German is not so great.
The 264 version (it is so compact it should run on C16 too): 10 graphic3,1:color0,1:color1,1: 20 color1,10,5:color2,8,6: color3,10,3:xm=105:ym=105:rem recolor 30 xc=0.77:yc=0.6:T=22:S=40 35 xl=-1.4:xr=1.4:yo=-0.9:yu=1.2 40 dx=(xr-xl)/xm:dy=(yu-yo)/ym 50 for n=0 to ym:y1=yo+n*dy:for m=0toxm:x=xl+m*dx:y=y1:k=0 60 x2=x*x:y2=y*y:y=2*x*y-yc:x=x2-y2-xc:k=k+1 65 if ( k< t ) and (x2+y2 < s) then goto 60 70 u=m+53-n/2:u1=u+1:v=n+80:v1=v-3*(k-1) 75 draw3,u,v to u,v1:draw2,u1,v to u1,v1:draw1,u,v1 to u1,v1 80 next:next 90 getkey a$
In line 20 you can recolor it to your taste. With the original C64 colors did not look so great.
The BASIC3.5 version without speedup certainly needs patience. With speedup too (I can run my YAPE at max 650%, good candidate for compilers)
Re: Simple but nice BASIC code examples You are welcome! This is a fantastic thread. Thats the magic of homecomputing Mandelbrot-Set
100 TH=20:REM ESCAPE THRESHOLD IN ITERATIONS 110 graphic1,1:color4,1:color0,1:color1,6,4:SCNCLR 120 FOR PY=0 TO 100 130 IM = 1.12-PY*2.24/200 140 FOR PX=0 TO 319 150 RE=PX/320*2.47-2 160 X=0:Y=0:IT=0 170 DO WHILE (X*X+Y*Y < =4) AND (IT < TH) 180 XT = X*X-Y*Y+RE 190 Y = 2*X*Y+IM 200 X = XT 210 IT = IT +1 220 LOOP 230 IF IT=TH THEN DRAW 1,PX,PY:DRAW 1,PX,199-PY 240 NEXT PX 250 NEXT PY 260 GETKEY K$ 270 GRAPHIC 0 RUN
Re: Simple but nice BASIC code examples Aaaah, thank you @George, a nice Mandelbrot. Fractals were my thesis work at the university. I like fractals
Herewith I attach the slightly modified version of the above 64'er program from G.Paret, the code produced a more familiar shape with other parameters
UPADTE 04.03: I agree with the comment I received the border would llok better in black. Very easy on 264 machines :-)
Up the Mandrels!
The code (I copied two of your "best practices" graphic 0 after drawing; use a "run" after the code, so it can run at once after copied just by pressing an ENTER):
10 graphic3,1:color0,1:color1,1: 20 color1,6,4:color2,6,6: color3,6,1:color4,1:rem recolor 30 xm=105:ym=105:xc=1:yc=0:T=20:S=60 35 xl=-0.15:xr=0.26:yo=0.47:yu=0.9 40 dx=(xr-xl)/xm:dy=(yu-yo)/ym 50 for n=0 to ym:y1=yo+n*dy:for m=0 to xm: x=xl+m*dx:y=y1:k=0 60 x2=x*x:y2=y*y:y=2*x*y-yc:x=x2-y2-xc:k=k+1 65 if (k < t) and (x2+y2 < s) then goto 60 70 u=m+53-n/2:u1=u+1:v=n+80:v1=v-3*(k-1) 75 draw3,u,v to u,v1:draw2,u1,v to u1,v1:draw1,u,v1 to u1,v1 80 next:next 90 getkey a$ 100 graphic0,1 run
Re: Simple but nice BASIC code examples MMS: I like your fractal. I, too, like fractals. I have a PC program on a floppy called CyberPlot. It graphs the results of 3D equations, and I like using it. In fact, I have some of my favorite plots in a text file. Unfortunately, all of my USB floppy drives are broken, and the only floppy drive that's not is internal to a Win98SE computer at my mother's house. I'm expecting a new drive in the mail later today, though.
BTW, is there a resource online that contains these codes and more?
Re: Simple but nice BASIC code examples Fantastic Posts! Btw ugBasic seems to have a Bug with Hiresmode. Can anybody confirm?
Spiral
5 REM SPIRAL 10 GRAPHIC1,1:COLOR4,1:COLOR0,1:COLOR1,6,4:SCNCLR 20 FOR X=1 TO 321 30 FOR Y=-100 TO 100 40 W = ATN(Y/X) 50 R = SQR(X*X+Y*Y) 60 H = .5 + .5*SIN(2*W+LOG(R)*10) 70 IF H>=RND(1) THEN DRAW,160-X,100+Y:DRAW,159+X,100-Y 80 NEXT Y 90 NEXT X 100 GETKEY A$: GRAPHIC 0 RUN
Intro of the C16 and Plus/4 https://www.retropoly.de/c64er/1985/64er_1985-01.pdf
In fact there were so many great codes released over the last decades, really good source to learn. Those good codes are available not only for C64, but C16 too. Between 85 and 87 a lot of nice codes were released on our platform too in famous magazines, not only on 64'er but in English and US magazines.
Simons' BASIC is a good candidate for simple conversion, as the logic, the screen resolution, most functions/calculations, the commands are very similar and you can easily find a similar color what C64 / C128 had. The C64 BASIC 2.0 codes contain a lot of POKE commands just to set color or make sound, so (although I know most of them) I prefer something less close to register level programming
ZX Spectrum, CPC codes are also very good source (there are literally thousands of good examples), but needs more time to understand and learn the scale of the gfx screens or some special commands we do not have on Commodores.
On Facebook I am also a member of groups "BASIC Programming Languange", "BASIC on the ZX Spectrum", "Commodore 64 BASIC" and "commodore 64/128 programming". sometimes (like the very first Pine tree) some goodies pop up there too.
@George Very nice spiral code! and thank you for the hints Litwr also messaged me that he think the Plus/4 ugBASIC compiler is buggy. :-(
Re: Simple but nice BASIC code examples @MMS Yes. I contacted the developer and he confirmed the bug.
COLORED SERPINSKI TRIANGLES
100 REM COLORED SERPINSKI TRIANGLES 110 GRAPHIC3,1:COLOR4,1:COLOR0,1:COLOR1,6,4:SCNCLR 120 SEED=RND(-PEEK(78)-PEEK(79)*256) 130 REM SCREEN SIZE AND MAIN TRIANGLE VERTICES 140 W=160:W2=80:H=200:H2=100:DIM X(2),Y(2) 150 X(0)=0:Y(0)=H:X(1)=W2:Y(1)=0:X(2)=W:Y(2)=H 160 REM ARRAY OF RANDOM HI-RES COLOURS 170 DIM C(3):C(0)=3:C(1)=6:C(2)=7:C(3)=8 180 FOR I=0 TO 3:J=INT(RND(1)*4) 190 T=C(I):C(I)=C(J):C(J)=T:NEXT 200 REM START POINT AND NUMBER OF DOTS 210 X=0:Y=199:DT=40000 220 REM PLOT POINTS HALFWAY TOWARDS RANDOM VERTICES 230 FOR I=0 TO DT 240 V=RND(1)*3:X=(X+X(V))/2:Y=(Y+Y(V))/2 250 IF Y =>H THEN Y=199 260 REM PICK COLOR FOR EACH TRIANGLE 270 COLOR1,C(0),6:IF Y>H2 THEN COLOR1,C(1),6 280 IF Y>H2 AND X>W2 THEN COLOR1,C(2),6 290 DRAW 1,X,Y:NEXT 300 GETKEY A$: GRAPHIC 0
Re: Simple but nice BASIC code examples @George wow, these triangles look really great! I've never seen them in different colors, what a nice extra! It looks fantastic in this form and color scheme.
Re: Simple but nice BASIC code examples @MMS Thank you. Great thread by the way!
10 REM **** Lissajous curves **** 20 color4,1:color0,1:graphic1,1:c=2 30 dx=35: dy=35: dt=3.1415926535/2 40 for a=1 to 4 50 for b=1 to 3 60 color1,c,5: c=c+1 70 for t=0 to 4*dt step .02 80 x = dx*2*a+dx*sin(a*t+t+1)*.75-dx+15 90 y = dy*2*b+dy*sin(b*t+t)*.75+10-dy-15 100 if t>0 then draw to x,y: else draw,x,y 110 next t, b, a 120 Getkey a$: Graphic0 : color 1,6,4 Run
Re: Simple but nice BASIC code examples @Litwr I agree with you. Indeed pieces of graphical art. Would be cool to collect as much as possible of them over time. I would say this small programms should fit into a screen page.
Re: Simple but nice BASIC code examples @LITWR in my opinion any program is coding, no matter the actual language... coding = programming, or not? The important thing is the end product, that is what the demoscene is about; everything on the demoscene is a kind of art, digital art.
Re: Simple but nice BASIC code examples @Lavina: yeah, but not everyone can code at the level you or some coders can do, those codes in asssembler (=demos) are at the same level mathematics as art, if you get, what I mean.
Believe me, Litwr can do proper programming, and problably know several times more dialects than most of us.
Re: Simple but nice BASIC code examples 5 REM *** EULERS SPIRAL *** 10 COLOR4,1:COLOR0,1:COLOR1,6,4:GRAPHIC1,1 20 CIRCLE 1,160,100,2: LOCATE 160,100 30 SIZE=125: V1=0.001:K=0:V=0:PI=3.1415926535 40 DO 50 D=V/100:X2=0:X3=0:Y2=0:Y3=0:X1=1:Y1=0:Z=D 60 FORI=1TO99STEP2 70 P=(PI*Z*Z)/2:X2=X2+COS(P):Y2=Y2+SIN(P):Z=Z+2*D 80 NEXT 90 Z=2*D 100 FORI=2TO98STEP2 110 P=(PI*Z*Z)/2:X3=X3+COS(P):Y3=Y3+SIN(P):Z=Z+2*D 120 NEXT 130 P=(PI*V*V)/2:X4=COS(P):Y4=SIN(P) 140 X=(D/3)*(X1+X4+4*X2+2*X3):Y=(D/3)*(Y1+Y4+4*Y2+2*Y3) 150 DRAW1,160+(X*SIZE),101-(Y*SIZE):V=V+V1 160 DRAW1,160-(X*SIZE),100+(Y*SIZE) 170 LOOP UNTIL ABS(V)>=5 RUN
You can replace PI with the greek PI Charakter.
"Hexapod"-Fractal A Variation of the Barsnley-Fern
NEW 5 REM *** HEXAPOD *** 30 XX(2) = 0.98: XY(2) = -0.97: YX(2) = 0.9: YY(2) = 0.96: Y(2) = 1.7: 40 XX(4) = -.15: XY(4) = .28: YX(4) = .26: YY(4) = .24: Y(4) = .44: 50 COLOR4,1:COLOR0,1:COLOR1,6,4 55 GRAPHIC1,1 60 X=0:Y=0 70 FOR I = 1 TO 75000 80 R = INT(RND (1) * 100) 90 IF R<86 THEN F=2:ELSE F=4 100 X = XX(F) * X + XY(F) * Y 110 Y = YX(F) * X + YY(F) * Y 120 Y = Y + Y(F) 130 X% = 220 + X * 39.9 140 Y% = 100 - Y * 39.9 150 DRAW, X%,Y% 160 NEXT 170 GETKEY K$:GRAPHIC 0 RUN
Refactored by Csabo (and George): 1 REM LOXODROME 2 COLOR4,1:COLOR.,1:COLOR1,6,4:GRAPHIC1,1 3 B=-1.2:W=.006:D=.6:SD=SIN(D):CD=COS(D):CB=COS(B) 4 FORI=.TO80STEP.1:B=B+W*CB 5 SB=SIN(B):CB=COS(B):CI=COS(I)*CB 6 X=160+96*SIN(I)*CB 7 Y=96*(1-SB*CD-CI*SD) 8 Z=SB*SD-CI*CD 9 IFZ>.THENDRAW1TOX,Y:ELSEDRAW1,X,Y 10 NEXT
Original ORIC code (don't use this) 5 REM LOXODROME 10 color4,1:color0,1:color1,6,4: graphic1,1 15 B=-1.2:W=.06:D=.6 20 XR=120:XB=100:YB=YA 21 YA=96-96*SIN(B)*COS(D)-96* COS(I)*COS(B)*SIN(D) 30 I=I+.1:B=B+W*.1*COS(B) 40 X=160+120*SIN(I)*COS(B)/1.25 50 Y=96-96*SIN(B)*COS(D)-96*COS(I)*COS(B)*SIN(D) 51 Z=SIN(B)*SIN(D)-COS(I)*COS(B)*COS(D) 60 IF Z>0 THEN :DRAW 1 TO X,Y:XA=X:YA=Y 61 H=(2*X+XA)/3:V=(2*Y+YA)/3 65 IF Z<=0 THEN:DRAW 1, X,Y:XA=X:YA=Y 70 IF I<80 THEN 30 80 END
Re: Simple but nice BASIC code examples Thanks for sharing these guys, the code is really useful for my learning. I really like the first one, the Christmas Tree. Its very fitting for the season we are in .
Re: Simple but nice BASIC code examples Very nice, but can I complain a little bit?
I looked at the code for the last one only and I'm... kinda disappointed that it's so sloppy. I'm not saying the "everything posted on Plus/4 World must be the best possible code!" or anything like that, but... Well, let me break it down:
- It's *full* of code that's not used! This is my biggest issue. The assignments to XR, XB, XA, YA, H, V are never used. This makes everything look more complicated and actually slows down the code quite a bit. - The fact that the cases are mixed (line 10 is lowercase for some reason) just looks off - "THEN:" is unnecessary and odd (the colon means there's "blank" instruction there). I'm surprised that even works! - Once the extra code is removed, it's clear that the two separate IF statements in 60 and 65 simply should have been an IF/ELSE.
Here's my version, I did some minor optimization as well: any SIN/COS value that's used more than once is per-calculated.
The original runs for about 5:50, this one runs in about 2:40. About twice as fast, but produces an identical result. Further optimizations are still possible.
So... they are very nice, I'm happy to see them, and I don't expect "super optimized" code, but at least look over it once for such glaring issues
Re: Simple but nice BASIC code examples @Csabo Your complaint is valid. Its a port from the Oric forum and the guy ported it from somewhere else from a magazine. So i guessed the code should habe been revised already.
I should have checked and optimized it, but my focus lied on to get it somehow running by changing the drawing commands. Oric basic is somehow wierd. Also was late at night.
Thank you for your efforts. Its great when people double check things.
EDIT: I also looked over the code and got the runtime down to ~2m:10s