Rotate image math (C#)

前端 未结 6 2249
醉话见心
醉话见心 2020-12-05 08:39

I have an image with two points, aligned something like this:

|----------------|
|                |
|    .           |
|                |
|          .     |
         


        
6条回答
  •  无人及你
    2020-12-05 09:18

    No code, sorry, but a stratagy.

    You need to be able to create the result image by sampling the the source image. You know the angle of rotation, so you now need to create a mapper function which maps from the result back to the original.

    The code would simply scan each row of the result image, and map the pixel back to the original image. You can do a simple;

    for (int plotY = 0; plotY < resultHeight; plotY++)
    {
       for (int plotX = 0; plotX < resultWidth; plotX++)
       {
             resultImage.PlotPixel(getOriginalPixel(plotX, plotY, angleOfRotation));
       } 
    }
    

    So now we just need the magical "getOriginalPixel" method, and this is where the maths comes in.

    If we rotate the image 0 degrees, then plotX, plotY is just the X/Y of the original image. But that's no fun.

    pickX = x * cos(angle) - y * sin(angle)
    pickY = y * cos(angle) + x * sin(angle)
    

    I think will map to the source pixel. You'll need to check if it's out of bounds and just return black or something :)

提交回复
热议问题