Created
February 21, 2017 07:22
-
-
Save razchiriac/578061065cd1deb6f6557ffe9ff4e8f7 to your computer and use it in GitHub Desktop.
JS Bin sumInPerimeter // source https://jsbin.com/tikumas
This file contains 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 name="description" content="sumInPerimeter"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
function searchForArray(haystack, needle){ | |
var i, j, current; | |
for(i = 0; i < haystack.length; ++i){ | |
if(needle.length === haystack[i].length){ | |
current = haystack[i]; | |
for(j = 0; j < needle.length && needle[j] === current[j]; ++j); | |
if(j === needle.length) | |
return i; | |
} | |
} | |
return -1; | |
} | |
getRects = (g,p) => { | |
var rects = [], i, j, k; | |
for (i = 1; i < p/2; i++) { | |
var rect = { | |
w: i, | |
h: p/2-i, | |
targets: [], | |
}; | |
for (j = 0; j <= g.length - rect.h; j++) { | |
for (k = 0; k <= g.length - rect.w; k++) { | |
rect.targets.push([j,k]); | |
} | |
} | |
rects.push(rect); | |
} | |
return rects; | |
} | |
getMaxSum = (grid,rects) => { | |
var i,j; | |
var sum = 0; | |
var maxSum = 0; | |
rects.forEach((r) => { | |
r.targets.forEach((t) => { | |
sum = 0; | |
for (i = t[0]; i < t[0]+r.h; i++) { | |
for (j = t[1]; j < t[1]+r.w; j++) { | |
sum += grid[i][j]; | |
} | |
} | |
if (sum > maxSum) maxSum = sum; | |
}) | |
}) | |
return maxSum; | |
} | |
sumInPerimeter = (grid, p) => { | |
var rects = getRects(grid, p); | |
var maxSum = getMaxSum(grid, rects); | |
console.log(maxSum); | |
return maxSum; | |
} | |
// sumInPerimeter(grid, p) | |
// given a shape return its perimeter | |
// shapes are | |
// 4-connected pixel neighborhoods | |
getPerim = (shape) => { | |
// go through each pixel in the shape and | |
// add up the number of exposed edges | |
// console.log(shape); | |
var i, j, p = 0; | |
var pixels = shape.pixels; | |
pixels.forEach((pixel) => { | |
var n = [pixel[0]-1, pixel[1]]; | |
var e = [pixel[0], pixel[1]+1]; | |
var s = [pixel[0]+1, pixel[1]]; | |
var w = [pixel[0], pixel[1]-1]; | |
var neighbors = [n,e,s,w]; | |
var edges = 4; | |
neighbors.forEach((n) => { | |
if (searchForArray(pixels,n) > -1) { | |
edges--; | |
} | |
}) | |
p += edges; | |
// console.log({pixel, neighbors, edges}); | |
}) | |
return p; | |
} | |
// translates a shape horizontally by dx | |
// and vertically by dy | |
// returns shape with new pixels | |
// if still inside the grid | |
// if not possible returns false | |
translateShape = (shape, dx, dy, gh, gw) => { | |
var pixels = shape.pixels; | |
var result = false; | |
pixels.forEach((p) => { | |
if ((p[1] + dx >= gw) || (p[1] + dx < 0) || (p[0] + dy >= gh) || (p[0] + dy < 0)) { | |
result = false; | |
return result; | |
} | |
}); | |
var newPixels = pixels.map((p)=>[p[0]+dy,p[1]+dx]); | |
// [[a,b],[a,b],[a,b]] | |
// if you find any a >= | |
result = newPixels; | |
return result; | |
} | |
// given the grid and a perimeter p | |
// return all shapes with perimeter p | |
// shapes are | |
// 4-connected pixel neighborhoods | |
getShapes = (grid, p) => { | |
// console.log(shape); | |
var shapes = []; | |
var gh = grid.length; | |
var gw = grid[0].length; | |
var shape = { | |
pixels: [[0,0], [0,1], [1,0], [1,1]] | |
} | |
shapes.push(shape); | |
var dx = 3; | |
var dy = 0; | |
shapes.push({pixels:translateShape(shape,dx,dy,gh,gw)}); | |
console.log({ | |
'grid height' : gh, | |
'grid width' : gw, | |
'shapes' : shapes | |
}); | |
} | |
var grid = [[1, 1, 1, 1], | |
[1, 5, 2, 1], | |
[0,-2, 3, 1], | |
[1, 2, 4, 1]]; | |
var p = 4; | |
getShapes(grid,p); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function searchForArray(haystack, needle){ | |
var i, j, current; | |
for(i = 0; i < haystack.length; ++i){ | |
if(needle.length === haystack[i].length){ | |
current = haystack[i]; | |
for(j = 0; j < needle.length && needle[j] === current[j]; ++j); | |
if(j === needle.length) | |
return i; | |
} | |
} | |
return -1; | |
} | |
getRects = (g,p) => { | |
var rects = [], i, j, k; | |
for (i = 1; i < p/2; i++) { | |
var rect = { | |
w: i, | |
h: p/2-i, | |
targets: [], | |
}; | |
for (j = 0; j <= g.length - rect.h; j++) { | |
for (k = 0; k <= g.length - rect.w; k++) { | |
rect.targets.push([j,k]); | |
} | |
} | |
rects.push(rect); | |
} | |
return rects; | |
} | |
getMaxSum = (grid,rects) => { | |
var i,j; | |
var sum = 0; | |
var maxSum = 0; | |
rects.forEach((r) => { | |
r.targets.forEach((t) => { | |
sum = 0; | |
for (i = t[0]; i < t[0]+r.h; i++) { | |
for (j = t[1]; j < t[1]+r.w; j++) { | |
sum += grid[i][j]; | |
} | |
} | |
if (sum > maxSum) maxSum = sum; | |
}) | |
}) | |
return maxSum; | |
} | |
sumInPerimeter = (grid, p) => { | |
var rects = getRects(grid, p); | |
var maxSum = getMaxSum(grid, rects); | |
console.log(maxSum); | |
return maxSum; | |
} | |
// sumInPerimeter(grid, p) | |
// given a shape return its perimeter | |
// shapes are | |
// 4-connected pixel neighborhoods | |
getPerim = (shape) => { | |
// go through each pixel in the shape and | |
// add up the number of exposed edges | |
// console.log(shape); | |
var i, j, p = 0; | |
var pixels = shape.pixels; | |
pixels.forEach((pixel) => { | |
var n = [pixel[0]-1, pixel[1]]; | |
var e = [pixel[0], pixel[1]+1]; | |
var s = [pixel[0]+1, pixel[1]]; | |
var w = [pixel[0], pixel[1]-1]; | |
var neighbors = [n,e,s,w]; | |
var edges = 4; | |
neighbors.forEach((n) => { | |
if (searchForArray(pixels,n) > -1) { | |
edges--; | |
} | |
}) | |
p += edges; | |
// console.log({pixel, neighbors, edges}); | |
}) | |
return p; | |
} | |
// translates a shape horizontally by dx | |
// and vertically by dy | |
// returns shape with new pixels | |
// if still inside the grid | |
// if not possible returns false | |
translateShape = (shape, dx, dy, gh, gw) => { | |
var pixels = shape.pixels; | |
var result = false; | |
pixels.forEach((p) => { | |
if ((p[1] + dx >= gw) || (p[1] + dx < 0) || (p[0] + dy >= gh) || (p[0] + dy < 0)) { | |
result = false; | |
return result; | |
} | |
}); | |
var newPixels = pixels.map((p)=>[p[0]+dy,p[1]+dx]); | |
// [[a,b],[a,b],[a,b]] | |
// if you find any a >= | |
result = newPixels; | |
return result; | |
} | |
// given the grid and a perimeter p | |
// return all shapes with perimeter p | |
// shapes are | |
// 4-connected pixel neighborhoods | |
getShapes = (grid, p) => { | |
// console.log(shape); | |
var shapes = []; | |
var gh = grid.length; | |
var gw = grid[0].length; | |
var shape = { | |
pixels: [[0,0], [0,1], [1,0], [1,1]] | |
} | |
shapes.push(shape); | |
var dx = 3; | |
var dy = 0; | |
shapes.push({pixels:translateShape(shape,dx,dy,gh,gw)}); | |
console.log({ | |
'grid height' : gh, | |
'grid width' : gw, | |
'shapes' : shapes | |
}); | |
} | |
var grid = [[1, 1, 1, 1], | |
[1, 5, 2, 1], | |
[0,-2, 3, 1], | |
[1, 2, 4, 1]]; | |
var p = 4; | |
getShapes(grid,p); | |
</script></body> | |
</html> |
This file contains 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
function searchForArray(haystack, needle){ | |
var i, j, current; | |
for(i = 0; i < haystack.length; ++i){ | |
if(needle.length === haystack[i].length){ | |
current = haystack[i]; | |
for(j = 0; j < needle.length && needle[j] === current[j]; ++j); | |
if(j === needle.length) | |
return i; | |
} | |
} | |
return -1; | |
} | |
getRects = (g,p) => { | |
var rects = [], i, j, k; | |
for (i = 1; i < p/2; i++) { | |
var rect = { | |
w: i, | |
h: p/2-i, | |
targets: [], | |
}; | |
for (j = 0; j <= g.length - rect.h; j++) { | |
for (k = 0; k <= g.length - rect.w; k++) { | |
rect.targets.push([j,k]); | |
} | |
} | |
rects.push(rect); | |
} | |
return rects; | |
} | |
getMaxSum = (grid,rects) => { | |
var i,j; | |
var sum = 0; | |
var maxSum = 0; | |
rects.forEach((r) => { | |
r.targets.forEach((t) => { | |
sum = 0; | |
for (i = t[0]; i < t[0]+r.h; i++) { | |
for (j = t[1]; j < t[1]+r.w; j++) { | |
sum += grid[i][j]; | |
} | |
} | |
if (sum > maxSum) maxSum = sum; | |
}) | |
}) | |
return maxSum; | |
} | |
sumInPerimeter = (grid, p) => { | |
var rects = getRects(grid, p); | |
var maxSum = getMaxSum(grid, rects); | |
console.log(maxSum); | |
return maxSum; | |
} | |
// sumInPerimeter(grid, p) | |
// given a shape return its perimeter | |
// shapes are | |
// 4-connected pixel neighborhoods | |
getPerim = (shape) => { | |
// go through each pixel in the shape and | |
// add up the number of exposed edges | |
// console.log(shape); | |
var i, j, p = 0; | |
var pixels = shape.pixels; | |
pixels.forEach((pixel) => { | |
var n = [pixel[0]-1, pixel[1]]; | |
var e = [pixel[0], pixel[1]+1]; | |
var s = [pixel[0]+1, pixel[1]]; | |
var w = [pixel[0], pixel[1]-1]; | |
var neighbors = [n,e,s,w]; | |
var edges = 4; | |
neighbors.forEach((n) => { | |
if (searchForArray(pixels,n) > -1) { | |
edges--; | |
} | |
}) | |
p += edges; | |
// console.log({pixel, neighbors, edges}); | |
}) | |
return p; | |
} | |
// translates a shape horizontally by dx | |
// and vertically by dy | |
// returns shape with new pixels | |
// if still inside the grid | |
// if not possible returns false | |
translateShape = (shape, dx, dy, gh, gw) => { | |
var pixels = shape.pixels; | |
var result = false; | |
pixels.forEach((p) => { | |
if ((p[1] + dx >= gw) || (p[1] + dx < 0) || (p[0] + dy >= gh) || (p[0] + dy < 0)) { | |
result = false; | |
return result; | |
} | |
}); | |
var newPixels = pixels.map((p)=>[p[0]+dy,p[1]+dx]); | |
// [[a,b],[a,b],[a,b]] | |
// if you find any a >= | |
result = newPixels; | |
return result; | |
} | |
// given the grid and a perimeter p | |
// return all shapes with perimeter p | |
// shapes are | |
// 4-connected pixel neighborhoods | |
getShapes = (grid, p) => { | |
// console.log(shape); | |
var shapes = []; | |
var gh = grid.length; | |
var gw = grid[0].length; | |
var shape = { | |
pixels: [[0,0], [0,1], [1,0], [1,1]] | |
} | |
shapes.push(shape); | |
var dx = 3; | |
var dy = 0; | |
shapes.push({pixels:translateShape(shape,dx,dy,gh,gw)}); | |
console.log({ | |
'grid height' : gh, | |
'grid width' : gw, | |
'shapes' : shapes | |
}); | |
} | |
var grid = [[1, 1, 1, 1], | |
[1, 5, 2, 1], | |
[0,-2, 3, 1], | |
[1, 2, 4, 1]]; | |
var p = 4; | |
getShapes(grid,p); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment