Created
September 18, 2020 07:53
-
-
Save asm-jaime/511869bf1e8098e347ef5ab28248c1a4 to your computer and use it in GitHub Desktop.
fast clasterization by k-mean+grid on javascript
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
const grid_clasterization = (points, quadrant_size, grid_size, x_max, y_max) => { | |
const quadrants = Array.apply(null, {length: Math.ceil(grid_size/quadrant_size)}).map(e => 0); | |
const grid_width = Math.sqrt(grid_size); | |
const quadrant_width = Math.sqrt(quadrant_size); | |
const grid_quadro_x_size = (x_max / grid_width)*quadrant_width; | |
const grid_quadro_y_size = (y_max / grid_width)*quadrant_width; | |
for(const point of points) { | |
const x_index = (Math.ceil(point.x/grid_quadro_x_size) - 1); | |
const y_index = (Math.ceil(point.y/grid_quadro_y_size) - 1); | |
const index = x_index + (quadrant_width*y_index); | |
quadrants[index]++; | |
} | |
return quadrants; | |
}; | |
// ========== test grid_clasterization | |
const print_quadro_array = array => { | |
const size = Math.sqrt(array.length); | |
let result = ''; | |
for(let i = 0; i < size; ++i) { | |
result = `${result}\n`; | |
for(let j = 0; j < size; ++j) { | |
result = `${result} ${array[i + size*j]}`; | |
} | |
} | |
return result; | |
}; | |
const points = [ | |
{x:0.9, y:0.9}, | |
{x:0.91, y:0.92}, | |
{x:0.3, y:0.3}, | |
{x:0.35, y:0.35}, | |
]; | |
const quadrant_size = 9; | |
const grid_size = 81; | |
const x_max = 1; | |
const y_max = 1; | |
const quadrants = grid_clasterization(points, quadrant_size, grid_size, x_max, y_max); | |
print_quadro_array(quadrants); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment