Probability of two overlapping bars in given regions I have a question regarding the problem outlined in the picture. Two bars of different lengths x_1 and x_2 can be located anywhere within their associated intervals (B_1 - B_2) and (A_1 - A_2) (uniform probability). What is the probability that they overlap?
I tried to solve this problem similar to the problem of two people meeting (Probability of two people meeting). However, I had to modify this classical problem for different waiting times and different time windows. I tried to solve this geometrically, as you can see in the picture. The solution would be to divide they green area by the entire area left blank. However, I am not sure about my solution. I crossed out in red the regions that are impossible due to the constraints given by the allowed intervals of the bars. But are these constraints really correctly drawn the way I did it?
Could anybody please help me here?
Thank you! :)

Perhaps this integral can help:
$\frac{1}{(A_2-A_1-a)(B_2-B_1-b)}\int_{A_1}^{A_2-a}\int_{B_1}^{B_2-b}(x<y+b)(y<x+a)dydx$
or using the signum function:
$\frac{1}{4(A_2-A_1-a)(B_2-B_1-b)}\int_{A_1}^{A_2-a}\int_{B_1}^{B_2-b}(sgn(y+b-x)+1)(sgn(x+a-y)+1)dydx$
Where $a$ and $b$ are the segment lengths. $x$ and $y$ are the left edges of the segments. Two segments on the same line overlap iff the left edge of each is less than the right edge of the other.
The region of interest is the intersection of two regions:

*

*A box bounded by $A_1<x<A_2 - a$ and $B_1<y<B_2-b$

*The region between two parallel lines: $y>x-b$ and $y<x+a$
In R:
library(data.table)
library(ggplot2)
library(pracma)

pOverlap <- function(a, b, a1, a2, b1, b2) {
  if (a == a2 - a1) {
    if (b == b2 - b1) {
      (a1 < b2)*(b1 < a2)
    } else {
      integrate(function(y) (a1 < y + b)*(y < a2), b1, b2 - b)$value/(b2 - b1 - b)
    }
  } else if (b == b2 - b1) {
    integrate(function(x) (x < b2)*(b1 < x + a), a1, a2 - a)$value/(a2 - a1 - a)
  } else {
    integral2(function(x, y) (x < y + b)*(y < x + a), a1, a2 - a, b1, b2 - b)$Q/(a2 - a1 - a)/(b2 - b1 - b)
  }
}

rplot <- function(seed) {
  set.seed(seed)
  A <- sort(sample(0:20, 2))
  B <- sort(sample(0:20, 2))
  a <- sample(1:diff(A), 1)
  b <- sample(1:diff(B), 1)
  
  dfBox <- data.frame(x = c(A[1], A[2] - a, A[2] - a, A[1], A[1]), y = rep(c(B[1], B[2] - b), length.out = 5))
  dt <- data.table(x = seq(0, 20, length.out = 101))
  dt[, `:=`(y1 = x - b, y2 = x + a, ymin = pmax(x - b, B[1]), ymax = pmin(x + a, B[2] - b))]
  
  suppressWarnings(
    print(
      ggplot() +
        geom_line(data = dt, aes(x = x, y = y1), color = "red") +
        geom_line(data = dt, aes(x = x, y = y2), color = "red") +
        geom_ribbon(data = dt[ymin <= B[2] - b & ymax >= B[1] & x >= A[1] & x <= A[2] - a], aes(x = x, ymin = ymin, ymax = ymax), fill = "gray40") +
        geom_rect(data = dfBox, mapping = aes(xmin = A[1], xmax = A[2] - a, ymin = B[1], ymax = B[2] - b, fill = FALSE), color = "blue", alpha = 0) +
        scale_x_continuous(limits = c(0, 20), breaks = seq(0, 20, 4)) +
        scale_y_continuous(limits = c(0, 20), breaks = seq(0, 20, 4)) +
        theme(legend.position="none")
    )
  )
  
  
  list(Segments = c(A1 = A[1], A2 = A[2], B1 = B[1], B2 = B[2], a = a, b = b), p = pOverlap(a, b, A[1], A[2], B[1], B[2]))
}

rplot(12)
#> $Segments
#> A1 A2 B1 B2  a  b 
#>  1 15  4 13  5  2 
#> 
#> $p
#> [1] 0.6743923

# rplot(2) # bottom right corner chopped off
# rplot(3) # part of a horizontal line (B1 = B2 - b)
# rplot(4) # no overlap possible
# rplot(11) # always overlapping
# rplot(12) # top left and bottom right corner chopped off


