Created
April 19, 2014 18:48
-
-
Save AlphaGit/11093696 to your computer and use it in GitHub Desktop.
Sugiyama algorithm: add fake nodes when needed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fakeNodeData = { __fake: true }; | |
sugiyamaService.insertFakeNodeBeforeNext = function(list, currentNode, nextNode) { | |
var fakeNode = sugiyamaService.createNode(fakeNodeData); | |
sugiyamaService.disconnectNodes(currentNode, nextNode); | |
sugiyamaService.connectNodes(fakeNode, nextNode); | |
sugiyamaService.connectNodes(currentNode, fakeNode); | |
list.push(fakeNode); | |
}; | |
sugiyamaService.addFakeNodes = function(columns) { | |
// check all columns except the last one | |
for (var columnIndex = 0; columnIndex < columns.length - 1; columnIndex++) { | |
var nextColumn = columns[columnIndex + 1]; | |
for (var rowIndex = 0; rowIndex < columns[columnIndex].length; rowIndex++) { | |
var currentNode = columns[columnIndex][rowIndex]; | |
for (var nextNodeIndex = 0; nextNodeIndex < currentNode.nextNodes.length; nextNodeIndex++) { | |
var nextNode = currentNode.nextNodes[nextNodeIndex]; | |
if (nextColumn.indexOf(nextNode) === -1) { | |
sugiyamaService.insertFakeNodeBeforeNext(columns[columnIndex + 1], currentNode, nextNode); | |
} | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment