Author Topic: RotoGear - Uses RotoZoom3 to show many gears rotating on the screen  (Read 3247 times)

0 Members and 1 Guest are viewing this topic.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
I wanted to test the seed of the ultra-cool RotoZoom3 Function on my laptop so I put this together to do just that.  It shows many gears of different sizes on the screen and rotates each one at different speeds. 

I can get about 200 gears going on my laptop before I notice a big slowdown.  RotoZoom3 is pretty fast.

The small BAS and PNG image is in the zip below.

- Dav

   (50k)


BAS source here for reference -- you will need the image in the zip...
Code: QB64: [Select]
  1. '============
  2. 'ROTOGEAR.BAS
  3. '============
  4. 'Showcase speed of RotoZoom3 Function(not by me) by
  5. 'rotating a large number of gears on the screen in various sizes.
  6. 'By Dav, OCT/2021
  7.  
  8. SCREEN _NEWIMAGE(700, 700, 32)
  9.  
  10. gear& = _LOADIMAGE("rotogear.png")
  11.  
  12. NumOfGears = 50 'Change number of gears shown on screen here
  13.  
  14. DIM Gearx(NumOfGears), Geary(NumOfGears), GearSize(NumOfGears)
  15. DIM GearRot(NumOfGears), GearSpeed(NumOfGears)
  16.  
  17. 'Init random values for each gear
  18. FOR G = 1 TO NumOfGears
  19.     Gearx(G) = RND * _WIDTH
  20.     Geary(G) = RND * _HEIGHT
  21.     GearSize(G) = RND * 1
  22.     GearRot(G) = 1
  23.     GearSpeed(G) = RND * 10
  24.  
  25.     CLS , _RGB(0, 0, 64)
  26.     FOR G = 1 TO NumOfGears
  27.         RotoZoom3 Gearx(G), Geary(G), gear&, GearSize(G), GearSize(G), _D2R(GearRot(G))
  28.         GearRot(G) = GearRot(G) + GearSpeed(G): IF GearRot(G) > 360 THEN GearRot(G) = 1
  29.     NEXT
  30.     _DISPLAY
  31.     _LIMIT 24
  32.  
  33.  
  34.  
  35. ' Description:
  36. ' Started from a mod of Galleon's in Wiki that both scales and rotates an image.
  37. ' This version scales the x-axis and y-axis independently allowing rotations of image just by changing X or Y Scales
  38. ' making this tightly coded routine a very powerful and versatile image tool.
  39. SUB RotoZoom3 (X AS LONG, Y AS LONG, Image AS LONG, xScale AS SINGLE, yScale AS SINGLE, radianRotation AS SINGLE)
  40.     ' This assumes you have set your drawing location with _DEST or default to screen.
  41.     ' X, Y - is where you want to put the middle of the image
  42.     ' Image - is the handle assigned with _LOADIMAGE
  43.     ' xScale, yScale - are shrinkage < 1 or magnification > 1 on the given axis, 1 just uses image size.
  44.     ' These are multipliers so .5 will create image .5 size on given axis and 2 for twice image size.
  45.     ' radianRotation is the Angle in Radian units to rotate the image
  46.     ' note: Radian units for rotation because it matches angle units of other Basic Trig functions
  47.     '       and saves a little time converting from degree.
  48.     '       Use the _D2R() function if you prefer to work in degree units for angles.
  49.  
  50.     DIM px(3) AS SINGLE: DIM py(3) AS SINGLE ' simple arrays for x, y to hold the 4 corners of image
  51.     DIM W&, H&, sinr!, cosr!, i&, x2&, y2& '   variables for image manipulation
  52.     W& = _WIDTH(Image&): H& = _HEIGHT(Image&)
  53.     px(0) = -W& / 2: py(0) = -H& / 2 'left top corner
  54.     px(1) = -W& / 2: py(1) = H& / 2 ' left bottom corner
  55.     px(2) = W& / 2: py(2) = H& / 2 '  right bottom
  56.     px(3) = W& / 2: py(3) = -H& / 2 ' right top
  57.     sinr! = SIN(-radianRotation): cosr! = COS(-radianRotation) ' rotation helpers
  58.     FOR i& = 0 TO 3 ' calc new point locations with rotation and zoom
  59.         x2& = xScale * (px(i&) * cosr! + sinr! * py(i&)) + X: y2& = yScale * (py(i&) * cosr! - px(i&) * sinr!) + Y
  60.         px(i&) = x2&: py(i&) = y2&
  61.     NEXT
  62.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, H& - 1)-(W& - 1, H& - 1), Image TO(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
  63.     _MAPTRIANGLE _SEAMLESS(0, 0)-(W& - 1, 0)-(W& - 1, H& - 1), Image TO(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
  64.  



Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: RotoGear - Uses RotoZoom3 to show many gears rotating on the screen
« Reply #1 on: October 02, 2021, 09:57:13 pm »
nice :-)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: RotoGear - Uses RotoZoom3 to show many gears rotating on the screen
« Reply #2 on: October 02, 2021, 10:58:19 pm »
That look is great for clocks or watches.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: RotoGear - Uses RotoZoom3 to show many gears rotating on the screen
« Reply #3 on: October 03, 2021, 12:45:35 am »
A little variation. Spinning on top of each other with different directions. Sort of looks like a kaleidoscope.

- Dav

Code: QB64: [Select]
  1. '============
  2. 'ROTOGEAR2.BAS
  3. '============
  4. 'Showcase speed of RotoZoom3 Function(not by me) by
  5. 'rotating a large number of gears on the screen in various sizes.
  6. 'By Dav, OCT/2021
  7.  
  8. SCREEN _NEWIMAGE(700, 700, 32)
  9.  
  10. gear& = _LOADIMAGE("rotogear.png")
  11.  
  12. NumOfGears = 50 'Change number of gears shown on screen here
  13.  
  14. DIM Gearx(NumOfGears), Geary(NumOfGears), GearSize(NumOfGears)
  15. DIM GearRot(NumOfGears), GearSpeed(NumOfGears), GearDir(NumOfGears)
  16.  
  17. 'Init random values for each gear
  18. FOR G = 1 TO NumOfGears
  19.     Gearx(G) = _WIDTH / 2
  20.     Geary(G) = _HEIGHT / 2
  21.     GearSize(G) = .1 + G / 20
  22.     GearRot(G) = 1
  23.     GearSpeed(G) = RND * 1
  24.     GearDir(G) = INT(RND * 2)
  25.  
  26.     CLS , _RGB(0, 0, 64)
  27.     FOR G = NumOfGears TO 1 STEP -1
  28.         RotoZoom3 Gearx(G), Geary(G), gear&, GearSize(G), GearSize(G), _D2R(GearRot(G))
  29.         IF GearDir(G) = 1 THEN
  30.             GearRot(G) = GearRot(G) + GearSpeed(G): IF GearRot(G) > 360 THEN GearRot(G) = 1
  31.         ELSE
  32.             GearRot(G) = GearRot(G) - GearSpeed(G): IF GearRot(G) < 1 THEN GearRot(G) = 360
  33.         END IF
  34.     NEXT
  35.     _DISPLAY
  36.     _LIMIT 24
  37.  
  38.  
  39.  
  40. ' Description:
  41. ' Started from a mod of Galleon's in Wiki that both scales and rotates an image.
  42. ' This version scales the x-axis and y-axis independently allowing rotations of image just by changing X or Y Scales
  43. ' making this tightly coded routine a very powerful and versatile image tool.
  44. SUB RotoZoom3 (X AS LONG, Y AS LONG, Image AS LONG, xScale AS SINGLE, yScale AS SINGLE, radianRotation AS SINGLE)
  45.     ' This assumes you have set your drawing location with _DEST or default to screen.
  46.     ' X, Y - is where you want to put the middle of the image
  47.     ' Image - is the handle assigned with _LOADIMAGE
  48.     ' xScale, yScale - are shrinkage < 1 or magnification > 1 on the given axis, 1 just uses image size.
  49.     ' These are multipliers so .5 will create image .5 size on given axis and 2 for twice image size.
  50.     ' radianRotation is the Angle in Radian units to rotate the image
  51.     ' note: Radian units for rotation because it matches angle units of other Basic Trig functions
  52.     '       and saves a little time converting from degree.
  53.     '       Use the _D2R() function if you prefer to work in degree units for angles.
  54.  
  55.     DIM px(3) AS SINGLE: DIM py(3) AS SINGLE ' simple arrays for x, y to hold the 4 corners of image
  56.     DIM W&, H&, sinr!, cosr!, i&, x2&, y2& '   variables for image manipulation
  57.     W& = _WIDTH(Image&): H& = _HEIGHT(Image&)
  58.     px(0) = -W& / 2: py(0) = -H& / 2 'left top corner
  59.     px(1) = -W& / 2: py(1) = H& / 2 ' left bottom corner
  60.     px(2) = W& / 2: py(2) = H& / 2 '  right bottom
  61.     px(3) = W& / 2: py(3) = -H& / 2 ' right top
  62.     sinr! = SIN(-radianRotation): cosr! = COS(-radianRotation) ' rotation helpers
  63.     FOR i& = 0 TO 3 ' calc new point locations with rotation and zoom
  64.         x2& = xScale * (px(i&) * cosr! + sinr! * py(i&)) + X: y2& = yScale * (py(i&) * cosr! - px(i&) * sinr!) + Y
  65.         px(i&) = x2&: py(i&) = y2&
  66.     NEXT
  67.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, H& - 1)-(W& - 1, H& - 1), Image TO(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
  68.     _MAPTRIANGLE _SEAMLESS(0, 0)-(W& - 1, 0)-(W& - 1, H& - 1), Image TO(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
  69.