Raytracer basic code for Amstrad CPC
Great example of very basic raytracer in Basic. Very slow one but works and simply to understand.

Great example of very basic raytracer in Basic. Very slow one but works and simply to understand.

Final result is this, after a lot (imagine!) of time

Code for this:
1 BORDER 0:MODE 0
2 st=TIME/300
3 INK 0,0:INK 1,6:INK 2,2:INK 3,18:INK 4,24:INK 5,26
5 dx=0:dy=0:GOSUB 1000
10 FOR x%=0 TO 639 STEP 4
20 FOR y%=0 TO 399 STEP 2
130 dx=(x%-320)/580
131 dy=(y%-200)/580
140 GOSUB 1000
300 PLOT x%,y%,col%
500 NEXT
505 GOSUB 3000:LOCATE 1,1:PEN 5:PRINT USING "#####,.##s";et
510 NEXT
515 WHILE 1:WEND
520 STOP
1000 mt=1E+10
1020 a=2*(dx*dx+dy*dy+1)
1100 RESTORE 9000
1101 READ ns%
1102 FOR s%=1 TO ns%
1110 READ cx,cy,cz,sr,sc%
1211 b=2*(dx*cx+dy*cy+cz)
1212 c=(cx*cx+cy*cy+cz*cz)-sr
1220 d=b*b-2*a*c
1230 IF d<0 THEN GOTO 1500
1231 d=SQR(d)
1240 t=(b+d)/a
1241 IF t>0 AND t<mt THEN col%=sc%:mt=t
1250 t=(b-d)/a
1251 IF t>0 AND t<mt THEN col%=sc%:mt=t
1500 NEXT
1999 IF mt=1E+10 THEN col%=0
2000 RETURN
3000 et=(TIME/300)-st
3010 RETURN
9000 DATA 4
9001 DATA 0,-1,4,1,1
9002 DATA 2,0,4,1,2
9003 DATA -2,0,4,1,3
9004 DATA 0,-5001,0,25000000,4