draw algorithm