Skip to content

Instantly share code, notes, and snippets.

@guiman
Created November 2, 2012 19:32
Show Gist options
  • Save guiman/4003818 to your computer and use it in GitHub Desktop.
Save guiman/4003818 to your computer and use it in GitHub Desktop.
Javascript active area mechanism draft
/*
Javascript Active area.
-----------------------
So this is pretty much what it does, you define areas and this code
allows you to verify if your data source is retreiving points
in any of the defined areas.
*/
window.areas = new Array();
window.conditions = new Array();
window.callbacks = new Array();
function initialize(areas, conditions, callbacks)
{
window.areas = areas;
window.conditions = conditions;
window.callbacks = callbacks;
}
function run(stream_data)
{
active_areas = activate_areas(stream_data, window.areas);
}
function destroy()
{
window.areas = new Array();
window.conditions = new Array();
window.callbacks = new Array();
}
/*
= Area should be something like [H,W,P] where:
H = height, numeric
W = width, numeric
P = point, array [x,y] representing the upper left corner
= Stream data expected is: [[X,Y,Z], [X,Y,Z], ...]
X = horizontal position
Y = vertical position
Z = magnitued
*/
function activate_areas(stream_data, areas)
{
active_areas = new Array();
for (i = 0; i < areas.length ; i++)
{
x = stream_data[0];
y = stream_data[1];
x_1 = areas[i][2][0];
x_2 = x_1 + areas[i][0];
y_1 = areas[i][2][1];
y_2 = y_1 + areas[i][1];
in_area = (y >= y_1) && (y <= y_2) && (x >= x_1) && (x <= x_2)
if (in_area)
{
active_areas.push(i);
callbacks[i]();
}
}
return active_areas;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment