blob: 1c188d9f77592ff26fd4d7c3dc9fca428b375da8 [file] [log] [blame]
library graphlib.layout.charted.intersect.rect;
intersectRect(node, point) {
var x = node.x;
var y = node.y;
// Rectangle intersection algorithm from:
// http://math.stackexchange.com/questions/108113/find-edge-between-two-boxes
var dx = point.x - x;
var dy = point.y - y;
var w = node.width / 2;
var h = node.height / 2;
var sx, sy;
if (dy.abs() * w > dx.abs() * h) {
// Intersection is top or bottom of rect.
if (dy < 0) {
h = -h;
}
sx = dy == 0 ? 0 : h * dx / dy;
sy = h;
} else {
// Intersection is left or right of rect.
if (dx < 0) {
w = -w;
}
sx = w;
sy = dx == 0 ? 0 : w * dy / dx;
}
return {"x": x + sx, "y": y + sy};
}