Rotating coordinates around a centre

In graphical programming you might want to rotate things at some point. I am not a mathematician, so I was happy when I figured out how to do it. Here is some pseudo code which you can adapt to the programming language of your choice.

xRot = xCenter + cos(Angle) * (x - xCenter) - sin(Angle) * (y - yCenter)
yRot = yCenter + sin(Angle) * (x - xCenter) + cos(Angle) * (y - yCenter)

Example in Processing (Java needed)

xRot and yRot give the rotated point, xCenter and yCenter mark the point that you want to rotate around, x and y mark the original point. Note that in most programming languages you will need to give Angle in radian, so multiply your degree value with Pi devided by 180 (Angle = Degree * PI / 180). Have fun spinning things around!

2 Replies to “Rotating coordinates around a centre”

  1. thanks, this works a threat. heres how i did it in java :

    public void rotatefront1(float a)
    {
    a = (float)Math.toRadians((double)a);
    double xpoint = x1; //original point
    double ypoint = y1; //original point

    x1 = (int) (115 + Math.cos(a) * (xpoint – 115) – Math.sin(a) * (ypoint – 165));
    y1 = (int) (165 + Math.sin(a) * (xpoint – 115) + Math.cos(a) * (ypoint – 165));
    }

    cheers!

  2. And in R (though rotating around any point

    rotate.p<-function (X1,Y1, Px, Py, D1) ##X1=Xcoords, Y1=Ycoords, Px=Rotation pt X, Py = Rotation pt Y, D1=clockwise degrees rotation
    {
    d1=-D1*pi/180
    xRot = Px + cos(d1) * (X1 – Px) – sin(d1) * (Y1 – Py)
    yRot = Py + sin(d1) * (X1 – Px) + cos(d1) * (Y1 – Py)
    rot=cbind(xRot, yRot)
    }

Leave a Reply

Your email address will not be published. Required fields are marked *