How to derive the marginal distribution based on a join distribution of X and Y? I am doing an exercise, and I have the solution for the exercise but don't know how to derive the solution. 
The exercise is in below

Two random variable X and Y are uniformly distributed in a square as
  shown in the figure below, find the marginal distribution $p_{X}(x)$
  and $p_{Y}(y)$.


The solution of this exercise is the following figure. It looks not very complicate but I don't have idea about how to get to there. Could anybody give a help? Thanks a lot!

 A: The fact that's it's uniformly distributed on this square says that a subset $S$ with area $A$, occurs with probability:
$P(S)= \frac{A}{T}$, where $T$ is the area of the whole square. Alternatively this means that the density function is:
$P(S)= f_{X,Y}(x,y)= \begin{cases} \frac{1}{T}  & ;(x,y)\in D\\
0 & ; (x,y) \notin D
\end{cases}$
Where $D$ is the given square. The probability of a subset $S$ in  $\mathbb{R}^2$ is:
$\int_{\mathbb{R}^2}f_{X,Y}(x,y)\cdot 1_{S}(x,y)dxdy= \int_{S}f_{X,Y}(x,y)dx dy$
Then the probability that $y\in E\subseteq \mathbb {R}$ is (By fubini):
$P(y\in E)= P\Big( (x,y)\in \mathbb{R} \times E \Big)=\int_{\mathbb{R}\times E}f_{X,Y}(x,y)=\int_{\mathbb{R}} \int_{E}f_{X,Y}(x,y)$
In particular for $E= (-\infty,t]$, we have:
$P(y\leq t)= \int_{\mathbb{R}} \int_ {-\infty}^t f_{X,Y}(x,y)dydx=\int_{-2}^0 \int_ {\max \{ t,-x-2 \} }^{\min\{t, x+2 \}} \frac{1}{T}dy dx+ \int_{-2}^0 \int_ {\max \{ t,x-2 \} }^{\min\{t, -x+2 \}} \frac{1}{T}dy dx$
Simlarly:
$P(x\leq t)= \int_{\mathbb{R}} \int_ {-\infty}^t f_{X,Y}(x,y)dxdy=\int_{-2}^0 \int_ {\max \{ t,-y-2 \} }^{\min\{t, y+2 \}} \frac{1}{T}dx dy+ \int_{-2}^0 \int_ {\max \{ t,y-2 \} }^{\min\{t, -y+2 \}} \frac{1}{T}dx dy$
Also notice that $T=4$, and that $\int_t^t c=0$.
This may be a tedious explanation, but the point is to use the fact that you can rewrite the double integral into an integral depending on a single desired variable, which will work on other such problems given the joint distribution.
