diff --git a/components/sections/aligning/handler.js b/components/sections/aligning/handler.js index 9b45b4ea..cd5f252c 100644 --- a/components/sections/aligning/handler.js +++ b/components/sections/aligning/handler.js @@ -1,17 +1,31 @@ module.exports = { + /** + * Setup function for a default quadratic curve. + */ setupQuadratic: function(api) { var curve = api.getDefaultQuadratic(); api.setCurve(curve); }, + /** + * Setup function for a default cubic curve. + */ setupCubic: function(api) { var curve = api.getDefaultCubic(); api.setCurve(curve); }, + /** + * A coordinate rotation function that rotates and + * translates the curve, such that the first coordinate + * of the curve is (0,0) and the last coordinate is (..., 0) + */ align: function(points, line) { var tx = line.p1.x, ty = line.p1.y, + // The atan2 function is so important to computing + // that most CPUs have a dedicated implementation + // at the hardware level for it. a = -Math.atan2(line.p2.y-ty, line.p2.x-tx), cos = Math.cos, sin = Math.sin, @@ -24,6 +38,10 @@ module.exports = { return points.map(d); }, + /** + * Draw a curve and its aligned counterpart + * side by side across two panels. + */ draw: function(api, curve) { api.setPanelCount(2); api.reset(); diff --git a/components/sections/arcapproximation/handler.js b/components/sections/arcapproximation/handler.js index cf6c074e..084fbd64 100644 --- a/components/sections/arcapproximation/handler.js +++ b/components/sections/arcapproximation/handler.js @@ -1,6 +1,9 @@ var atan2 = Math.atan2, PI = Math.PI, TAU = 2*PI, cos = Math.cos, sin = Math.sin; module.exports = { + // These are functions that can be called "From the page", + // rather than being internal to the sketch. This is useful + // for making on-page controls hook into the sketch code. statics: { keyHandlingOptions: { propName: "error", @@ -16,22 +19,39 @@ module.exports = { } }, + /** + * Setup up a skeleton curve that, when using its + * points for a B-spline, can form a circle. + */ setupCircle: function(api) { var curve = new api.Bezier(70,70, 140,40, 240,130); api.setCurve(curve); }, + /** + * Set up the default quadratic curve. + */ setupQuadratic: function(api) { var curve = api.getDefaultQuadratic(); api.setCurve(curve); }, + /** + * Set up the default cubic curve. + */ setupCubic: function(api) { var curve = api.getDefaultCubic(); api.setCurve(curve); api.error = 0.5; }, + /** + * Given three points, find the (only!) circle + * that passes through all three points, based + * on the fact that the perpendiculars of the + * chords between the points all cross each + * other at the center of that circle. + */ getCCenter: function(api, p1, p2, p3) { // deltas var dx1 = (p2.x - p1.x), @@ -69,15 +89,9 @@ module.exports = { // determine arc direction (cw/ccw correction) var __; if (sm || m>e) { s += TAU; } if (s>e) { __=e; e=s; s=__; } } else { - // if em || m>e) { s += TAU; } if (s>e) { __=e; e=s; s=__; } } else { - // if e