so what is this:

got ranges A and B
upper = clamp(B.upper, A);
offset = upper - B.upper;
lower = clamp(B.lower + offset, A);
return {lower, upper};

Follow

now if you have been programming for a while you might have noticed that comparison is just a special case of subtraction. We have a subtraction here and clamp has got to have a comparison with sameish parameters in it, so lets expand it and see if we can notice any patterns:

upper =
B.upper < A.lower ? A.lower :
A.upper < B.upper ? A.upper :
B.upper;
offset = upper - B.upper;
lower = clamp(B.lower + offset, A);
return {lower, upper};

We have 3 cases, let's consider what upper and hence offset would be in each

B.upper < A.lower ? A.lower
offset = A.lower - B.upper
A.upper < B.upper ? A.upper
offset = A.upper - B.upper
otherwise B.upper
offset = B.upper - B.upper

the parameters of comparison and subtraction are the same, and in the last case subtraction is unnecessary, it's just 0, so we can do the subtractions upfront and then compare the results

offset_lower = A.lower - B.upper;
offset_upper = A.upper - B.upper;
offset =
offset_lower > 0 ? offset_lower :
offset_upper < 0 ? offset_upper :
0;
upper = B.upper + offset;
lower = B.lower + offset;
lower = clamp(lower, A);
return {lower, upper};

ok, does it look like something now?
no, what a waste of time and effort

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.