# Calculate the overlapping line

(l, r) defines a line whose left end is at l and the right end is at r, on a 1-dimensional space.

Given 2 lines b = (0, bz) and f = (i, i + fz), v = (l, r) is the overlapping part of these lines. When b and f do not overlap (when v cannot have a positive length), v = (0, 0).

(input)   -> (output)
bz, fz, i -> l, r


The data type (int, float, string, etc.) for each value should be able to represent all integers from -128 to 127, inclusive.

You can change the order of input and output variables.

### Examples

0, 0, 0 -> 0, 0
1, 1, 1 -> 0, 0
1, 2, 3 -> 0, 0
1, 2, -3 -> 0, 0
2, 1, 1 -> 1, 2
4, 2, 1 -> 1, 3
2, 2, -1 -> 0, 1
2, 4, -1 -> 0, 2

• @KevinCruijssen You can change the order of the variables. Commented Mar 28, 2022 at 15:51
• @xiver77 why don't 1, 1, 1 overlap at point 1? afaict, the two segments are 0 to 1 and 1 to 2. am i missing something? Commented Mar 28, 2022 at 17:20
• May we take b's start point (0) as a fourth input? Commented Mar 28, 2022 at 17:24
• @xiver77 You define a "line" and then say "Given 2 lines ..." but you seem to then go on to define a different input spec from just taking these "lines" - we must take (bz, fz, i) and not (0, bz) and (i, i+fz)) - this seems really weird, perhaps there is some justification but I don't get it. Commented Mar 28, 2022 at 17:53
• In the comments you've said "Yes, because those are sizes" but the post says all inputs need to be able to take negative values - which is it? Commented Mar 28, 2022 at 18:15

# Python 3.8 (pre-release), 44 bytes

lambda a,b,c:[c*(a>c>0),min(a,b+c)*(a>c>-b)]


Try it online!

Test harness borrowed from @ophact.

The condition for overlap to exist is that both right ends are right of both left ends. Two of those four pairwise inequalities are as I understand it satisfied automatically (up to equality, but that does no harm), the other two can be folded into a single chained comparison a>c>-b. This is used to zero out the result we computed under the assumption that overlap exists. The left end makes a further shortcut using that b is guaranteed to be nonnegative and that one of the two possible ends is 0.

# Python 3.8 (pre-release), 52 bytes

lambda a,b,c:[d:=c*(c>0),e:=min(a,c+b)]*(d<e)or[0,0]


Try it online!

## Explanation

The overlapping line starts from the beginning of the second line (measured by starting point) because it cannot start from the beginning of the first line (unless both lines start at the same point) because then the beginning would not be part of the second line, and it cannot start at the end of any line because it would leave the line directly afterward.

It ends at the first endpoint, where it first "leaves" a line.

If the start is not less than the end (as found according to the above), then the lines do not overlap, and we can then return [0, 0].

# 05AB1E, 14 bytes

Ý+IÝÃÁ2£DËi00S


I/O: three loose inputs in the order $$\fz,i,bz\$$; output as pair [$$\r,l\$$].

Explanation:

Step 1: Calculate [r,l] for overlapping test cases:

Ý        # Push a list in the range [0, first (implicit) input fz]
+       # Add the second (implicit) input i to each to make the range [i,fz+i]
I      # Push the third input bz
Ý     # Pop and push a list in the range [0,bz]
Ã    # Keep all values in [i,fz+i] that are also in [0,bz]
Á   # Rotate this list once towards the right
2£ # Keep (up to) the first two values
# (this is the output [r,l] for the overlapping test cases)


Step 2: Deal with the edge cases of [] (no overlap) or [x] ($$\i=bz\$$ or $$\fz+i=0\$$):

D        # Duplicate the list
Ëi      # If both values are the same (truthy for [] and [x], falsey for [r,l]):
# (after which the result is output implicitly)


# Charcoal, 27 bytes

Ｎθ≔Φ⁺…⁰ＮＮ⁼ι﹪ιθηＩ⟦∨⌊η⁰⊕∨⌈η±¹


Try it online! Link is to verbose version of code. Explanation:

Ｎθ


Input bz.

≔Φ⁺…⁰ＮＮ⁼ι﹪ιθη


Input fz and i, create the range [i,i+fz), and filter out values not in the range [0,bz).

Ｉ⟦∨⌊η⁰⊕∨⌈η±¹


If there are no values left then output 0,0 otherwise output the endpoints of the remaining values.

# Jelly, 12 bytes

Ż+⁵fŻ}0ṚE?.ị


A full program that accepts fz bz i and prints the line as [l, r].

Try it online!

### How?

Ż+⁵fŻ}0ṚE?.ị - Main Link: fz, bz
Ż            - zero range fz -> [0,1,2,...,fz] (call this F)
⁵          - 3rd argument, i
}       - use right argument, bz:
Ż        -   zero range  -> [0,1,2,...,bz] (call this B)
f         - F filter keep if in B
?   - if...
E    - ...condition: all equal? (i.e. is that a single point)
0      - ...then: yield zero
Ṛ     - ...else: reverse
.  - 0.5
ị - index into -> [last value, first value] (when given 0 this is [0,0])
- implicit print

• Can't you remove the R and output in the order [r,l]? Or is an else-statement required for ?. Commented Mar 29, 2022 at 7:11
• The ? requires both branches; I couldn't think of any way to get less than 0ṚE?.ị so went with this as it maintains that (l,r) order. The main problem is that it needs to handle [0] so using ị ("repeat") as an "if" like ¬E¡.ị is out (e.g. lines (0,5) and (-5,0) would lead to (1,1) rather than (0,0)). There may be a save to find here though. Commented Mar 29, 2022 at 8:49

# Wolfram Language (Mathematica), 59 bytes

(IntervalIntersection@@Interval/@{{0,#1},{#3,#3+#2}})[[1]]&


Try it online!