Jump to content

Photo

Graphics 8 Fedora Hat


124 replies to this topic

#1 Wally1 OFFLINE  

Wally1

    Moonsweeper

  • 271 posts
  • RAP SUCKS!

Posted Sun Nov 10, 2013 11:26 AM

Does anybody remember a basic program (about 10-12 lines) involving formulas

 

to draw a Fedora type hat in graphics 8?

 

it was cool, I'd like to analyze it, if somebody could paste the code?

 

thank you

 



#2 NuY OFFLINE  

NuY

    Chopper Commander

  • 213 posts

Posted Sun Nov 10, 2013 11:45 AM

Here you go:

11 GRAPHICS 24:COLOR 1:RESTORE
20 REM ...PROGRAM THAT PUTS A NEW PICTURE ON THE TV SCREEN GOES HERE EXCEPT SEE LINE 10
23 REM MICRO TECH UNLIM. AD IN 11/81,,,,,,  COMPUTE
25 P=160:Q=100
30 XP=144:XR=1.5*3.1415927
40 YP=56:YR=1:ZP=64
50 XF=XR/XP:YF=YP/YR:ZF=XR/ZP
60 FOR ZI=-Q TO Q-1 STEP 1
70 IF ZI<-ZP OR ZI>ZP THEN GOTO 150
80 ZT=ZI*XP/ZP:ZZ=ZI
90 XL=(SQR(XP*XP-ZT*ZT))
93 XL=INT(0.5+XL)
100 FOR XI=-XL TO XL STEP 1
105 TRAP 120
110 XT=SQR(XI*XI+ZT*ZT)*XF:XX=XI
120 YY=(SIN(XT)+0.4*SIN(3*XT))*YF
130 GOSUB 170
140 NEXT XI
145 IF PEEK(764)=42 THEN IN4O8=8:GOTO 300
150 NEXT ZI
160 GOTO 300
170 X1=(XX+ZZ+P)
180 Y1=YY-ZZ+Q:Y1=191-Y1
182 IF X1<0 OR X1>319 THEN RETURN
184 IF Y1<0 OR Y1>191 THEN RETURN
195 COLOR 1:PLOT X1,Y1
200 IF Y1>=190 THEN RETURN
210 COLOR 2:PLOT X1,Y1+1:DRAWTO X1,191
220 RETURN
230 REM ... PROGRAM TO PUT PICTURE ON TV SCREEN ENDS HERE
300 GOTO 300

Taken from Compute! issue 30 in a program to save a graphics 8 screen to disk.



#3 MrFish OFFLINE  

MrFish

    River Patroller

  • 4,088 posts
  • Location:1010-1010

Posted Sun Nov 10, 2013 12:29 PM

Awesome!



#4 Wally1 OFFLINE  

Wally1

    Moonsweeper

  • Topic Starter
  • 271 posts
  • RAP SUCKS!

Posted Sun Nov 10, 2013 12:57 PM

Hey, thanks a million!

 

Wally



#5 Hobart OFFLINE  

Hobart

    Combat Commando

  • 7 posts
  • Location:DENVER CO US

Posted Sun Feb 22, 2015 1:04 AM

Some more history of that program:

 

Atari 8-bit "Archimedes Spiral" demo - Found again! 

 

-jon



#6 Larry OFFLINE  

Larry

    River Patroller

  • 3,852 posts
  • Location:U.S. -- Midwest

Posted Sun Feb 22, 2015 7:28 AM

Now to see (many years later) how fast we can get it to run using more modern Basics/Basic Compilers/accelerators (real hardware).

 

-Larry



#7 Rybags ONLINE  

Rybags

    Quadrunner

  • 15,118 posts
  • Location:Australia

Posted Sun Feb 22, 2015 7:57 AM

Faster line draw would help a bit.  The part that erases from X,Y to X,191 could probably be changed to a smaller value which would speed it up a little.

 

