There are two ways to have self-crossing:
- vertical line and horizontal line touch each other
- vertical line and vertical line touch each other
When current direction is north or south (horizontal line created), check whether this horizontal line touches the most recent two vertical lines created.
When current direction is west or east (vertical line created), check whether this vertical line touches the most recent two horizontal and two vertical lines created.
Of course, most recent two lines exclude the vertical or horizontal line where current line starts. Otherwise we instantly have self-crossing.
Since we only keep track of the most recent two horizontal lines and vertical lines, the space we need is constant.