QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: unatic on June 16, 2018, 06:41:08 pm

Title: Rotate an image
Post by: unatic on June 16, 2018, 06:41:08 pm
Using the sample program,  I have been able to flip an image in both directions, but I am unable to figure out how to rotate the image by 90 degrees.  I am using the _PUTIMAGE but have not been able to figure this out.  Any help would be appreciated.
Title: Re: Rotate an image
Post by: Ashish on June 17, 2018, 12:21:37 am
Hi unatic!
See Example #1 of this page - http://qb64.org/wiki/MAPTRIANGLE
Title: Re: Rotate an image
Post by: bplus on June 17, 2018, 11:10:31 am
Here is my test code for RotoZoom2 with independent scales for X and Y:
Title: Re: Rotate an image
Post by: Petr on June 17, 2018, 01:06:46 pm
Good work Bplus, so now just add background shift in Y axis and enemies and game is done :-D
Title: Re: Rotate an image
Post by: bplus on June 17, 2018, 02:54:39 pm
To make the sub even easier to use I have written up some notes for it:
Code: QB64: [Select]
  1. 'notes: X, Y is the place where you want the center of the image to go
  2. ' image is the image handle number
  3. ' xScale and yScale: if you want the image the same size as file image use 1, half the size use .5
  4. ' twice the size use 2... this allows you to stretch/shrink an image into a rectangle area any size
  5. ' Rotation is an angle in Degrees units: 0 degrees is due East (as file image) 270 degrees is due North....
  6. SUB RotoZoom2 (X AS LONG, Y AS LONG, Image AS LONG, xScale AS SINGLE, yScale AS SINGLE, Rotation AS SINGLE)
  7.     DIM px(3) AS SINGLE: DIM py(3) AS SINGLE
  8.     W& = _WIDTH(Image&): H& = _HEIGHT(Image&)
  9.     px(0) = -W& / 2: py(0) = -H& / 2: px(1) = -W& / 2: py(1) = H& / 2
  10.     px(2) = W& / 2: py(2) = H& / 2: px(3) = W& / 2: py(3) = -H& / 2
  11.     sinr! = SIN(-Rotation / 57.2957795131): cosr! = COS(-Rotation / 57.2957795131)
  12.     FOR i& = 0 TO 3
  13.         x2& = (px(i&) * cosr! + sinr! * py(i&)) * xScale + X: y2& = (py(i&) * cosr! - px(i&) * sinr!) * yScale + Y
  14.         px(i&) = x2&: py(i&) = y2&
  15.     NEXT
  16.     _MAPTRIANGLE (0, 0)-(0, H& - 1)-(W& - 1, H& - 1), Image& TO(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
  17.     _MAPTRIANGLE (0, 0)-(W& - 1, 0)-(W& - 1, H& - 1), Image& TO(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
  18.  

EDIT: forgot to make yScale as Single
Title: Re: Rotate an image
Post by: unatic on June 17, 2018, 08:37:27 pm
Thanks for the information and pointers!  I just did not look deep enough at the image functions in QB64.  I will study these samples and after I understand them, I will incorporate the functionality into my program.  Again, thank you!!
Title: Re: Rotate an image
Post by: bplus on June 18, 2018, 09:10:05 am
Ha, yeah, I remember the first time I looked at RotoZoom sub... yikes! a head scratcher until you realize it draws a rectangular image by drawing two triangles: 2 edges and the diagonal, then the other 2 edges and same diagonal. To do that, it calculates the (x, y) points at corners for the triangles at drawing surface. The mapping then takes all points in triangle of image and translates them to triangle on drawing surface.