Anything that uses SIN or other trig functions will be slow.  It's often a better idea to put them into an array, the granuality would need to be worked out first.

 

Reordering and packing the lines of the Basic program also.

 

I get the feeling we went over a lot of this in another thread in the last year or so.



#8 bfollett OFFLINE  

bfollett

    Dragonstomper

  • 508 posts

Posted Fri Feb 27, 2015 1:52 PM

I've had the folks over in the TI 99/4A forum playing with this example to get it running on that platform and they've been playing around with some optimizations.  The following code cuts the time down to about a 28 minute run from the original 3 hour run.  I tested it also with the Fast floating point option in Altirra and using Altirra Basic and that actually get the run time down to about 6 1/2 minutes.  Not sure what a compiled Basic would accomplish.

 

100 DIM RR(320)
120 FOR I=0 TO 320:RR(I)=193:NEXT I
140 GRAPHICS 8+16:SETCOLOR 2,0,0
150 XP=144:XR=4.71238905:XF=XR/XP
160 FOR ZI=64 TO -64 STEP -1
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
223 IF RR(X1)<=Y1 THEN 250
226 RR(X1)=Y1
230 TRAP 250:COLOR 1:PLOT X1,Y1
250 NEXT XI:NEXT ZI
260 GOTO 260

 

Bob
 


Edited by bfollett, Fri Feb 27, 2015 1:53 PM.


#9 Stephen OFFLINE  

Stephen

    Quadrunner

  • 6,390 posts
  • A8 Gear Head
  • Location:Akron, Ohio

Posted Fri Feb 27, 2015 2:12 PM

Lookup table for SIN function would be a huge speedup.  I'm not sure if a table for SQRT could be constructed for this.



#10 Synthpopalooza OFFLINE  

Synthpopalooza

    Stargunner

  • 1,383 posts
  • Location:knoxville, TN

Posted Fri Feb 27, 2015 3:50 PM

What about making RR a string variable and using ascii values? Would that speed anything up?

#11 Rybags ONLINE  

Rybags

    Quadrunner

  • 15,118 posts
  • Location:Australia

Posted Fri Feb 27, 2015 7:10 PM

Modified DList that only shows every 3rd scanline and blanks in-between would give probably 10-15%

Then switch back to the normal one once the draw is completed.

 

Further optimization could be to pack lines together where possible.



#12 fujidude OFFLINE  

fujidude

    River Patroller

  • 4,658 posts
  • Location:United States of America

Posted Fri Feb 27, 2015 8:06 PM

Run it in an emulator at full speed on a fast computer.  ;-)



#13 dmsc OFFLINE  

dmsc

    Chopper Commander

  • 249 posts
  • Location:Viņa del Mar, Chile

Posted Fri Feb 27, 2015 8:38 PM

Hi!,
 

I've had the folks over in the TI 99/4A forum playing with this example to get it running on that platform and they've been playing around with some optimizations.  The following code cuts the time down to about a 28 minute run from the original 3 hour run.  I tested it also with the Fast floating point option in Altirra and using Altirra Basic and that actually get the run time down to about 6 1/2 minutes.  Not sure what a compiled Basic would accomplish.


A good way to speed-up graphics is simply calculating less values :-)

