3 ms·
A complex number represents a 2D point in the complex plane, but multiplication by a complex number on the unit circle also happens to correspond to a rotation
by tomstuart 4y ago
A complex number represents a 2D point in the complex plane, but multiplication by a complex number on the unit circle also happens to correspond to a rotation in the complex plane, e.g. 3 × i = 3i, which is a rotation of 90 degrees anticlockwise, or -5i × -i = -5, which is a rotation of 90 degrees clockwise.
So if you use the complex numbers 1, i and -i to represent the actions “go straight”, “turn left” and “turn right” respectively, you can update a complex number representing the current heading of the snake (1 = east, i = north, -1 = west, -i = south) by multiplying it by the action. To get the snake’s current position, add up all the “current heading” numbers you’ve seen so far.