`cos(rotation)` and `sin(rotation)` are constant across the extent so you don't have to compute them `height * width` times. You should pull `granularityY * cos(rotation)` and `granularityX * sin(rotation)` out of the loops and store them as constants. The common case will be that `sin` and `cos` evaluate to `1.0`.