Precise subpixel line drawing algorithm (rasterization algorithm)

If you need just constant color (not interpolated by used area of pixel) then use DDA: void line_DDA_subpixel(int x0,int y0,int x1,int y1,int col) // DDA subpixel -> thick { int kx,ky,c,i,xx,yy,dx,dy; x1-=x0; kx=0; if (x1>0) kx=+1; if (x1<0) { kx=-1; x1=-x1; } x1++; y1-=y0; ky=0; if (y1>0) ky=+1; if (y1<0) { ky=-1; y1=-y1; } y1++; … Read more

find all subsets that sum to a particular value

def total_subsets_matching_sum(numbers, sum): array = [1] + [0] * (sum) for current_number in numbers: for num in xrange(sum – current_number, -1, -1): if array[num]: array[num + current_number] += array[num] return array[sum] assert(total_subsets_matching_sum(range(1, 10), 9) == 8) assert(total_subsets_matching_sum({1, 3, 2, 5, 4, 9}, 9) == 4) Explanation This is one of the classic problems. The idea … Read more

Non-recursive depth first search algorithm [closed]

DFS: list nodes_to_visit = {root}; while( nodes_to_visit isn’t empty ) { currentnode = nodes_to_visit.take_first(); nodes_to_visit.prepend( currentnode.children ); //do something } BFS: list nodes_to_visit = {root}; while( nodes_to_visit isn’t empty ) { currentnode = nodes_to_visit.take_first(); nodes_to_visit.append( currentnode.children ); //do something } The symmetry of the two is quite cool. Update: As pointed out, take_first() removes and … Read more

Algorithm to fill triangle

if it helps a bit here is my ancient C++ source for triangle in VCL/GDI: //————————————————————————— class gfx_main { public: Graphics::TBitmap *bmp; int **pyx,xs,ys; gfx_main(); ~gfx_main(); void resize(int _xs=-1,int _ys=-1); void troj(int x0,int y0,int x1,int y1,int x2,int y2,int col); // this is filled triangle void _troj_line(int *pl,int *pr,int x0,int y0,int x1,int y1); // this is … Read more