I'm trying to extract the angle $[0 ... 2\pi]$ from a $3\times3$ homogeneous 2D transformation matrix. In fact I've found 2 very helpful posts at stackoverflow and stackexchange. $$A= \begin{pmatrix} a&b&tx\\ c&d&ty\\ 0&0&1 \end{pmatrix}$$ $$\mathrm{angle} = \arctan(c/d)$$
The thing is that I would like the angle to be in the range $[0 ... 2\pi]$. This means that the angle needs to be adjusted somewhat. I've discovered a pattern that seems to be working and I wonder if it makes any sense or if there is a more clever way to do this.
if scale x is flipped (or scale y, seems not to matter) and (-PI/2 < angle < PI/2)
angle = angle + PI
elseif angle < 0
angle = angle + 2PI
elseif angle > 0
angle = angle // no change