How to rotate a two-dimensional array to an arbitrary degree?

Say I have a bool [] [] and I want to rotate it 37 degrees. I know that the transformation will not always be perfect, and that’s good. I have many answers similar to my question, but the only solutions I found solve the problem with only 90 degrees.

0
source share
4 answers

The best way is to sort through the destinations and for each of them read the correct source location. If you try a different path (for example, looping at the source and recording at the destination), you will get spaces.

The rotation formula is simple ...

source_x = dest_x * c + dest_y * s + x0 source_y = dest_x * -s + dest_y * c + y0 

where c is the cosine of the angle, s is the sine of the angle, and x0, y0 are used to correctly translate the rotated image. In psedudocode

 for y = 0, 1, ... dest_height for x = 0, 1, ... dest_width src_x = c*x + s*y + x0 src_y = -s*x + c*y + y0 copy from source[src_y][src_x] to dest[y][x] 

x0, y0 can be calculated so that the center of the source falls into the center of destination at

  x0 = src_width/2 - c*dest_width/2 - s*dest_height/2 y0 = src_height/2 - c*dest_height/2 + s*dest_width/2 

If instead of using c = cos(angle) and s = sin(angle) you scale both of them with a coefficient k , the resulting image will be rotated and enlarged in the center.

We also note that the formulas are bilinear in x and y ; this means that you can use the full formula for the full value for the first pixel of the row, and then just do src_x += c and src_y -= s for each element of the same row, because this happens when switching from x to x+1 .

Please also note that depending on the size of the source and destination, it may turn out that the calculated source element is not available due to the image. In this case, there are several commonly used options.

  • Write a fixed value (e.g. false )
  • Do not write this destination cell
  • Make a β€œclip” by restricting both coordinates to the maximum allowed before reading
  • Perform the "tile" by normalizing the coordinates using the modulo operator
+5
source

Where x, y are your Cartesian coordinates, and R is your rotation angle:

 newx = x * cos(R) - y * sin(R) newy = x * sin(R) + y * cos(R) 
+4
source

Assuming you rotate a bitmap, I will first consider using an image framework (such as System.Drawing) to achieve what you want. Say take your bools, turn it into a 1-bit bitmap, rotate it and rotate it back.

If this is not what you want, you can either use the rotation provided by wberry and apply to each bool, or if the performance is high, write your own textured rectangle rasterizer. To do the latter, take a look at some old graphic programming tutorials on how to texture a map of arbitrary two-dimensional polygons.

+1
source

If you really, really want to do it yourself, consider using something like AForge - more specifically AForge.Imaging.Filters.RotateBicubic . This procedure processes 24bpp and 8bpp images, so you will need to rotate the RGB content (like a 24bpp image) separately from the alpha channel (like an 8bpp image).

0
source

All Articles