Author Topic: Spiro  (Read 2365 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Spiro
« on: February 08, 2012, 05:12:06 AM »

Code: OxygenBasic
  1. include "win64.h"
  2.  
  3. Window "Spiro", 800, 800, 2
  4. Font 12,24,0,"courier"
  5. sys_mode=4
  6.  
  7.  
  8. 'VELOCITIES
  9. '
  10. 'alter these to create different shapes
  11. '
  12. '------------------------------
  13. ti1=0.666  'x angle
  14. ti2=0.999  'y angle
  15. ti3=0.3315 'both x and y angles
  16. '==============================
  17.  
  18. x1=0      'start of line x
  19. y1=0      'start of line y
  20. x2=0      'end of line x
  21. y2=0      'end of line y
  22. xc=400    'centre x
  23. yc=400    'centre y
  24. xa=370.0  'size x
  25. ya=370.0  'size y
  26. an1=0.0   'angle for x
  27. an2=pi/3  'angle for y
  28. an3=0.0   'angle for both x and y
  29. st=0.1    'line step size
  30. cr=32     'line color red
  31. cg=32     'line color green
  32. cb=96     'line color blue
  33. e=5000    'nuber of steps
  34. da=.9999  'decay of amplitude
  35.  
  36. 'backgound color r g b
  37.  
  38. ClsColor 200,200,248
  39.  
  40.  
  41. 'calculating point position
  42.  
  43. macro pos(x,y)
  44.   x=cos(an1)*xa*cos(an3)+xc
  45.   y=sin(an2)*ya*sin(an3)+yc
  46. end macro
  47. '
  48. 'draw shape
  49. '
  50. pos(x1,y1)
  51. for i=1 to e
  52.   pos(x2,y2)
  53.   Line x1, y1, x2, y2, 1, cr,cg,cb
  54.   an1+=st*ti1
  55.   an2+=st*ti2
  56.   an3+=st*ti3
  57.   xa*=da
  58.   ya*=da
  59.   x1=x2
  60.   y1=y2
  61. next
  62.  
  63. 'Esc to exit
  64.  
  65. WaitKey(27)
  66. WinEnd
  67.  

Charles

Charles Pegge

  • Guest
Re: Spiro
« Reply #1 on: February 08, 2012, 09:01:40 AM »
Hi Peter,

When the macro is invoked then x and y are replaced by x1,y1 and then in the iteration, x2 and y2. Gives better performance than a function at the expense of extra binary.

Macros have to be used with caution, I agree, but they can replace small functions to good effect.

Charles

efgee

  • Guest
Re: Spiro
« Reply #2 on: February 08, 2012, 12:05:13 PM »
Peter you can think a macro (as Charles implemented it) as the following:

Code: [Select]
inline function pos(x,y)  
  x=cos(an1)*xa*cos(an3)+xc  
  y=sin(an2)*ya*sin(an3)+yc  
end function

The "inline function" will substitute this:

Code: [Select]
pos(x1,y1)  

for i=1 to e  
  pos(x2,y2)  
  Line x1, y1, x2, y2, 1, cr,cg,cb  
  an1+=st*ti1  
  an2+=st*ti2  
  an3+=st*ti3  
  xa*=da  
  ya*=da  
  x1=x2  
  y1=y2  
next

with that:

Code: [Select]
x1=cos(an1)*xa*cos(an3)+xc  
y1=sin(an2)*ya*sin(an3)+yc

for i=1 to e  
  pos(x2,y2)  
  Line x1, y1, x2, y2, 1, cr,cg,cb  
  an1+=st*ti1  
  an2+=st*ti2  
  an3+=st*ti3  
  xa*=da  
  ya*=da  
  x1=x2  
  y1=y2  
next

at compile time; which makes the app faster as there is one less function call to do...

Aurel

  • Guest
Re: Spiro
« Reply #3 on: February 08, 2012, 02:03:17 PM »
Because i don't have 64bit computer i try Peter example translate to ABasic and resulting
image you can see in attachment.Is this right drawing ?

Charles Pegge

  • Guest
Re: Spiro
« Reply #4 on: February 08, 2012, 03:34:16 PM »

Yes that looks okay, Aurel. Only the colors are different.

Peter's Win64 is somewhat misnamed. It actually works in all modes of Oxygen.

Charles Pegge

  • Guest
Re: Spiro
« Reply #5 on: February 10, 2012, 02:17:33 PM »
Thanks Peter.

And a manual too!

I created an alternative sky, to save using a background BMP

Code: OxygenBasic
  1.   'SetBmp s1,0,0,320,240,0
  2.  'sky
  3.   for y=0 to 239
  4.      line 0,y,320,y,25,10,10,240-y
  5.    next
  6.   'stars
  7.   sys_seed=0x35353535
  8.    for i=0 to 100
  9.       poke rand(0,319),rand(0,239),rand(128,200),rand(128,200),rand(128,200)
  10.    next
  11.  

Charles