I figured I might as well show you the boring stuff I've been working on (well some of it). Specifically I'm going to show you the zoom and rotate capabilities I've added to my procedural texture generation and how to deal with wrapping.
Rotation
Rotation is rather simple:
float NewX = (Cos(Angle) * (OriginalX - CenterX)) - (Sin(Angle) * (OriginalY - CenterY)) + CenterX;
float NewY = (Sin(Angle) * (OriginalX - CenterX)) + (Cos(Angle) * (OriginalY - CenterY)) + CenterY;
Doing this will rotate a single point around an axis defined by you (CenterX and CenterY). However if you do this with an image, you'll notice that this causes black spots to pop up at the corners of the image (basically where nothing exists on the original image). How can we fight this you ask? Simple, flip the image at key locations. Let me show you:
The image in the middle is the one we are trying to rotate. The images above and below the image are flipped around the x axis. The images to the left and right are flipped around the y axis. So when you end up with a position off of the original image due to rotation, we end up on one of the four other images. And since it is only a small amount that should be visible of the other images, the fact that we flipped the image shouldn't be that noticeable. For instance:
is simply a rotation of:
Zoom
Adding zoom is just as simple:
float NewX=OldX*Coefficient;
float NewY=OldY*Coefficient;
The code is a bit simpler than rotation, but we run into the same issue. If you zoom out of an image, you'll see the original image get smaller and smaller in the upper left hand side of the image. In order to combat this, we use a similar technique:
The image in the upper left is the original. The image to the right is flipped along the y axis, the one below the original image is flipped along the x axis, and the one diagonal is flipped along both. However unlike the rotation, we have to deal with images past those. In order to make the images match up, we simply flip it each time we reach a new image. Each time we hit an image along the x axis that is not divisible by 2, we flip the original image around the y axis. Each time we hit an image along the y axis that is not divisible by 2, we flip the original image around the x axis. This leads to images that look like this:
This time around, the pattern becomes very noticeable. But on the bright side, we can zoom in and out of the image without seeing it surrounded by black.
Code
I know that I never explain these concepts in a manner that anyone but myself understands. I'm terrible at explaining anything, but that's why I give you code
:
RotateAndZoom.zip (1.79 kb)
The code works, but keep in mind that it uses a float array (was originally designed for height maps). So if you're going to use an image, you'll have to modify the code. Anyway, download the code, play with it, leave feedback, and as always happy coding.
afa14490-6004-450a-9197-f55bfaf8bd0f|0|.0