clamp to beginning or end depending on relative position

Signed-off-by: Tomas Neme <lacrymology@gmail.com>