
function koch(a) 
{
    this.init(a)
}

koch.prototype =
{
    context: null,
    //prevMouseX: null,
    //prevMouseY: null,
    //points: null,
    //count: null,
    
    initContext: function( ctx )
    {
        ctx.lineWidth = curr_line_width;
        ctx.strokeStyle = curr_color_str(0.1*curr_opacity);
    },
    
    init: function (a) 
    {
        this.context = a;
        this.initContext( a );
        //this.points = new Array();
        //this.count = 0;
    },
    
    destroy: function () { },
    strokeStart: function (b, a) 
    {
        //this.prevMouseX = b;
        //this.prevMouseY = a;
    },

    /*
    draw_line2: function( x1, y1, len, a, v )
    {
        var sin = Math.sin(a);
        var cos = Math.cos(a);
        
        var xe = x1 + sin * len;
        var ye = x1 + cos * len;
        
        if( 0 == v )
        {
            this.context.beginPath();
            this.context.moveTo(x1, y1);
            this.context.lineTo(xe, ye);
            this.context.stroke();
        }
        else
        {
            var dx = x2-xe;
            var dy = y2-ye;
            var x01 = x1 + dx * 0.33333;
            var x02 = x1 + dx * 0.66667;
            var y01 = y1 + dy * 0.33333;
            var y02 = y1 + dy * 0.66667;
            var x0m = x1 + dx / 2;
            var y0m = y1 + dy / 2;
            
            var len = Math.sqrt( dx*dx + dy*dy );
            var a = Math.atan2( dx, dy );
            
            x0m += Math.sin( a ) * len / 3;
            y0m += Math.cos( a ) * len / 3;
            
            this.draw_line( x1, y1, x01, y01, v-1 );
            this.draw_line( x01, y01, x0m, y0m, v-1 );
            this.draw_line( x0m, y0m, x02, y02, v-1 );
            this.draw_line( x02, y02, x2, y2, v-1 );
        }
    },
    */
    
    draw_line: function( x1, y1, x2, y2, v )
    {
        if( 0 == v )
        {
            this.context.beginPath();
            this.context.moveTo(x1, y1);
            this.context.lineTo(x2, y2);
            this.context.stroke();
        }
        else
        {
            var dx = x2-x1;
            var dy = y2-y1;
            var x01 = x1 + dx * 0.3333;
            var x02 = x1 + dx * 0.6667;
            var y01 = y1 + dy * 0.3333;
            var y02 = y1 + dy * 0.6667;
            var x0m = x1 + dx + dy/2;
            var y0m = y1 + dy - dx/2;
            
            /*
            var x0m = x1 + dx/2;
            var y0m = y1 + dy/2;

            var len = Math.sqrt( dx*dx + dy*dy );
            var a = Math.atan2( dx, dy );
            
            var turn = Math.PI / 4;
            
            x0m += Math.sin( a + turn ) * len / 3;
            y0m += Math.cos( a + turn ) * len / 3;
            */
            
            this.draw_line( x1, y1, x01, y01, v-1 );
            this.draw_line( x01, y01, x0m, y0m, v-1 );
            this.draw_line( x0m, y0m, x02, y02, v-1 );
            this.draw_line( x02, y02, x2, y2, v-1 );
        }
    },
    
    draw_snow: function( x, y, r, n, a, v )
    {
        var d = Math.PI*2 / n;
        var prev_a = a;
        var next_a = a + d;
        
        var i = 0;
        for( i = 0; i < n; ++i )
        {
            var x1 = x + Math.sin( prev_a ) * r;
            var x2 = x + Math.sin( next_a ) * r;
            var y1 = y + Math.cos( prev_a ) * r;
            var y2 = y + Math.cos( next_a ) * r;
            prev_a = next_a;
            next_a += d;
            
            this.draw_line( x1, y1, x2, y2, v );
        }
    },
    
    stroke: function (x, y) 
    {
        var a = Math.random() * Math.PI * 2;
        var r = Math.random() * 10 + 10;
        var n = Math.round(Math.random() * 3) + 3;
        var v = Math.round(Math.random() * 3) + 2;

        this.draw_snow( x, y, r, n, a, v );
    },

    strokeEnd: function (b, a) { }
};






function drunk_master(a) 
{
    this.init(a)
}

drunk_master.prototype =
{
    context: null,
    prevMouseX: null,
    prevMouseY: null,
    //points: null,
    //count: null,
    
    initContext: function( ctx )
    {
        ctx.lineWidth = curr_line_width;
        ctx.strokeStyle = curr_color_str(0.1*curr_opacity); 
    },
    
    init: function (a) 
    {
        this.context = a;
        this.initContext( a );
        //this.points = new Array();
        //this.count = 0;
    },
    
    destroy: function () { },
    strokeStart: function (x, y) 
    {
        this.prevMouseX = x;
        this.prevMouseY = y;
    },

    draw_line: function( x1, y1, x2, y2, turn, v )
    {
        turn = Math.random() * Math.PI * 2;
        
        var dx = x2-x1;
        var dy = y2-y1;

        if( 0 == v )
        {
            this.context.beginPath();
            this.context.moveTo(x1, y1);
            this.context.lineTo(x2, y2);
            this.context.lineTo(x2+(dx>1?dx:1), y2+(dy>1?dy:1));
            
            this.context.stroke();
        }
        else
        {
            var x01 = x1 + dx * 0.3333;
            var x02 = x1 + dx * 0.6667;
            var y01 = y1 + dy * 0.3333;
            var y02 = y1 + dy * 0.6667;
            
            //var x0m = x1 + dx - dy/2;
            //var y0m = y1 + dy - dx/2;
            
            var x0m = x1 + dx / 2;
            var y0m = y1 + dy / 2;
            
            var len = Math.sqrt( dx*dx + dy*dy );
            var a = Math.atan2( dx, dy );
            
            // turn around x0m, y0m by "turn", R = len/3
            var offset = len / 3;
            x0m += Math.sin( a + turn ) * offset;
            y0m += Math.cos( a + turn ) * offset;
            
            this.draw_line( x1,  y1,  x01, y01,  turn, v-1 );
            this.draw_line( x01, y01, x0m, y0m,  turn, v-1 );
            this.draw_line( x0m, y0m, x02, y02,  turn, v-1 );
            this.draw_line( x02, y02, x2,  y2,   turn, v-1 );
        }
    },
    
    stroke: function (x, y) 
    {
        var dx = x - this.prevMouseX;
        var dy = y - this.prevMouseY;
        var dst = Math.sqrt( dx*dx + dy*dy );

        var turn = 0;
        //var turn = Math.PI / 2;
        //var turn = Math.random() * Math.PI * 2;
        
        /*
        var max_dst = 10;
        var count = Math.round( dst / max_dst );
        
        dst /=  count;
        dx /= count;
        dy /= count;
        
        for( var i = 0; i < count; ++i )
        {
            x += dx;
            y += dy;
        }
        */
        
        var n = Math.round(Math.random() * dst / 3);
        
        if( n > 5 ) n = 5;

        this.draw_line( this.prevMouseX, this.prevMouseY, x, y, turn, n );
        
        this.prevMouseX = x;
        this.prevMouseY = y;
    },

    strokeEnd: function (b, a) { }
};
