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

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

+1


source to share


2 answers


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

+5


source


There are several problems with this question. First, you don't really know what you are doing.

1) Javascript is not really a data carrier unless you want to transfer pixels with JSON, in which case you may need to rephrase to explicitly state this.



2) No mention of what you expect from the spiral: are we talking about a free spiral or a rigid spiral? Monocolor or gradient or range of colors? Are you looking at a curved spiral or rectangular?

3) What is the ultimate goal here? Do you want to draw the spiral directly using JS, or are you passing it somewhere else?

+3


source







All Articles