A JavaScript Algorithm to compute intersection of squares in reverse order