Skip to content

Commit

Permalink
jshint cleanup for position
Browse files Browse the repository at this point in the history
  • Loading branch information
cpettitt committed Oct 3, 2013
1 parent 4ba1528 commit 194e3ae
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions lib/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,24 @@ module.exports = function() {
g.eachNode(function(v) {
var xs = [];
for (var alignment in xss) {
xDebug(alignment, g, v, xss[alignment][v]);
posXDebug(alignment, g, v, xss[alignment][v]);
xs.push(xss[alignment][v]);
}
xs.sort(function(x, y) { return x - y; });
x(g, v, (xs[1] + xs[2]) / 2);
posX(g, v, (xs[1] + xs[2]) / 2);
});

// Translate layout so left edge of bounding rectangle has coordinate 0
var minX = util.min(g.nodes().map(function(u) { return x(g, u) - width(g, u) / 2; }));
g.eachNode(function(u) { x(g, u, x(g, u) - minX); });
var minX = util.min(g.nodes().map(function(u) { return posX(g, u) - width(g, u) / 2; }));
g.eachNode(function(u) { posX(g, u, posX(g, u) - minX); });

// Align y coordinates with ranks
var posY = 0;
var y = 0;
layering.forEach(function(layer) {
var maxHeight = util.max(layer.map(function(u) { return height(g, u); }));
posY += maxHeight / 2;
layer.forEach(function(u) { y(g, u, posY); });
posY += maxHeight / 2 + config.rankSep;
y += maxHeight / 2;
layer.forEach(function(u) { posY(g, u, y); });
y += maxHeight / 2 + config.rankSep;
});
}

Expand All @@ -105,18 +105,29 @@ module.exports = function() {

function findConflicts(g, layering) {
var conflicts = {}, // Set of conflicting edge ids
pos = {}; // Position of node in its layer
pos = {}, // Position of node in its layer
prevLayer,
currLayer,
k0, // Position of the last inner segment in the previous layer
l, // Current position in the current layer (for iteration up to `l1`)
k1; // Position of the next inner segment in the previous layer or
// the position of the last element in the previous layer

if (layering.length <= 2) return conflicts;

function updateConflicts(v) {
var k = pos[v];
if (k < k0 || k > k1) {
conflicts[undirEdgeId(currLayer[l], v)] = true;
}
}

layering[1].forEach(function(u, i) { pos[u] = i; });
for (var i = 1; i < layering.length - 1; ++i) {
var prevLayer = layering[i];
var currLayer = layering[i+1];
var k0 = 0; // Position of the last inner segment in the previous layer
var l = 0; // Current position in the current layer (for iteration up to `l1`)
var k1; // Position of the next inner segment in the previous layer or
// the position of the last element in the previous layer
prevLayer = layering[i];
currLayer = layering[i+1];
k0 = 0;
l = 0;

// Scan current layer for next node that is incident to an inner segement
// between layering[i+1] and layering[i].
Expand All @@ -136,11 +147,7 @@ module.exports = function() {

if (k1 !== undefined) {
for (; l <= l1; ++l) {
g.predecessors(currLayer[l]).forEach(function(v) {
var k = pos[v];
if (k < k0 || k > k1)
conflicts[undirEdgeId(currLayer[l], v)] = true;
});
g.predecessors(currLayer[l]).forEach(updateConflicts);
}
k0 = k1;
}
Expand Down Expand Up @@ -295,6 +302,10 @@ module.exports = function() {
smallestAlignment,
shift = {}; // Amount to shift a given alignment

function updateAlignment(v) {
xss[alignment][v] += shift[alignment];
}

var smallest = Number.POSITIVE_INFINITY;
for (var alignment in xss) {
var xs = xss[alignment];
Expand All @@ -318,10 +329,8 @@ module.exports = function() {
});

// Find average of medians for xss array
for (var alignment in xss) {
g.eachNode(function(v) {
xss[alignment][v] += shift[alignment];
});
for (alignment in xss) {
g.eachNode(updateAlignment);
}
}

Expand Down Expand Up @@ -360,7 +369,7 @@ module.exports = function() {
return (w + s) / 2;
}

function x(g, u, x) {
function posX(g, u, x) {
switch (config.rankDir) {
case "LR":
if (arguments.length < 3) {
Expand All @@ -378,7 +387,7 @@ module.exports = function() {
}
}

function xDebug(name, g, u, x) {
function posXDebug(name, g, u, x) {
switch (config.rankDir) {
case "LR":
if (arguments.length < 3) {
Expand All @@ -396,7 +405,7 @@ module.exports = function() {
}
}

function y(g, u, y) {
function posY(g, u, y) {
switch (config.rankDir) {
case "LR":
if (arguments.length < 3) {
Expand Down

0 comments on commit 194e3ae

Please sign in to comment.