He utilizado una estrategia similar a Karpathy hasta allí, pero utiliza una función extra. Como puede ver, estoy usando cvClipLine para recortar la línea al tamaño de la imagen, lo cual es innecesario pero agrega un poco de amabilidad.
También el multiplicador aquí se define como theMult = max (img-> height, img-> width) por lo que no obtenemos números que un día podrían desbordarse o algo así.
void drawLine(IplImage * img, float line[4], int thickness,CvScalar color)
{
double theMult = max(img->height,img->width);
// calculate start point
CvPoint startPoint;
startPoint.x = line[2]- theMult*line[0];// x0
startPoint.y = line[3] - theMult*line[1];// y0
// calculate end point
CvPoint endPoint;
endPoint.x = line[2]+ theMult*line[0];//x[1]
endPoint.y = line[3] + theMult*line[1];//y[1]
// draw overlay of bottom lines on image
cvClipLine(cvGetSize(img), &startPoint, &endPoint);
cvLine(img, startPoint, endPoint, color, thickness, 8, 0);
}