Login
Back to forumReply to this topicGo to last reply

Posted By

MMS
on 2024-02-10
21:00:39
 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 happy

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



---------------------------------------------------------

Posted By

George
on 2024-04-01
09:13:27
 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



Link: https://archive.org/details/analog-computing-magazine-07/page/n61/mode/2up

Posted By

MMS
on 2024-04-01
08:46:37
 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)

https://ugbasic.iwashere.eu/target/plus4

Posted By

SVS
on 2024-04-01
11:47:40
 Re: Simple but nice BASIC code examples

Fantastic! This system has even a Multitask mode!
I wish to have a bit more sparetime to try/test it, sigh.

Posted By

MMS
on 2024-04-01
17:31:46
 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.

Original code is at 62'er Magazine Fractal Berge - special issue SH07 S.124 from G.Paret
https://ia903108.us.archive.org/6/items/64er_sonderheft_1986_07/64er_sonderheft_1986_07.pdf

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. happy

The BASIC3.5 version without speedup certainly needs patience. With speedup too happy
(I can run my YAPE at max 650%, good candidate for compilers)





Posted By

George
on 2024-04-03
06:54:53
 Re: Simple but nice BASIC code examples

Barnsley Fern

100 YY(1) = .16
110 XX(2) = .85:XY(2) = .04
120 YX(2) = - .04:YY(2) = .85
130 Y(2) = 1.6
140 XX(3) = .20:XY(3) = - .26
150 YX(3) = .23:YY(3) = .22
160 Y(3) = 1.6
170 XX(4) = - .15:XY(4) = .28
180 YX(4) = .26:YY(4) = .24
190 Y(4) = .44
200 graphic 1,1
210 color4,1:color0,1:color1,6,4:scnclr
220 X = 0:Y = 0
230 FOR I = 1 TO 100000
240 R = INT ( RND (1) * 100)
250 F = ((R < 7)*-1) + ((R < 14)*-1) + 2
260 F = F - ((R = 99)*-1)
270 X = XX(F) * X + XY(F) * Y
280 Y = YX(F) * X + YY(F) * Y
290 Y = Y + Y(F)
300 X% = 150 + X * 29.9
320 Y% = 200- Y * 19.1
330 draw, X% * 1 + 1,Y%
340 NEXT
350 GETKEY K$
360 GRAPHIC 0
run



Wikipedia: Barnsley Fern

Posted By

MMS
on 2024-04-01
19:52:26
 Re: Simple but nice BASIC code examples

Thank you! Very nice fractal!

Posted By

George
on 2024-04-03
06:46:39
 Re: Simple but nice BASIC code examples

You are welcome! This is a fantastic thread. Thats the magic of homecomputing happy
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



Link: https://rosettacode.org/wiki/Mandelbrot_set


Posted By

MMS
on 2024-04-04
15:30:51
 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 happy

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 happy

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! happy

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


how it looks like (updated to have black border):


Posted By

Harry Potter
on 2024-04-03
07:18:44
 Re: Simple but nice BASIC code examples

MMS: I like your fractal. happy 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. sad I'm expecting a new drive in the mail later today, though.

BTW, is there a resource online that contains these codes and more?

Posted By

SVS
on 2024-04-03
09:29:08
 Re: Simple but nice BASIC code examples

Simply fantastic!

Posted By

javierglez
on 2024-04-03
13:14:08
 Re: Simple but nice BASIC code examples

The BBC Microbot has many examples in BBC BASIC. Here I post the conversion of one:

5a=rnd(-10)
10graphic3,1:w=160:h=200:n=8:dimx(n),y(n),r(n):v=30
12fori=1to4:colori-1,asc(mid$("2471",i))-48,asc(mid$("7640",i))-48:next
20fori=1ton:x(i)=rnd(1)*w+1:y(i)=rnd(1)*h+1:r(i)=rnd(1)*v+8:next
50color1,8,7:m=rnd(1):p=(rnd(1)*w+1)/v:fori=1ton:b=(rnd(1)+m)>1
52z=int(rnd(1)*2)-2*b:e=x(i)+m*r(i)*cos(p):f=y(i)+m*r(i)*sin(p)
54g=p+1.5708:gosub70:next
60goto50
70k=cos(g):l=sin(g):forj=-1to1
72drawz,e+j*l,h-f-j*ktoe+j*l+5*k,h-f-j*k-10*l:next:return

(it takes a lot of time to actually produce the picture)


Posted By

George
on 2024-04-03
13:40:47
 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



Posted By

MMS
on 2024-04-04
19:01:04
 Re: Simple but nice BASIC code examples

@Harry Potter: I am happy to have so many positive feedback on this thread.

Well, in fact I was looking for the Simons' BASIC compilers, when on the C64 Wikipedia page I found that link for the 64'er magazine link.

This is one link to several one, though I do not know if you speak German or not, but pictures and some common words may help.
https://www.retropoly.de/retroforum/viewtopic.php?f=20&t=2166

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 happy

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. :-(

Posted By

George
on 2024-04-05
15:59:32
 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



Link: https://www.calormen.com/jsbasic/

Posted By

MMS
on 2024-04-05
15:42:30
 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. grin

Posted By

George
on 2024-04-17
14:52:49
 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



Posted By

Litwr
on 2024-04-17
15:41:13
 Re: Simple but nice BASIC code examples

IMHO These little programs are more pieces of art than coding.

Posted By

George
on 2024-04-18
05:04:01
 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.

Posted By

Lavina
on 2024-04-19
03:55:56
 Re: Simple but nice BASIC code examples

@LITWR in my opinion any program is coding, no matter the actual language... wink 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.

Posted By

MMS
on 2024-04-19
11:45:40
 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.

Posted By

George
on 2024-04-19
22:17:36
 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







Back to topReply to this topic


Copyright © Plus/4 World Team, 2001-2024. Support Plus/4 World on Patreon