Skip to content

Commit

Permalink
fixing kdtree-bugs, adding in grid logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yelper committed Nov 20, 2015
1 parent 3fe2108 commit cd62382
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fileops.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ var loadFile = function(text, dataStart, xCol, yCol, grpCol) {
var euclidDist = function(a,b) {
var dx = a.x - b.x;
var dy = a.y - b.y;
return dx * dx - dy * dy;
return dx * dx + dy * dy;
};

pointTree = new kdTree(points, euclidDist, ['x', 'y']);
Expand Down
40 changes: 40 additions & 0 deletions splatterplot.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ gl.onmousedown = function(e) {
panY = gl.canvas.height - e.y;
};

var lastPt = {'x': Number.MAX_VALUE, 'y': Number.MIN_VALUE};
gl.onmousemove = function(e) {
if (drags(e)) {
screenOffset[0] += e.x - panX;
Expand All @@ -861,6 +862,45 @@ gl.onmousemove = function(e) {
panX = e.x;
panY = gl.canvas.height - e.y;
gl.ondraw();
} else {
// get actual mouse position within the canvas
var mouseX = e.x - gl.canvas.offsetLeft;
var mouseY = gl.canvas.height - (e.y - gl.canvas.offsetParent.offsetTop);

// just do the simple thing and ask which grid cell we're in
var minPt = [untransformX(0), untransformY(0)];
var maxPt = [untransformX(gl.canvas.width), untransformY(gl.canvas.height)];
var gridSize = [gl.canvas.width / clutterRadius, gl.canvas.height / clutterRadius];

// get the grid size in actual coords
for (var i = 0; i < 2; i++) {
gridSize[i] = (maxPt[i] - minPt[i]) / gridSize[i];
}

var resolution = [gl.canvas.width, gl.canvas.height];

var gridOffset = [0,0];
for (var i = 0; i < 2; i++) {
gridOffset[i] = (minPt[i] / gridSize[i]) - Math.floor(minPt[i] / gridSize[i]);
gridOffset[i] = gridOffset[i] * clutterRadius / resolution[i];
}

var curPos = [untransformX(mouseX), untransformY(mouseY)];
var curCell = [];
for (var i = 0; i < 2; i++) {
curCell[i] = Math.floor((curPos[i] + gridOffset[i] - minPt[i]) / gridSize[i]);
}

// console.log("---");
// console.log("pixel space: %3d, %3d, point space: %4.3f, %4.3f", mouseX, mouseY, curPos[0], curPos[1]);
// console.log("in gridcell %2d, %2d!", curCell[0], curCell[1]);

// get closest point
var nearestPt = pointTree.nearest({'x': curPos[0], 'y': curPos[1]}, 1)[0];
if (!(lastPt.x == nearestPt[0].x && lastPt.y == nearestPt[0].y)) {
console.log("%s (%4.3f, %4.3f), distance %4.3f", nearestPt[0].grp, nearestPt[0].x, nearestPt[0].y, nearestPt[1]);
lastPt = nearestPt[0];
}
}
};

Expand Down

0 comments on commit cd62382

Please sign in to comment.