This interpolates the positions each 10 pixels:
100 DIM RR(320)
110 FOR I=0 TO 320:RR(I)=193:NEXT I
120 GRAPHICS 8+16:SETCOLOR 2,0,0
130 XP=144:XR=4.71238905:XF=XR/XP
140 COLOR 1:TRAP 320
150 FOR ZI=64 TO -64 STEP -1
160   ZT=ZI*2.25:ZS=ZT*ZT
170   XL=INT(SQR(20736-ZS)+0.5)
180   XT=SQR(XL*XL+ZS)*XF
190   YY=(SIN(XT)+SIN(XT*3)*0.4)*56
200   X1=-XL+ZI+160:Y1=90-YY+ZI
210   FOR XI=-XL+10 TO XL+9 STEP 10
220     XT=SQR(XI*XI+ZS)*XF
230     YY=(SIN(XT)+SIN(XT*3)*0.4)*56
240     X2=XI+ZI+160:Y2=90-YY+ZI
250     IY=(Y2-Y1)*0.1
260     XE=X2-1
270     IF XI>XL THEN XE=XL+ZI+160
280     FOR X=X1 TO XE
290       IF RR(X)<=Y1 THEN 320
300       RR(X)=Y1
310       PLOT X,Y1
320       Y1=Y1+IY:NEXT X:X1=X2:Y1=Y2
330   NEXT XI:NEXT ZI
340 GOTO 340
The runtime in TurboBasic XL is 7.5min in PAL, 8min in NTSC, down from about 39min in PAL.

#14 devwebcl OFFLINE  

devwebcl

    Stargunner

  • 1,122 posts
  • Location:Chile

Posted Fri Feb 27, 2015 8:55 PM

Changing few lines based on the algorithm/figure symmetry we can speed-up

(it should take a half of time)

 

which remains me, Knuth quote: "premature optimization is the root of all evil."

 

http://manillismo.bl...arquimedes.html

    190 FOR XI=0 TO XL
    225 X11=-XI+ZI+160
    231 PLOT X11,Y1
    241 PLOT X11,Y1+1:DRAWTO X11,191

Edited by devwebcl, Fri Feb 27, 2015 8:59 PM.


#15 dmsc OFFLINE  

dmsc

    Chopper Commander

  • 249 posts
  • Location:Viņa del Mar, Chile

Posted Fri Feb 27, 2015 9:24 PM

Hi!,
 

Changing few lines based on the algorithm/figure symmetry we can speed-up
(it should take a half of time)


You can combine both optimizations, now runtime in TurboBasic XL (PAL) is 5min 20sec:
100 DIM RR(320)
110 FOR I=0 TO 320:RR(I)=193:NEXT I
120 GRAPHICS 8+16:SETCOLOR 2,0,0
130 XP=144:XR=4.71238905:XF=XR/XP
140 COLOR 1:TRAP 340
150 FOR ZI=64 TO -64 STEP -1
160   ZT=ZI*2.25:ZS=ZT*ZT
170   XL=INT(SQR(20736-ZS)+0.5)
180   XT=SQR(ZS)*XF
190   YY=(SIN(XT)+SIN(XT*3)*0.4)*56
200   X1=ZI+160:Y1=90-YY+ZI:Q=2*X1
210   FOR XI=10 TO XL+9 STEP 10
220     XT=SQR(XI*XI+ZS)*XF
230     YY=(SIN(XT)+SIN(XT*3)*0.4)*56
240     X2=XI+ZI+160:Y2=90-YY+ZI
250     IY=(Y2-Y1)*0.1
260     XE=X2-1
270     IF XI>XL THEN XE=XL+ZI+160
280     FOR X=X1 TO XE
290       IF RR(X)<=Y1 THEN 310
300       RR(X)=Y1:PLOT X,Y1
310       R=Q-X
320       IF RR(R)<=Y1 THEN 340
330       RR(R)=Y1:PLOT R,Y1
340       Y1=Y1+IY:NEXT X:X1=X2:Y1=Y2
350   NEXT XI:NEXT ZI
360 GOTO 360


#16 flashjazzcat OFFLINE  

flashjazzcat

    Quadrunner

  • 12,590 posts
  • Location:United Kingdom

Posted Sat Feb 28, 2015 5:25 AM

Wouldn't another (albeit very tiny) optimisation be to stop computing XL+9 at the top of the FOR loop and instead define a variable equal to XL+9 beforehand?

#17 Rybags ONLINE  

Rybags

    Quadrunner

  • 15,118 posts
  • Location:Australia

Posted Sat Feb 28, 2015 5:47 AM

