Вы можете удалить 2 цикла из этого алгоритма
Алго
Pmin = starting point of drawing graph line
pmax = ending point of drawing graph line
THpixel = Total Horizontal pixel;
TVpixel = Total Vertical pixel;
псевдокод
loop hpixel <= THpixel
{
// use this method if there is a way to draw this without walking through Box pixels
// the walking will bring complexity of algorithm again to O(n)
drawBox(THpixel,TVpixel,backgroungColor);// this is drawing box with background color
// if there is no way to draw Box, you can remember the last data of graph and when
//you want to refresh just redraw last graph with background color and draw new one with graph color, so the first step(when 1st time drawing) will took O(mxn) time and the following steps will took O(n) time
//drawing min to max line
loop min < Vpixel < pmax
{
draw Vpixel with graph color
Vpixel = Vpixel + 1;
}
hpixel = hpixel + 1;
}
его сложность будет O (nx (pmax-pmin)) = O (N);