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