This looks like homework and many people uploading homework problems edit them away after they've been answered. We will be sure to not to do majority of work (so you won't be cheating) if you promise not to delete the question after we help you.
Hint: Basically you might do something like
if x+deltaX > xMax
x = xMax - (x + deltaX - xMax); % Reflect about xMax
Best Answer