Calculating definite integral with absolute value I need to evaluate, with $a,c>0$, the integral:
$$\int_{-a}^a\left(1-\frac{|x-y|}c\right)\,dy$$
This is what i tried
if $x>y:$
$$\int_{-a}^a\left(1+\frac{y-x}c\right)\,dy=2 a-\frac{2 a x}{c}$$
if $x<y:$
$$\int_{-a}^a\left(1+\frac{x-y}c\right)\,dy=2 a+\frac{2 a x}{c}$$
is this right? Can I combine the two?
 A: For $-a\le x\le a$ and $y\in[-a,a]$, we can write
$$|x-y|=\begin{cases}x-y&, x>y\\\\y-x&,x<y\end{cases}$$
Thus, we have
$$\int_{-a}^a \left(1-\frac{|x-y|}{c}\right)\,dy=\int_{-a}^x \left(1-\frac{x-y}{c}\right)\,dy+\int_{x}^a \left(1-\frac{y-x}{c}\right)\,dy\tag1$$

For $x>a$ and $y\in[-a,a]$, $|x-y|=x-y$ and we have
$$\int_{-a}^a \left(1-\frac{|x-y|}{c}\right)\,dy=\int_{-a}^a \left(1-\frac{x-y}{c}\right)\,dy\tag2$$
while for $x<-a$ and $y\in[-a,a]$, $|x-y|=y-x$ and we have
$$\int_{-a}^a \left(1-\frac{|x-y|}{c}\right)\,dy=\int_{-a}^a \left(1-\frac{y-x}{c}\right)\,dy\tag2$$
A: For $x \in (-a,a) $
$$I \equiv \int_{-a}^a\left(1-\frac{|x-y|}c\right)\,dy \\ = \frac 1c\int_{-a}^x\left( c-x+y \right)\,dy
\\ + \frac 1c\int_{x}^a\left( c+x-y \right)\,dy
\\ = \frac 1c  \left[  (c-x)(x+a)+(c+x)(a-x) \right]
\\ =  \frac 1c  (-2x^2+2ac) =2a-2\frac{x^2}{c}  $$
for $x<-a$
$$I = 2a+2\frac{ax}c  $$
for $x>a$
$$I = 2a-2\frac{ax}c  $$
A: $\newcommand{\bbx}[1]{\,\bbox[15px,border:1px groove navy]{\displaystyle{#1}}\,}
 \newcommand{\braces}[1]{\left\lbrace\,{#1}\,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\,{#1}\,\right\rbrack}
 \newcommand{\dd}{\mathrm{d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\expo}[1]{\,\mathrm{e}^{#1}\,}
 \newcommand{\ic}{\mathrm{i}}
 \newcommand{\mc}[1]{\mathcal{#1}}
 \newcommand{\mrm}[1]{\mathrm{#1}}
 \newcommand{\pars}[1]{\left(\,{#1}\,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\,{#2}\,}\,}
 \newcommand{\totald}[3][]{\frac{\mathrm{d}^{#1} #2}{\mathrm{d} #3^{#1}}}
 \newcommand{\verts}[1]{\left\vert\,{#1}\,\right\vert}$
\begin{align}
&\bbox[15px,#ffd]{\left.\int_{-a}^{a}\pars{1 - {\verts{x - y} \over c}}\dd y
\,\right\vert_{\ a,\, c\ >\ 0}}
\\[5mm] = &\
\left. y\pars{1 - {\verts{x - y} \over c}}\right\vert_{\ -a}^{\ a} +
{1 \over c}\int_{-a}^{a}y\,\mrm{sgn}\pars{y - x}\dd y
\\[5mm] = &\
a\pars{1 - {\verts{x - a} \over c}} -
a\pars{1 - {\verts{x + a} \over c}} +
\left.{y^{2} \over 2c}\mrm{sgn}\pars{y - x}\right\vert_{\ -a}^{\ a} \\[3mm] - &\
{1 \over c}\int_{-a}^{a}y^{2}\,\delta\pars{y - x}\dd y
\\[5mm] = &\
{a \over c}\pars{\verts{x + a} - \verts{x - a}} +
{a^{2} \over 2c}
\bracks{\mrm{sgn}\pars{x + a} - \mrm{sgn}\pars{x - a}}
\\[3mm] & -
\bracks{\vphantom{A^{A^{A}}}\verts{x} < a}{x^{2} \over c}
\label{1}\tag{1}
\end{align}

Note that
\begin{align}
\verts{x + a} - \verts{x - a}
& \,\,\,\stackrel{a\ >\ 0}{=}\,\,\,
\left\{\begin{array}{rcl}
\ds{-2a} & \mbox{if} & \ds{x \leq -a}
\\
\ds{2x} & \mbox{if} & \ds{-a < x < a}
\\
\ds{2a} & \mbox{if} & \ds{x \geq a}
\end{array}\right.
\\[3mm] & =
2\braces{\bracks{\vphantom{A^{A}}\verts{x} < a}x +
\bracks{\vphantom{A^{A}}\verts{x} \geq a}\mrm{sgn}\pars{x}a}
\\[1cm]
\mbox{and}\quad
\mrm{sgn}\pars{x + a} - \mrm{sgn}\pars{x - a}
& \,\,\,\stackrel{a\ >\ 0}{=}\,\,\,
\left\{\begin{array}{rcl}
\ds{0} & \mbox{if} & \ds{x < -a}
\\
\ds{2} & \mbox{if} & \ds{-a < x < a}
\\
\ds{0} & \mbox{if} & \ds{x > a}
\end{array}\right.
\\[3mm] & = \bracks{\verts{x} < a}2
\end{align}
A: You can write an explicit antiderivative, using
$$\frac d{dy}(|y-x|(y-x))=\text{sgn}(y-x)(y-x)+|y-x|=2|y-x|.$$
Then
$$2I=2\int_{-a}^a|y-x|\,dy=|a-x|(a-x)+|a+x|(a+x).$$

The integral curve has three regimes:
$$\begin{cases}x\le-a&\to-2ax,
\\-a\le x\le a&\to x^2+a^2,
\\a\le x&\to2ax.\end{cases}$$
These sections join with continuous derivative at $x=\pm a$.

(Caution, I simplified the integrand.)
