\documentclass[12pt]{article}
% version 1.3
\usepackage{amsmath,amssymb}
\usepackage{graphicx}

%% for metapost graphics
\usepackage{emp,ifpdf}
\ifpdf
\DeclareGraphicsRule{*}{mps}{*}{}
\fi
\setlength{\unitlength}{1cm}

\empaddtoprelude{input mp-tool}
\empTeX{}

\begin{document}

\setlength{\unitlength}{1cm}
\begin{empfile}[dave]

\begin{empdef}[evalf](4,4)

def mygridcolor=0.65white enddef;

% inititialize numerator and denominator of the slope
numeric a, b; 
a=3;
b=1;

% initialize the given point
numeric c, d;
c=2;
d=-1;

% initialize function label abcissa
numeric flabel;
flabel=0.666666666666667;

% define linear function
vardef f(expr x)=
	d+((a*(x-c))/b)
enddef;

% draw line with given point and slope
path L;
L:=(-5,f(-5));
for x=-5 step 1 until 5:
	L:=L--(x,f(x));
endfor;

% initialize scale
numeric u; 10u=w;

% the line
L:=L scaled 1u;
draw L withcolor blue;

% clipping path
path cpath;
cpath:=(-5,-5)--(5,-5)--(5,5)--(-5,5)--cycle;
cpath:=cpath scaled 1u;

% clip and save current picture
picture pic;
clip currentpicture to cpath;
pic:=currentpicture;

% erase currentpicture
currentpicture:=nullpicture;

% draw grid 
for k=-5u step 1u until 5u:
	draw (-5u,k)--(5u,k) withcolor mygridcolor;
	draw (k,-5u)--(k,5u) withcolor mygridcolor;
endfor;

% draw axes
drawarrow (-5u,0)--(5u,0);
drawarrow (0,-5u)--(0,5u);

% label axes
label.rt(btex $x$ etex, (5.2u,0));
label.top(btex $y$ etex, (0,5.2.u));
label.bot(btex $5$ etex, (5u,0));
label.lft(btex $5$ etex, (0,5u));

% redraw line
draw pic;

% label the function f
pair D;
D:=(0.666666666666667,f(0.666666666666667)) scaled u;
label.bot(btex $f$ etex,D);

\end{empdef}

\end{empfile}

\immediate\write18{mpost  dave}

\begin{center}
\empuse{evalf}
\end{center}


\end{document}
