How do I rotate an image?

后端 未结 4 1350
悲&欢浪女
悲&欢浪女 2021-02-08 07:46

See also: Why is my image rotation algorithm not working?

This question isn\'t language specific, and is a math problem. I will however use

4条回答
  •  广开言路
    2021-02-08 08:10

    To rotate an image, you create 3 points:

    A----B 
    |
    |
    C
    

    and rotate that around A. To get the new rotated image you do this:

    • rotate ABC around A in 2D, so this is a single euler rotation
    • traverse in the rotated state from A to B. For every pixel you traverse also from left to right over the horizontal line in the original image. So if the image is an image of width 100, height 50, you'll traverse from A to B in 100 steps and from A to C in 50 steps, drawing 50 lines of 100 pixels in the area formed by ABC in their rotated state.

    This might sound complicated but it's not. Please see this C# code I wrote some time ago: rotoZoomer by me

    When drawing, I alter the source pointers a bit to get a rubber-like effect, but if you disable that, you'll see the code rotates the image without problems. Of course, on some angles you'll get an image which looks slightly distorted. The sourcecode contains comments what's going on so you should be able to grab the math/logic behind it easily.

    If you like Java better, I also have made a java version once, 14 or so years ago ;) -> http://www.xs4all.nl/~perseus/zoom/zoom.java

提交回复
热议问题