Two-dimensional rotations come up often in digital image production and game programming. As objects move around in a virtual world they can both translate, and rotate. In two dimensions, a rotation is defined by an axis, the point about which the rotation takes place, and the angle of rotation. To rotate a triangle, you rotate the three vertices that define the triangle.

## Triangle in the Grid

Points on a two-dimensional grid are written (x, y). A triangle has three such points, one defining each vertex. For instance, we could label these p1 = (x1, y1), p2 = (x2, y2) and p3 = (x3, y3).

## Rotations around the Origin

If you rotate a point p by an angle θ, you get a new point p’ = (x’, y’) where x’ = x∙cos(θ) - y∙sin(θ) and y’ = y∙cos(θ) + x∙sin(θ). To rotate a triangle, you apply this process to p1, p2 and p3.

## Rotations around an Arbitrary Axis

To rotate around an arbitrary axis, you translate the axis to the origin, taking the point p with it, perform the rotation as above, then undo the translation. Let P = (X, Y) be the axis. Translate both the axis and the point to the origin by subtracting P. This gives a new axis, P* = P - P = (0, 0), and a new point p* = (x*, y*) = p-P = (x - X, y - Y). Perform the rotation as usual on p. Finally, undo the initial translation by adding P to get x’ = X + (x - X)∙cos(θ) - (y - Y)∙sin(θ) and y’ = Y + (y - Y)∙cos(θ) + (x - X)∙sin(θ). Repeat this process for each point.

## Example

Let a triangle be defined by the points p1 = (1,1), p2 = (2,3) and p3 = (4,2). Rotate the triangle around the point P = (3,-1) by 23 degrees. Using the formula, p1 - P = (-2,2) and p1’_x = 3 + (-2)∙cos(23) - 2∙sin(23) = 0.3775, and p1’_y = -1 + 2∙cos(23) + (-2)∙sin(23) = 0.0595. Transforming the other points in a similar way, we have the new points: p1’ = (0.3775, 0.0595), p2’ = (0.5166, 2.2913) and p3’ = (2.7483, 2.1522).