Search code examples
javascriptalgorithmdrawingspiral

What is the algorithm for storing the pixels in a spiral in JS?


What is the algorithm for storing the pixels in a spiral in JS?


Solution

  • http://www.mathematische-basteleien.de/spiral.htm

    var Spiral = function(a) {
        this.initialize(a);
    }
    
    Spiral.prototype = {
        _a: 0.5,
    
        constructor: Spiral,
    
        initialize: function( a ) {
           if (a != null) this._a = a;
        },
    
    
        /* specify the increment in radians */
        points: function( rotations, increment ) {
           var maxAngle = Math.PI * 2 * rotations;
           var points = new Array();
    
           for (var angle = 0; angle <= maxAngle; angle = angle + increment)
           {
              points.push( this._point( angle ) );
           }
    
           return points;
        },
    
        _point: function( t ) {
            var x = this._a * t * Math.cos(t);
            var y = this._a * t * Math.sin(t);
            return { X: x, Y: y };
        }
    }
    
    
    var spiral = new Spiral(0.3);
    var points = spiral.points( 2, 0.01 );
    
    plot(points);
    

    Sample implementation at http://myweb.uiowa.edu/timv/spiral.htm