Make the test rect go to xMax, yMax not xMax-1,yMax-1

I don't understand why the -1 is there and removing
it actually fixes bug #76387
Reorder the if check condition

I think the previous condition was correct anyway because of the extra -1 in the i check
but it really makes more sense to have the i check first

Bug #76478