3-D Functions on the Vic

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
GreyGhost
Vic 20 Nerd
Posts: 525
Joined: Wed Oct 05, 2005 11:10 pm

3-D Functions on the Vic

Post by GreyGhost »

I spent last night converting 2 programs in the May 1984 issue of Compute written for the C64 to our beloved vic-20. Spheri and Rectan plot 3-D functions to the screen. I used Minigrafik to handle all the hard work(thanks Mike). I got the two programs from the compute! disk collection I found online. I cleaned them up a bit and changed the formulas so it would work on the vic. Pretty easy really.

http://www.freedrive.com/file/1344594

The disk contains a copy of Minigrafik for ease of use( I hope you don't mind Mike). Just LOAD"*",8,1 & RUN to start Minigrafik and then load either vic-rectan or vic-spheri using LOAD"file",8.

VIC-RECTAN:

I played with this one all night last night, changing the equation for Z to see what I could come up with. I also found some equations online. LINE 799 has the formula for Z. Change it around and play with it. I have some preset stuff after the RETURN statement at the end of the program. Just cursor over the line and change the line number to 799 to use any of them.

The program when run will ask for a little input. It's menu asks if you want to see the preset stuff or input your own. If you input your own, make sure you change line 799 to a Z formula you want to see. It also asks for upper and lower X and upper and lower Y. I found that most of the time the larger these numbers got the less detail you could see. I mostly used -1 for lower and 1 for upper values for both X and Y. It will also ask for slices in X and Y. I found that 14 or 15 for both was good. If I used an even number for the upper and lower limits, I would use an even number for the slices as well, otherwise the grid left open lines on the edges of the picture and didn't look very pretty. Also, for a cool shading effect, use 45 slices for X and no slices for Y. The last thing it asks for is the observation angle. I always used 45 degrees for this , but I guess that's more to taste than anything.

VIC-SPHERI:

I have no idea how to manipulate this one. It uses theta and some other Greek numbers to plot with. I'm sure some of you guys will understand it. There is still some preset stuff in the menu that you can look at. A doughnut shape(torus I think it was) and a sphere. It's not exactly round. I'm sure it has to do with the pixel shape and no means for the program to compensate.

One last thing. I recommend using this with an emulator on warp mode or you will be at it for a long time :roll: .

Well there you go. Enjoy.

Later,
Last edited by GreyGhost on Fri Dec 03, 2010 3:37 pm, edited 1 time in total.
Rob
User avatar
Mike
Herr VC
Posts: 4846
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: 3-D Functions on the Vic

Post by Mike »

Hi, Rob,
GreyGhost wrote:The disk contains a copy of Minigrafik for ease of use (I hope you don't mind Mike).
Surely I don't object if MG is used in and distributed alongside third-party programs - it is its very purpose to ease the development of graphics oriented programs and games on the VIC-20. :)

Now for the two programs in this package:
I recommend using this with an emulator on warp mode or you will be at it for a long time. :roll:
I suppose the original versions on the C64 were not very fast, either. :lol: Drawing a 3D grid surely can be sped up by a good amount, so you won't wait for the result over an hour, but rather only a few minutes:

- for some obscure reason, the programs first check the Z range, and then (try to) scale that range to the allowed screen-Y co-ordinates. During that time nothing happens on screen ... I'd rather simply ask for a Z minimum and maximum value instead, scale that one to the allowed range of screen-Y co-ordinates, and ignore function values outside that range. => drawing starts instantly.

- The program RECTAN draws sections of constant Y, then of constant X co-ordinates to draw the grid. Rather than holding one co-ordinate constant, and varying the other one fine, you could simply connect two grid points with the line command in MINIGRAFIK: @ <colour>,<x1>,<y1> TO <x2>,<y2>. That easily eliminates many unnecessary calculations of Z, and at that resolution the introduced inaccuracies in the plot are negligible. => huge speed-up.

- Finally, instead of getting into an infinite loop when the plot has completed, you might consider doing something like this:

Code: Select all

X GETA$:ON-(A$="")GOTO X:@RETURN:END
... which waits for a key, and then returns to text mode before ending the program. :)

Greetings,

Michael
User avatar
GreyGhost
Vic 20 Nerd
Posts: 525
Joined: Wed Oct 05, 2005 11:10 pm

Post by GreyGhost »

Thanks for the reply. I couldn't even begin to implement your suggestions other than the "not ending in an infinite loop".:oops: My mathematical skills are limited. I did however notice that line 270 can be omitted altogether. I guess instead of scaling down from 99999999, it scales up from zero(don't think it added to the speed though). I would like to see this program run faster. If you find some time, maybe you could explain in more detail for me how I would go about making those changes. Does your mandelbrot program use these ideas you spoke of?

