User Tools

Site Tools


algorithms

Algorithms

Rectangle Intersection (rect-intersect)

http://tekpool.wordpress.com/2006/10/11/rectangle-intersection-determine-if-two-given-rectangles-intersect-each-other-or-not/

bool IntersectRect(const RECT * r1, const RECT * r2)
{
    return ! (
           r2->left > r1->right ||
           r2->right < r1->left ||
           r2->top > r1->bottom ||
           r2->bottom < r1->top
    );
}

Time-Difference in days, hours, minutes and seconds

Example: 105 days 17h 27m 46s

$up = time() - $starttime;
$s = $up % 60;
$m = ($up % 3600) / 60;
$h = ($up / 3600) % 24;
$d = ($up / 3600) / 24;
 
printf('%d days %02dh %02dm %02ds', $d, $h, $m, $s);

Array permute

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/permute [rev. #1]
permute = function(v, m){
    for(var p = -1, j, k, f, r, l = v.length, q = 1, i = l + 1; --i; q *= i);
    for(x = [new Array(l), new Array(l), new Array(l), new Array(l)], j = q, k = l + 1, i = -1;
        ++i < l; x[2][i] = i, x[1][i] = x[0][i] = j /= --k);
    for(r = new Array(q); ++p < q;)
        for(r[p] = new Array(l), i = -1; ++i < l; !--x[1][i] && (x[1][i] = x[0][i],
            x[2][i] = (x[2][i] + 1) % l), r[p][i] = m ? x[3][i] : v[x[3][i]])
            for(x[3][i] = x[2][i], f = 0; !f; f = !f)
                for(j = i; j; x[3][--j] == x[2][i] && (x[3][i] = x[2][i] = (x[2][i] + 1) % l, f = 1));
    return r;
};
algorithms.txt · Last modified: 2011-11-17 19:50 CET by dominik