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
/*
So this is a basic mechanism to determin if a flow of positions land
in fixed rectangular areas. Depending on certain conditions for each areas
a callback is executed or no.
= 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
*/
var areas = new Array();
var conditions = new Array();
var callbacks = new Array();
function initialize(areas, conditions, callbacks)
{
areas = areas;
conditions = conditions;
callbacks = callbacks;
}
function run(stream_data)
{
active_areas = activate_areas(stream_data, areas);
}
function destroy()
{
areas = new Array();
conditions = new Array();
callbacks = new Array();
}
function activate_areas(stream_data, areas)
{
active_areas = new Array();
for (i = 0; i < areas.length ; i++)
{
for (j = 0; j < stream_data.length ; j++)
{
//x_1 = areas[i][2][0];
//x_2 = areas[i][2][0] + areas[i][1];
//y_1 = areas[i][2][1];
//y_2 = areas[i][2][1] + areas[i][0];
in_area = (((stream_data[j][1] < areas[i][2][1]) && ((areas[i][2][1] + areas[i][0]) < stream_data[j][1])) && ((stream_data[j][0] > areas[i][2][0]) && (stream_data[0] < (areas[i][2][0] + areas[i][1]))));
if ((in_area) && (conditions.length > 0) && (conditions[i] != undefined) && (conditions[i]))
{
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