It's the only reference I can see for it... the destination for loops is only calculated at the first iteration, ie if the value of XL changes during the loop it makes no difference to the exit criteria.



#18 flashjazzcat OFFLINE  

flashjazzcat

    Quadrunner

  • 12,590 posts
  • Location:United Kingdom

Posted Sat Feb 28, 2015 6:02 AM

I wasn't sure if that was the case: thanks. I don't recall too much about the workings of BASIC.

#19 devwebcl OFFLINE  

devwebcl

    Stargunner

  • 1,122 posts
  • Location:Chile

Posted Sat Feb 28, 2015 6:15 AM

ZI can be optimized as well

The problem is the order for drawing it

#20 bfollett OFFLINE  

bfollett

    Dragonstomper

  • 508 posts

Posted Sat Feb 28, 2015 7:19 AM

The original new code I posted produced the same output as the original code in the article.  I just ran the new code with both new optimizations in post #15 and there is some compromise going on.  The output definitely isn't as smooth, but yes it's so much faster.  Screen shots from new code and new code with optimizations.

 

Bob

 

 

Attached Thumbnails

  • New.png
  • new2.png


#21 ClausB OFFLINE  

ClausB

    Stargunner

  • 1,384 posts
  • Location:Michigan

Posted Sat Feb 28, 2015 7:20 AM

I always thought the sinc function was prettier than the fat hat. Here's the program I wrote back in the day. It takes about 40 minutes (emulated).

 

airydiskbas.png sinc.png


Edited by ClausB, Sat Feb 28, 2015 7:44 AM.


#22 Larry OFFLINE  

Larry

    River Patroller

  • 3,852 posts
  • Location:U.S. -- Midwest

Posted Sat Feb 28, 2015 9:48 AM

The original new code I posted produced the same output as the original code in the article.  I just ran the new code with both new optimizations in post #15 and there is some compromise going on.  The output definitely isn't as smooth, but yes it's so much faster.  Screen shots from new code and new code with optimizations.

 

Bob

 

 

 

Yes, at some point, it's no longer the same thing.  The output should look the same -- not an approximation (IMO).

 

-Larry



#23 dmsc OFFLINE  

dmsc

    Chopper Commander

  • 249 posts
  • Location:Viņa del Mar, Chile

Posted Sat Feb 28, 2015 10:11 AM

Hi!,

The original new code I posted produced the same output as the original code in the article.  I just ran the new code with both new optimizations in post #15 and there is some compromise going on.  The output definitely isn't as smooth, but yes it's so much faster.  Screen shots from new code and new code with optimization

Yes, my code only calculates the complex function once each 10 pixels and interpolates the values, this is the reason for the 7x speedup. You can interpolate over shorter intervals for a closer approximation.

Another possibility would be to use quadratic interpolation, this needs one more add in the inner loop but will look smoother, and by using cubic interpolation the plotted curve can be made completely smooth.

#24 devwebcl OFFLINE  

devwebcl

    Stargunner

  • 1,122 posts
  • Location:Chile

Posted Sat Feb 28, 2015 2:19 PM

The original new code I posted produced the same output as the original code in the article.  I just ran the new code with both new optimizations in post #15 and there is some compromise going on.  The output definitely isn't as smooth, but yes it's so much faster.  Screen shots from new code and new code with optimizations.

 

Bob

 

 

 

Try using my optimization on post #14. The result is the same

http://manillismo.bl...arquimedes.html



#25 devwebcl OFFLINE  

devwebcl

    Stargunner

  • 1,122 posts
  • Location:Chile

Posted Sat Feb 28, 2015 2:23 PM

I have a new optimization avoid some add operators:

 

http://manillismo.bl...quimedes-2.html

160 FOR ZI=96 TO 224
170 ZT=(ZI-160)*2.25:ZS=ZT*ZT
220 X1=XI+ZI:Y1=-70-YY+ZI
225 X11=-XI+ZI

 






0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users