I think `rx` and `ry` should be switched: `i` indexes the rows which should be the `y`-direction (and similarly for `j` and `x`).