# Rotate image DIY

Hi all

I have an assignment to make my own rotation method for rotating an image around a point using. Problem is, I’m completely clueless and haven’t been able to find anything helpful.

I’m supposed to have the following input: a grayscale image, an angle, x and y coordinates for the point.

So far, I have made the code that allocate the space needed for the rotated image. But I can’t figure out how to rotate it

``````

int angle = 90;

colorImg.allocate(inputImg.getWidth(), inputImg.getHeight());
colorImg.setFromPixels(inputImg.getPixels(), inputImg.getWidth(), inputImg.getHeight());

grayImg.allocate(inputImg.getWidth(), inputImg.getHeight());
grayImg = colorImg;

//Calculate the size of the rotated image canvas

float point1x = inputImg.getWidth() * cosine;
float point1y = inputImg.getWidth() * sine;
float point2x = -inputImg.getHeight() * sine;
float point2y = inputImg.getHeight() * cosine;
float point3x = inputImg.getWidth() * cosine - inputImg.getHeight() * sine;
float point3y = inputImg.getWidth() * sine + inputImg.getHeight() * cosine;

float minx = min(0 , min(point1x , min(point2x , point3x)));
float miny = min(0 , min(point1y , min(point2y , point3y)));
float maxx = max(point1x , max(point2x , point3x));
float maxy = max(point1y , max(point2y , point3y));

int rotWidth = floor(fabs(maxx) - minx);
int rotHeight = floor(fabs(maxy) - miny);

rotatedImg.allocate(rotWidth, rotHeight);

unsigned char* pixels  = grayImg.getPixels();

//Actually rotate the image
for(int y = 0; y < rotHeight; y++)
{
for(int x = 0; x < rotWidth; x++)
{
//WHAT TO DO HERE?
}
}

``````

Any ideas to get me in the right direction?

something like this should do the trick:

``````

p2.x=cos(a)*(p1.x-o.x)-sin(a)*(p1.y-o.y)+o.x;
p2.y=sin(a)*(p1.x-o.x)+cos(a)*(p1.y-o.y)+o.y;

``````

where p2 is your target point, p1 is your source point, o is the rotation origin and a is the angle