Consider the following:
An object of length L is placed in a space containing two alternating types of region with fixed lengths, region A of width $a$, and region B of width $b$. Axis are fixed so that the start of a region A is placed at the origin and the object is placed so that it starts some distance $D\in[0,a+b]$ from the origin.
(As shown in the figure)
Is there a way to express the amount of space the object occupies in a region of type A as a function a function of $L,D,a$ and $b$?

There may be an easier way to do this, but here's one way.
Consider the contribution from five parts:
Let's tackle the top two in the list above.
We know that $D \leq a+b$. If $D=0$, then the contribution is $a$, and goes down linearly to $0$ as $D$ goes to $a$. So, the contribution is $a(1-(D/a))$ up to $D=a$.
Even though the contribution stops at $D=a$, that function doesn't. We'd like to "turn it off" after $D=a$. Or, put another way, we want to "turn on" a function at $D=a$ that subtracts out the contribution.
We can use the Heaviside step function $H(x)$ to do this. We want to add a contribution $a(D-a)/a = D-a$, but only if $D \geq a$. We do this by multiplying it by the Heaviside function at $x=a$, which "turns it on" there.
So, for the first two blocks $A/B$, the contribution is
$$C_{1,2} = a(1-(D/a)) + H(a)(D-a).$$
For the third region, we'll need to figure out the number of pairs of blocks $A/B$ that the bar covers completely. For this we can use the floor function to force a continuous function to take on an integer value.
We subtract out the part we just dealt with, and then use the floor function on the rest:
$$C_3 = a\lfloor [L-(a+b-D)]/(a+b) \rfloor$$
Now we're left with whatever is hanging on the right side.
The left edge of the rightmost $A/B$ block is at $x=(a+b)\lfloor(D+L)/(a+b)\rfloor$.
The contribution is linear up to $x+a$, and stops after that.
So, finally,
$$C_{4,5} = D+L-x - H(x+a)[D+L-x-a].$$
Add these up, substitute $x$ back in as a function of $D,L,a,b$, and you have the answer.