Continuous random variables

This is purely for revision purposes, i have a test tomorrow.

We will have a question asking how to mathmatically show a probability density function of a simple distribution. I can do integration, but i really dont understand how to convert a diagram into an integral of mulitple parts.

For example diagrams with the following

(upward sloping) 0 < x < 2, (downward sloping) 2 < x < 4 (mountain shape!)

or

(downward sloping) 0 < x < 2, (upward sloping) 2 < x < 4 ( 'V' Shape!)

I know it has to do with the straight line equation (f (x) = a + bx )

But im just stumped on how it is figured out..

Any help appreciated!