Turning a Sketch About a Pin

complex numbers · rotation · multiplication · Euler's formula · translation

A drawing app stores every point of a sketch as a complex number: x + y*1j is the point (x, y). To turn the sketch, the user sticks a pin into the canvas at the point centre and drags it round by angle radians, anticlockwise for a positive angle and clockwise for a negative one.

Multiplication does the turning. The unit arrow e^{i\varphi} = \cos\varphi + i\sin\varphi (cmath.exp(1j * phi)) has length 1, and multiplying any complex number z by it keeps |z| and adds \varphi to the angle of z: it rotates z about the origin by \varphi. For instance (2 + 1i)\cdot i = -1 + 2i, a quarter turn anticlockwise. (Later in this path, multiplying an amplitude by such a unit factor will be called a phase change.)

Write rotate_about(points, centre, angle) that returns the list of the points after the sketch is turned by angle about centre, in the same order.

Examples

Input:  points = [2, 1+1j, 1], centre = 1, angle = 1.5707963267948966
Output: [(1+1j), 0j, (1+0j)]
Explanation: a quarter turn anticlockwise about the point 1. The point 2 is one unit to the right
of the pin and ends one unit above it, at 1+1j. The pin itself does not move.

Input:  points = [3+1j, 1+2j], centre = 0, angle = -1.5707963267948966
Output: [(1-3j), (2-1j)]
Explanation: a quarter turn clockwise about the origin.

Values like 6.1e-17 where the exact answer is 0 count as correct.

Constraints

  • answers are compared with a tolerance of 1e-6; do not round

Goals

  • Rotate a point about the origin by multiplying with a unit arrow
  • Rotate about another centre by shifting there and back
  • Read a negative angle as a clockwise turn
Starting Python…