Recent

Author Topic: rotate image x degrees  (Read 12118 times)

picstart

  • Full Member
  • ***
  • Posts: 236
rotate image x degrees
« on: August 18, 2012, 09:10:50 pm »

What is the best way to rotate an image a given number of degrees?
Ex. a compass rose loaded from a bmp into Timage.........needs to rotate  to have heading direction toward top of the screen......if the direction was SSE then SSE would be the at the top.
Win7 pro 32 lazarus 1.0RC1

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12842
  • FPC developer.
Re: rotate image x degrees
« Reply #1 on: August 18, 2012, 09:22:56 pm »
The general case is more expensive than n* 90 degrees.

That latter case (n*90 degrees) I researched thoroughly, see

http://stackoverflow.com/questions/848025/rotating-bitmaps-in-code

picstart

  • Full Member
  • ***
  • Posts: 236
Re: rotate image x degrees
« Reply #2 on: August 18, 2012, 11:12:18 pm »
Well,
 90 deg transform doesn't fit the heads up compass requirement. The magnetometer takes several dozens of milli seconds to settle so speed isn't critical. I did a web search and there are API's SetWorldTransform()  XFORM type transforms for other languages that do the rotational transform and interpolate to the nearest pixel..I was hoping lazarus might have something as well.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: rotate image x degrees
« Reply #3 on: August 19, 2012, 02:19:35 pm »
Does something like BGRABitmap do the thing you are after?

Then again if you're planning a full blown graphical game, i can hint about my OpenGL based game engine :)
http://code.google.com/p/nxpascal/
Code: [Select]
procedure DrawRotateS(x,y: single; pattern: word; a,cx,cy, sizeX,sizeY: single);This can draw like 10000 big about fullscreen sized images in fraction of second. Rotate with n% degrees, colored, transluscent, scaled, rotation centered on any point of the image and so forth. Also it accepts patterns for animation, for example you can have several animation frames in same image.

circular

  • Hero Member
  • *****
  • Posts: 4471
    • Personal webpage
Re: rotate image x degrees
« Reply #4 on: August 19, 2012, 03:21:27 pm »
If you don't need intensive drawing, you can use BGRABitmap. Here is a sample project, using BGRAControls and BGRABitmap :
Conscience is the debugger of the mind

circular

  • Hero Member
  • *****
  • Posts: 4471
    • Personal webpage
Re: rotate image x degrees
« Reply #5 on: August 19, 2012, 03:23:49 pm »
It uses PutImageAngle function on a TBGRAGraphicControl. Here is how it looks like :
Conscience is the debugger of the mind

picstart

  • Full Member
  • ***
  • Posts: 236
Re: rotate image x degrees
« Reply #6 on: August 19, 2012, 06:11:56 pm »
Thank you circular
I spent several hours getting 'SetWorldTransform' and TXForm working within the windows unit. I use BGRA alot but thought it only did 90 deg at a time rotations. Often a wrong assumption leads to a diversion..I did get TXForm to rotate  the compass rose but BGRA is my favorite so I'll recode to BGRA. Oh and kudo's the quality code you supplied actually worked out of the box so thanks for testing it before posting it. What's more it is infinitely better than the convoluted code I have for 'SetWorldTransform' and TXForm .

circular

  • Hero Member
  • *****
  • Posts: 4471
    • Personal webpage
Re: rotate image x degrees
« Reply #7 on: August 19, 2012, 07:12:41 pm »
You're welcome.
Conscience is the debugger of the mind

circular

  • Hero Member
  • *****
  • Posts: 4471
    • Personal webpage
Re: rotate image x degrees
« Reply #8 on: August 19, 2012, 07:46:51 pm »
If for some reason you need to play with transforms, you can use Canvas2D property of BGRABitmap, which works like the HTML canvas. There you have "rotate", "translate" etc.

But I also prefer functions like PutImageAngle or PutImageAffine. It's sufficient in most cases.
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018