By the way a cool shape for Spheri is:

XT = ( 4 + C1 ) * C2
YT = ( 4 + C1 ) * S2
ZT = S1 * C1

with inputs of : 0,180,0,360,14,14 with an observation angle of 320-330.
or : 0,360,0,360,14,14 and the same angle.
Rob
User avatar
Mike
Herr VC
Posts: 4846
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

GreyGhost wrote:If you find some time, maybe you could explain in more detail for me how I would go about making those changes.
I made a quick re-implementation of the whole program, so I could concentrate on the details of the things I suggested (download):

Just two input options, mesh size and view angle (in degrees):

Code: Select all

10 INPUT"MESH SIZE";N
11 INPUT"VIEW ANGLE";P
12 :
You could try N=15 and P=30, for example.

Now come fixed start and end-coordinates in the X,Y plane. These should normally be given per INPUT as well. DX and DY help to calculate intermediary X,Y co-ordinates for f(X,Y) - for this the grid points are numbered from -N to N and addressed in steps of 2.

Code: Select all

13 X1=-4:X2=4:DX=(X2-X1)/(2*N)
14 Y1=-4:Y2=4:DY=(Y2-Y1)/(2*N)
15 :
Precalculated sine and cosine values for the rotation formula in the lines 40 and 41:

Code: Select all

16 C=COS(P*{PI}/180)
17 S=SIN(P*{PI}/180)
18 :
Switch on the hires mode and clear the screen:

Code: Select all

19 @ON:@CLR
20 :
Step along constant Y co-ordinates, calculate Z function values for each X co-ordinate (GOSUB39) and draw a line if the previous (X3,Y3) and current point (X4,Y4) have valid Y co-ordinates. For the first grid point, there exists no valid previous point, therefore Q is set to false before entering the XG loop:

Code: Select all

21 FORYG=-NTONSTEP2:Q=0
22 FORXG=-NTONSTEP2
23 GOSUB39
24 IFPANDQTHEN:@1,X3,Y3TOX4,Y4
25 X3=X4:Y3=Y4:Q=P
26 NEXT
27 NEXT
28 :
Same as above, but stepping along constant X co-ordinates instead:

Code: Select all

29 FORXG=-NTONSTEP2:Q=0
30 FORYG=-NTONSTEP2
31 GOSUB39
32 IFPANDQTHEN:@1,X3,Y3TOX4,Y4
33 X3=X4:Y3=Y4:Q=P
34 NEXT
35 NEXT
36 :
Wait for key, then return to text mode, and end the program.

Code: Select all

37 GETA$:ON-(A$="")GOTO37:@RETURN:END
38 :
Calculate the base screen co-ordinates for a rotated square for each grid point (XG,YG), result in X4, Y4:

Code: Select all

39 XP=55*XG/N:YP=55*YG/N
40 X4=80.5+(C*XP-S*YP)
41 Y4=96.5-(S*XP+C*YP)
42 :
Calculate X and Y values to put into the formula:

Code: Select all

43 X=X1+DX*(XG+N)
44 Y=Y1+DY*(YG+N)
45 :
Calculate Z, adjust Y4, and return whether Y4 is in the valid range of 0 to strictly less than 192:

Code: Select all

46 Z=10*(COS(X)+SIN(Y))
47 :
48 Y4=Y4-Z:P=(Y4>=0)AND(Y4<192):RETURN
To see just the rotated square, try out '46 Z=0' as formula.
Does your Mandelbrot program use these ideas you spoke of?
Well, there is quite another algorithm at work. But of course it also tries to avoid unnecessary calculations.

If any questions remain regarding the program above, don't hesitate to ask.

Michael
Last edited by Mike on Thu Feb 20, 2014 4:26 pm, edited 2 times in total.
User avatar
GreyGhost
Vic 20 Nerd
Posts: 525
Joined: Wed Oct 05, 2005 11:10 pm

Post by GreyGhost »

Hey that is super. The program you wrote is exponentially faster than the two in Compute magazine. I liked it enough to eat my words and decided to make this work Maxigrafik. It looks really cool seeing it so big on the vic20 screen.

Change these lines in the program Mike wrote above, save it on the Maxigrafik disk, load Maxigrafik and then load the program.

Code: Select all

19 clear
24 ifpandqthen draw1,x3,y3tox4,y4
32 ifpandqthen draw1,x3,y3tox4,y4
36 show:end
(delete line 37)
39 xp=73*xg/n:yp=73*yg/n
40 x4=104+(c*xp-s*yp)
41 y4=128-(s*xp+c*yp)
48 y4=y4-z:p=(y4>0)and(y4<256):return
Rob
Post Reply