more efficient math

/!\ this is only correct if x is always comprised between 0 and width,
and y between 0 and height