Rotating a point around a center point algorithm

Hi, Im looking for an algorithm that can rotate a pixel, ie move a pixel from x,y to newx,newy based on a given angle(most likely not 0,90,180,270,360) I have seen it somewhere i just cant remember the math behind it.

surely you mean trigonometry?

newX=x+Math.cos(rad)*distance
newY=y+Math.sin(rad)*distance

or around a given origin (centre point)

distance=Math.sqrt((x-originX)(x-originX)+(y-originY)(y-originY));
startAngle=Math.atan2(x-originX,y-originY);
endAngle=startAngle+rad;
newX=originX+Math.cos(endAngle)*distance
newY=originy+Math.sin(endAngle)*distance

Note that this is untested and from memory of math from school over 14 years ago so it may be incorrect :stuck_out_tongue:

Or like this:
To rotate a point ‘p’ around another point/origin ‘c’ simply subtract c from p, then rotate p and add c.

p.x -= c.x
p.y -= c.y

newx = p.x * cos(angle) - p.y * sin(angle)
newy = p.x * sin(angle) + p.y * cos(angle)

p.x = newx + c.x
p.y = newy + c.y

Exactly what i was looking for thanks! this will really help me with my 3d engine.

How would I apply this to three d? Say I have point xyz wanting to rotated around point pxpypz?

Exactly the same principle; you just rotate around an axis of your choosing, rather than a point.

www.rel.phatcode.net/mytutes/3dtutes/chapter2/chapter2.htm

But I’d rather you use matrices:

www.rel.phatcode.net/mytutes/3dtutes/chapter4/chapter4.htm

Here is an example of a rotation matrix. It can be used to rotate a vector around any arbitary axis.
http://pastebin.com/Gq4mHL82

Keep things simple. A rotation is one way to describe how one choice of a coordinate frame is related to some other. In the case of a rotation and matrices, then the matrix describes how the orthonormal basis (the X,Y & Z axis directions) change…which is a parallel projection…so in the 3D, 3x3 matrix form each row (or column depending on choice of convention) is ‘dotted’ with the input to project from one to the other. So each source direction is dotted with the target direction (you so have 3 equations) to get the output. Humm…that was probably as clear as mud. :slight_smile: