From 044ec05a1e105b085fee1c5f3a29e6be67a7a651 Mon Sep 17 00:00:00 2001 From: lumos309 <35829443+lumos309@users.noreply.github.com> Date: Wed, 31 Jul 2019 17:31:30 +0800 Subject: [PATCH 1/2] Resize viewport to fit drawing --- public/externalLibs/visualizer/visualizer.js | 83 +++++++++++++++++--- 1 file changed, 74 insertions(+), 9 deletions(-) diff --git a/public/externalLibs/visualizer/visualizer.js b/public/externalLibs/visualizer/visualizer.js index df49841db6..c1eacf12a4 100644 --- a/public/externalLibs/visualizer/visualizer.js +++ b/public/externalLibs/visualizer/visualizer.js @@ -7,11 +7,6 @@ container.id = 'list-visualizer-container' container.hidden = true document.body.appendChild(container) - stage = new Kinetic.Stage({ - width: 1000, - height: 1000, - container: 'list-visualizer-container' - }) /** * Converts a list, or a pair, to a tree object. Wrapper function. @@ -871,9 +866,14 @@ * Then shift it to the left end. */ function draw(xs) { + stage = new Kinetic.Stage({ + width: findListWidth(xs) * 45 + 200, + height: findListHeight(xs) * 60 + 100, + container: 'list-visualizer-container' + }); minLeft = 500 nodelist = [] - fnNodeList = [] + fnNodeList = [] nodeLabel = 0 // hides all other layers for (var i = 0; i < layerList.length; i++) { @@ -951,10 +951,75 @@ updateListVisualizerButtons() } - function is_function(data) { - return typeof(data) == "function" - } + function is_function(data) { + return typeof(data) == "function" + } + + /** + * Find the height of a drawing (in number of "rows" of pairs) + */ + function findListHeight(xs) { + // Store pairs/arrays that were traversed previously so as to not double-count their height. + const existing = []; + + function helper(xs) { + if ((!is_pair(xs) && !is_array(xs)) || is_null(xs)) { + return 0; + } else { + const leftHeight = existing.includes(xs[0]) + ? 0 + : helper(xs[0]); + if (!existing.includes(xs[0]) + && (is_pair(xs[0]) || is_array(xs[0]))) { + existing.push(xs[0]); + } + const rightHeight = existing.includes(xs[1]) + ? 0 + : helper(xs[1]); + if (!existing.includes(xs[1]) + && (is_pair(xs[1]) || is_array(xs[1]))) { + existing.push(xs[1]); + } + return leftHeight > rightHeight + ? 1 + leftHeight + : 1 + rightHeight; + } + } + + return helper(xs, []); + } + + /** + * Find the width of a drawing (in number of "columns" of pairs) + */ + function findListWidth(xs) { + const existing = []; + + function helper(xs) { + if ((!is_pair(xs) && !is_array(xs)) || is_null(xs)) { + return 0; + } else { + const leftHeight = existing.includes(xs[0]) + ? 0 + : helper(xs[0]); + if (!existing.includes(xs[0]) + && (is_pair(xs[0]) || is_array(xs[0]))) { + existing.push(xs[0]); + } + const rightHeight = existing.includes(xs[1]) + ? 0 + : helper(xs[1]); + if (!existing.includes(xs[1]) + && (is_pair(xs[1]) || is_array(xs[1]))) { + existing.push(xs[1]); + } + return leftHeight + rightHeight + 1; + } + } + return helper(xs); + } + exports.ListVisualizer = { draw: draw, clear: clearListVisualizer, From 8a4377430cec983ae4e9b33727fc1027c1a33b69 Mon Sep 17 00:00:00 2001 From: lumos309 <35829443+lumos309@users.noreply.github.com> Date: Sun, 4 Aug 2019 17:04:43 +0800 Subject: [PATCH 2/2] Fixed bug for circular lists --- public/externalLibs/visualizer/visualizer.js | 46 +++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/public/externalLibs/visualizer/visualizer.js b/public/externalLibs/visualizer/visualizer.js index c1eacf12a4..8bfd902742 100644 --- a/public/externalLibs/visualizer/visualizer.js +++ b/public/externalLibs/visualizer/visualizer.js @@ -966,19 +966,21 @@ if ((!is_pair(xs) && !is_array(xs)) || is_null(xs)) { return 0; } else { - const leftHeight = existing.includes(xs[0]) - ? 0 - : helper(xs[0]); - if (!existing.includes(xs[0]) - && (is_pair(xs[0]) || is_array(xs[0]))) { + let leftHeight; + let rightHeight; + if (existing.includes(xs[0]) + || (!is_pair(xs[0]) && (!is_array(xs[0])))) { + leftHeight = 0; + } else { existing.push(xs[0]); + leftHeight = helper(xs[0]); } - const rightHeight = existing.includes(xs[1]) - ? 0 - : helper(xs[1]); - if (!existing.includes(xs[1]) - && (is_pair(xs[1]) || is_array(xs[1]))) { + if (existing.includes(xs[1]) + || (!is_pair(xs[1]) && (!is_array(xs[1])))) { + rightHeight = 0; + } else { existing.push(xs[1]); + rightHeight = helper(xs[1]); } return leftHeight > rightHeight ? 1 + leftHeight @@ -999,21 +1001,23 @@ if ((!is_pair(xs) && !is_array(xs)) || is_null(xs)) { return 0; } else { - const leftHeight = existing.includes(xs[0]) - ? 0 - : helper(xs[0]); - if (!existing.includes(xs[0]) - && (is_pair(xs[0]) || is_array(xs[0]))) { + let leftWidth; + let rightWidth; + if (existing.includes(xs[0]) + || (!is_pair(xs[0]) && (!is_array(xs[0])))) { + leftWidth = 0; + } else { existing.push(xs[0]); + leftWidth = helper(xs[0]); } - const rightHeight = existing.includes(xs[1]) - ? 0 - : helper(xs[1]); - if (!existing.includes(xs[1]) - && (is_pair(xs[1]) || is_array(xs[1]))) { + if (existing.includes(xs[1]) + || (!is_pair(xs[1]) && (!is_array(xs[1])))) { + rightWidth = 0; + } else { existing.push(xs[1]); + rightWidth = helper(xs[1]); } - return leftHeight + rightHeight + 1; + return leftWidth + rightWidth + 1; } }