Author Topic: RotoZoom3 (Modification) by bplus  (Read 3147 times)

0 Members and 1 Guest are viewing this topic.

Offline Junior Librarian

  • Moderator
  • Newbie
  • Posts: 19
RotoZoom3 (Modification) by bplus
« on: September 25, 2021, 06:20:27 am »
Author and contributor: @bplus @Galleon
Source: QB64.org Wiki and Forum
URL: https://www.qb64.org/wiki/MAPTRIANGLE
https://www.qb64.org/forum/index.php?topic=1511.msg115148#msg115148
Version: 2020 version #3
Tags: [graphics] [image]
Description:
A modification of Galleon's RotoZoom in Wiki that both scales and rotates an image, this version scales the x-axis and y-axis independently allowing some rotations of image just by changing X or Y Scales making this already powerful routine even more a versatile image tool.

Source Code (and demos with Spike Image (attached)):
Code: QB64: [Select]
  1. _TITLE "Another RotoZoom Demo" 'b+ started 2020-03-02
  2.  
  3. CONST xmax = 1200, ymax = 700, xc = 600, yc = 350
  4. SCREEN _NEWIMAGE(xmax, ymax, 32)
  5. _SCREENMOVE 100, 40
  6. DIM SHARED s&, ao
  7. DIM a, x, y, x1, y1, xs, dxs, ddxs, ys, dys, ddys, maxScale
  8.  
  9. ' Starting from an image I pulled from Internet, I used Paint 3D to give it a transparent background.
  10. s& = _LOADIMAGE("tspike.png") 't for transparent background
  11.  
  12.  
  13. ' Standard Rotation about the image center on a given X, Y location. Rotating image in middle of screen,
  14. ' I used something like this to find ideal angle for level point on left head on right.
  15. WHILE _KEYDOWN(27) = 0
  16.     a = a + _PI(1 / 36)
  17.     IF a > _PI(1.999) THEN a = 0
  18.     CLS
  19.     RotoZoom3 xc, yc, s&, 1, 1, a
  20.     PRINT "Raw image rotation:": PRINT
  21.     PRINT "radian angle:"; a; "   degrees:"; _R2D(a) \ 1; " press key for next angle... esc to rotate on y axis"
  22.     WHILE LEN(INKEY$) = 0: _LIMIT 60: WEND
  23.  
  24. ao = _PI(.27) ' I have to offset the image angle by this amount so that the spike point is on the left
  25. '               and the head is on the right at 0 degrees or radians.
  26.  
  27.  
  28. 'Demo of the independent x and y scale for axis rotations
  29. maxScale = 4: dxs = .01: ddxs = 1: xs = maxScale:
  30.     CLS
  31.     PRINT "Press any for rotation on x axis..."
  32.     RotoZoom3 xc, yc, s&, xs, maxScale, ao
  33.     IF xs + dxs > maxScale OR xs + dxs < -maxScale THEN ddxs = ddxs * -1
  34.     xs = xs + dxs * ddxs
  35.     _DISPLAY
  36.     _LIMIT 60
  37.  
  38. ys = maxScale: dys = .01: ddys = 1
  39.     CLS
  40.     PRINT "Press any to see layout of image over whole screen and end demo..."
  41.     RotoZoom3 xc, yc, s&, maxScale, ys, ao
  42.     IF ys + dys > maxScale OR ys + dys < -maxScale THEN ddys = ddys * -1
  43.     ys = ys + dys * ddys
  44.     _DISPLAY
  45.     _LIMIT 60
  46.  
  47. ' Demo of an applied layout on screen
  48. COLOR , &HFFBBBBBB: CLS ' the image has slight gray halo so hide with gray background
  49. FOR x = 40 TO _WIDTH - 40 STEP 20
  50.     RotoZoom3 x, 15, s&, .2, .2, _PI(1.5 + .27)
  51.     RotoZoom3 x, _HEIGHT - 15, s&, .2, .2, _PI(.5 + .27)
  52. FOR y = 40 TO _HEIGHT - 40 STEP 20
  53.     RotoZoom3 15, y, s&, .2, .2, _PI(1 + .27)
  54.     RotoZoom3 _WIDTH - 15, y, s&, .2, .2, _PI(.27)
  55. FOR a = 0 TO _PI(2) STEP _PI(1 / 6)
  56.     x1 = xc + 200 * COS(a)
  57.     y1 = yc + 200 * SIN(a)
  58.     RotoZoom3 x1, y1, s&, 2, 2, a + ao
  59.  
  60. 'And finally a little show. What is better than a knife thrower throwing bananas?
  61. WHILE _KEYDOWN(27) = 0
  62.     CLS
  63.     drawKite xc, .9 * ymax, 600, a + ao
  64.     _DISPLAY
  65.     _LIMIT 30
  66.     a = a + _PI(2 / 360)
  67.  
  68. SUB drawKite (x, y, s, a)
  69.     RotoZoom3 x, y, s&, s / _WIDTH(s&), s / _HEIGHT(s&), a + ao
  70.     IF s > 10 THEN
  71.         drawKite x + .5 * s * COS(_PI(2) - a), (y - .25 * s) + .25 * s * SIN(_PI(2) - a), s / 1.5, a
  72.         drawKite x + .5 * s * COS(_PI + a), (y - .25 * s) + .25 * s * SIN(_PI + a), s / 1.5, a
  73.     END IF
  74.  
  75. ' Description:
  76. ' Started from a mod of Galleon's in Wiki that both scales and rotates an image.
  77. ' This version scales the x-axis and y-axis independently allowing rotations of image just by changing X or Y Scales
  78. ' making this tightly coded routine a very powerful and versatile image tool.
  79. SUB RotoZoom3 (X AS LONG, Y AS LONG, Image AS LONG, xScale AS SINGLE, yScale AS SINGLE, radianRotation AS SINGLE)
  80.     ' This assumes you have set your drawing location with _DEST or default to screen.
  81.     ' X, Y - is where you want to put the middle of the image
  82.     ' Image - is the handle assigned with _LOADIMAGE
  83.     ' xScale, yScale - are shrinkage < 1 or magnification > 1 on the given axis, 1 just uses image size.
  84.     ' These are multipliers so .5 will create image .5 size on given axis and 2 for twice image size.
  85.     ' radianRotation is the Angle in Radian units to rotate the image
  86.     ' note: Radian units for rotation because it matches angle units of other Basic Trig functions
  87.     '       and saves a little time converting from degree.
  88.     '       Use the _D2R() function if you prefer to work in degree units for angles.
  89.  
  90.     DIM px(3) AS SINGLE: DIM py(3) AS SINGLE ' simple arrays for x, y to hold the 4 corners of image
  91.     DIM W&, H&, sinr!, cosr!, i&, x2&, y2& '   variables for image manipulation
  92.     W& = _WIDTH(Image&): H& = _HEIGHT(Image&)
  93.     px(0) = -W& / 2: py(0) = -H& / 2 'left top corner
  94.     px(1) = -W& / 2: py(1) = H& / 2 ' left bottom corner
  95.     px(2) = W& / 2: py(2) = H& / 2 '  right bottom
  96.     px(3) = W& / 2: py(3) = -H& / 2 ' right top
  97.     sinr! = SIN(-radianRotation): cosr! = COS(-radianRotation) ' rotation helpers
  98.     FOR i& = 0 TO 3 ' calc new point locations with rotation and zoom
  99.         x2& = xScale * (px(i&) * cosr! + sinr! * py(i&)) + X: y2& = yScale * (py(i&) * cosr! - px(i&) * sinr!) + Y
  100.         px(i&) = x2&: py(i&) = y2&
  101.     NEXT
  102.     _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))
  103.     _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))
  104.  
  105.  

Attachments:
 
                                                                                                                                         (124 downloads previously)

Spiked.PNG