From: Scott Worley Date: Wed, 21 May 2014 07:59:14 +0000 (-0700) Subject: Use a params object rather than loose arguments X-Git-Url: http://git.scottworley.com/nt3d/commitdiff_plain/3bafa9bd82dbcd125a75e796f8b78ab63a560422 Use a params object rather than loose arguments Otherwise, making subroutines is tedious -- each function has to accept a bunch of loose args only to pass them along. Adding a parameter that in only used in a subroutine required changing all the callers all the way up the stack. --- diff --git a/box_by_extrude.html b/box_by_extrude.html index fb1ca2c..62dca74 100644 --- a/box_by_extrude.html +++ b/box_by_extrude.html @@ -3,17 +3,17 @@ Box by extrude diff --git a/box_by_faces.html b/box_by_faces.html index b27d795..938c8e8 100644 --- a/box_by_faces.html +++ b/box_by_faces.html @@ -3,15 +3,15 @@ Box by faces diff --git a/box_by_quadstrip.html b/box_by_quadstrip.html index 83ad5ab..ede01d9 100644 --- a/box_by_quadstrip.html +++ b/box_by_quadstrip.html @@ -3,21 +3,21 @@ Box by quadstrip diff --git a/box_by_rotation.html b/box_by_rotation.html index d575a42..e39394e 100644 --- a/box_by_rotation.html +++ b/box_by_rotation.html @@ -3,11 +3,11 @@ Box by rotation diff --git a/box_by_trianglefan.html b/box_by_trianglefan.html index 7c0519c..3090d4a 100644 --- a/box_by_trianglefan.html +++ b/box_by_trianglefan.html @@ -3,20 +3,20 @@ Box by trianglefan diff --git a/cone_primitive.html b/cone_primitive.html index 19a7686..ef044fd 100644 --- a/cone_primitive.html +++ b/cone_primitive.html @@ -3,12 +3,12 @@ Cone primitive diff --git a/medusa.html b/medusa.html index e82d9e5..69ea3ba 100644 --- a/medusa.html +++ b/medusa.html @@ -3,19 +3,19 @@ Medusa diff --git a/nt3d.js b/nt3d.js index d4e62f6..80390b9 100644 --- a/nt3d.js +++ b/nt3d.js @@ -421,14 +421,14 @@ nt3d = { setTimeout(function() { // Get params from form - var params = []; + var params = {}; for (var i = 0; i < this.user_params.length; i++) { var as_string = this.form.elements["param"+i].value; var as_num = +as_string; - params[i] = isNaN(as_num) ? as_string : as_num; + params[this.user_params[i][0]] = isNaN(as_num) ? as_string : as_num; } - this.points = this.user_function.apply(null, params); + this.points = this.user_function.call(null, params); this.validate(this.points); @@ -476,7 +476,18 @@ nt3d = { tr = document.createElement("tr"); table.appendChild(tr); var td = document.createElement("td"); - td.appendChild(document.createTextNode(params[i][0])); + var description; + if (params[i].length > 2) { + description = params[i][2]; + } else { + description = params[i][0]; + description = description[0].toUpperCase() + description.substr(1); + description = description.replace(/_(.)/g, function(_, c) { + return " " + c.toUpperCase(); + }); + description = description.replace("Num ", "Number of "); + } + td.appendChild(document.createTextNode(description)); tr.appendChild(td); td = document.createElement("td"); var input = document.createElement("input"); diff --git a/rmm.html b/rmm.html index dd212e5..8264bd1 100644 --- a/rmm.html +++ b/rmm.html @@ -9,43 +9,41 @@ // https://plus.google.com/101508799331795210529/posts/Yg7LmV8BpTw return 3.7*x + (Math.log(1-.95*x)-Math.log(.95*x+1)); } - function rmm(base_separation, base_offset, - width, short_arc_height, tall_arc_height, - tube_width, steps_along, steps_around) { + function rmm(params) { var tau = Math.PI*2; var path = []; // Short arc - for (var i = 0; i < steps_along; i++) { - var progress = i/steps_along; - var x_separation = Math.cos(progress*tau/2)*base_separation; - var y_offset = -Math.sin(progress*tau/4)*base_offset; - var y_arc = Math.sin(progress*tau)*width; - var z_arc = -Math.cos(progress*tau)*short_arc_height + short_arc_height; + for (var i = 0; i < params.steps_along; i++) { + var progress = i/params.steps_along; + var x_separation = Math.cos(progress*tau/2)*params.base_separation; + var y_offset = -Math.sin(progress*tau/4)*params.base_offset; + var y_arc = Math.sin(progress*tau)*params.width; + var z_arc = -Math.cos(progress*tau)*params.short_arc_height + params.short_arc_height; path.push([x_separation, y_offset + y_arc, z_arc]); } // Tall arc - for (var i = 0; i < steps_along; i++) { - var progress = i/steps_along; - var x_arc = -Math.cos(progress*tau*1.5)*base_separation; - var y_offset = -Math.cos(progress*tau/4)*base_offset; - var y_twist = -pointy_sin(progress*2 - 1)*width/2; - var z_arc = -Math.cos(progress*tau)*tall_arc_height + tall_arc_height; + for (var i = 0; i < params.steps_along; i++) { + var progress = i/params.steps_along; + var x_arc = -Math.cos(progress*tau*1.5)*params.base_separation; + var y_offset = -Math.cos(progress*tau/4)*params.base_offset; + var y_twist = -pointy_sin(progress*2 - 1)*params.width/2; + var z_arc = -Math.cos(progress*tau)*params.tall_arc_height + params.tall_arc_height; path.push([x_arc, y_offset + y_twist, z_arc]); } return nt3d.closed_extrude( path, - nt3d.circle(tube_width, steps_around), + nt3d.circle(params.tube_width, params.steps_around), nt3d.shapenormals_from_closed_path(path), nt3d.pathnormals_from_point(path, [0,0,0])); } - var params = [["Base separation", 30], - ["Base offset", 10], - ["Width", 40], - ["Short arc height", 60], - ["Tall arc height", 80], - ["Tube width", 10], - ["Steps along each arc", 70], - ["Steps around", 12]]; + var params = [["base_separation", 30], + ["base_offset", 10], + ["width", 40], + ["short_arc_height", 60], + ["tall_arc_height", 80], + ["tube_width", 10], + ["steps_along", 70, "Steps Along Each Arc"], + ["steps_around", 12]]; diff --git a/serrated.html b/serrated.html index 99de7ed..f688d5d 100644 --- a/serrated.html +++ b/serrated.html @@ -3,42 +3,42 @@ Serrated diff --git a/sphere_primitive.html b/sphere_primitive.html index 0e5095a..4eaa132 100644 --- a/sphere_primitive.html +++ b/sphere_primitive.html @@ -3,12 +3,12 @@ Sphere primitive diff --git a/torus_by_extrude.html b/torus_by_extrude.html index 10ae92e..ef323a9 100644 --- a/torus_by_extrude.html +++ b/torus_by_extrude.html @@ -3,20 +3,20 @@ Torus by extrude diff --git a/wrap_sphere.html b/wrap_sphere.html index 1372939..4d51914 100644 --- a/wrap_sphere.html +++ b/wrap_sphere.html @@ -3,28 +3,28 @@ Wrap Sphere