The 0,1 & 2 conundrum!

I’m working on a little light casting algorithm for my 4k entry (yes, starting early :D).

These rays (actually just bounds of a 2d light cone [sector]) are being projected through a mesh of triangles.

When the ray hits an edge, it either drops a clip vertex & moves along the edge, or it passes into the next triangle.
This is all done through a neat recursive algorithm to minimize code size.

When passing into the next triangle, it passes with it knowledge (the index) of the edge from which it entered.
Thus each triangle will be just a single intersection “enters from edge0; does it intersect edge1? no. Must intersect edge2 then.”

This is an optimization over my previous O(n) intersection tests when using convex polygons; though the reasons for my optimization are code size motivated, not performance.

Anyhow, this is all entirely besides the point.
My question is quite trivial really; I’m struggling to think of a ‘code size optimal’ way of alternating between the values 0, 1 & 2 using just 2 operations.
e.g.

If:
entering from edge ‘0’, I want to check edge ‘1’ or ‘2’ for collision and use the other if appropriate.
entering from edge ‘1’, I want to check edge ‘0’ or ‘2’ for collision and use the other if appropriate.
entering from edge ‘2’, I want to check edge ‘0’ or ‘1’ for collision and use the other if appropriate.

Doing “(x+1)%3” twice works of course, but that’s 2*2 operations and was hoping for something a little smaller/smarter.

  • The operations could work off the initial input, or the result of the first op.
    e.g.
    input -OP1-> output1 -OP2-> output2
    or
    input -OP1-> output1
    input -OP2-> output2

  • OP1 and OP2 do not have to be the same operation (though extra points if they are!).