Created
June 10, 2014 14:24
-
-
Save feltnerm/258287d5abbb6e460039 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Utilities | |
*/ | |
function now(){ | |
return performance && | |
performance.now ? function now() { | |
return performance.now() | |
} : Date.now || function now() { | |
return +new Date | |
} | |
} | |
// http://stackoverflow.com/questions/23095637/how-do-you-do-a-random-rbg-in-javascript | |
function getRandomRgb() { | |
var num = Math.round(0xffffff * Math.random()); | |
var r = num >> 16; | |
var g = num >> 8 & 255; | |
var b = num & 255; | |
return [r,g,b]; | |
} | |
// ======================================== | |
/** | |
* Pixel | |
* | |
* @param r {String|int} red | |
* @param g {String|int} green | |
* @param b {String|int} blue | |
* @param a {String|int} alpha channel | |
* | |
* @returns {Array} Pixel of [r,g,b,a] | |
*/ | |
function Pixel(r, g, b, a){ | |
return [r|0,g|0,b|0,a|0]; | |
} | |
/** | |
* Image | |
* | |
* @param x {String|int} Length | |
* @param y {String|int} Width | |
* | |
* @returns {Array} An 2d image array | |
*/ | |
function Image(x, y){ | |
var image = []; | |
for (var i = 0; i < x; i++){ | |
image[i] = []; | |
for (var j = 0; j < y; j++){ | |
var rgb = getRandomRgb(); | |
image[i][j] = Pixel(rgb[0],rgb[1],rgb[2],255); | |
} | |
} | |
return image; | |
} | |
// ======================================== | |
/** | |
* Algorithms | |
*/ | |
// naive as fuck | |
function naive_equals(image1, image2){ | |
// assuming this is a parallelogram. | |
var x0_length = image1.length, | |
y0_length = image1[0].length, | |
x1_length = image2.length, | |
y1_length = image2[0].length; | |
var result = true; | |
for (var i = 0; i < x0_length; i++){ | |
for (var j = 0; j < y0_length; j++){ | |
return image1[i][j] == image2[i][j] && true; | |
} | |
} | |
} | |
// ======================================== | |
// Go-go-gadget image duplicate detector! | |
function __main__(equals){ | |
var image1 = Image(100,100), | |
image2 = Image(100,100); | |
var start = now()(), | |
end; | |
var result = equals(image1, image1); | |
end = now()(); | |
console.log('========================================'); | |
console.log('equals: ' + result); | |
console.log('elapsed (ms): ' + parseFloat((end - start))); | |
} | |
__main__(naive_equals); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment