flash version here

// Covering the canvas ??

// set dimensions for new document

var w = 640; // units

var h = 480;

// instantiate raster canvas for use

var RC = new RasterCanvas(w, h, Units.PIXELS)

// set a color to use

RC.setPenColor(0, 0, 0);

// initialize variables

var opacity = .5;

var new_x = 0;

var new_y = 0;
var i = 0;
var dice3 = 0;

var a_x = 0;
var a_y = 0;

var b_x = 640;
var b_y = 0;

var c_x = 320;
var c_y = 480;


// roll the dice to decide which corner to go towards


while (i < 10000) {

dice3 = Math.random()

if ((dice3 > 0) && (dice3 < .333)) {
draw_midpoint(a_x, a_y, new_x, new_y);
}
if ((dice3 > .333) && (dice3 < .6666)) {
draw_midpoint(b_x, b_y, new_x, new_y)
}
if ((dice3 > .6666) && (dice3 < .9999)) {
draw_midpoint(c_x, c_y, new_x, new_y)
}
i++;

}

// release object referencess

RC = null;





// draw the midpoint between the old point and the corner
// make the new point the old point

function draw_midpoint(x1, y1, x2, y2) {
new_x =(x1 + x2) * .5
new_y =(y1 + y2) * .5

RC.setPixel(new_x, new_y, .4);

//C.drawLine(new_x, new_y, new_x+.001 , new_y+.001 )
//C.drawLine((1 - new_x), (1 - new_y), (1 - new_x+.001) , (1 - new_y+.001) )
}






































// Utility Object ------------------------------------





// object definition

function RasterCanvas(w, h, units) {

// public properties

this.width = w;

this.height = h;

this.penColor;

this.penWeight = 1;

this.opacity = 1.0;





// public methods

this.getPixel = function(x, y, pixelColor) {

// works on current layer

// returns color under region(x, y, x+1, y+1)

// *very* slow ..faster for dark colors.. ;P



if (this.inRange(x, this.w) && this.inRange(y, this.h)) {

// set doc mode to rgb pixels

var origUnits = preferences.rulerUnits;

preferences.rulerUnits = Units.PIXELS;

var origMode = this.D.mode;

this.D.mode = DocumentMode.RGB;

// make a 1 pixel selection to limit scope of histogram

this.D.selection.select(new Array(new Array(x, y),

new Array(x+1, y),

new Array(x+1, y+1),

new Array(x, y+1)

), SelectionType.REPLACE);

// find where in histogram is the pixel color value, per channel

var C = new Array(0,0,0);

for (var i = 0; i < 3; i++) {

// only run thru histogram if value is hiding inside

if (eval(this.D.channels[i].histogram.join("+")) > 0) {

// locate position/value of pixel within [0,255]

for (var n=0; n==0&&C[i]<256; C[i]++) {

n = this.D.channels[i].histogram[C[i]];

}

C[i]--; // undo last increment

}

}

// set color components

pixelColor.rgb.green = C[0];

pixelColor.rgb.red = C[1];

pixelColor.rgb.blue = C[2];

this.D.selection.deselect();

// restore settings

this.D.mode = origMode;

preferences.rulerUnits = origUnits;

} else {

//this.err("setPixel", "some values out of range, please double-check.");

}

}



this.setPixel = function(x, y, opacity) {

// works on current layer

// fills 1 pixel selection of region(x, y, x+1, y+1)

// uses color object passed in at opacity specified [0,100]

// rather slow



if (this.inRange(x, this.w) && this.inRange(y, this.h)) {

this.D.selection.select(new Array(new Array(x, y),

new Array(x+1, y),

new Array(x+1, y+1),

new Array(x, y+1)

), SelectionType.REPLACE);

this.D.selection.fill(this.penColor, ColorBlendMode.NORMAL, opacity*100, false);

this.D.selection.deselect();

} else {

//this.err("setPixel", "some values out of range, please double-check.");

}

}



this.setBackgroundColor = function(normR, normG, normB) {

if (this.normalizedAll(new Array(normR, normG, normB))) {

var c = this.newRgbColor(255 * normR, 255 * normG, 255 * normB);

var r = this.newRectRegion(0, 0, this.w-1, this.h-1);

this.fillRegion(this.D, r, c, 100);



} else {

this.err("setBackgroundColor", "some values out of range, please double-check.");

}

};





this.setPenColor = function(normR, normG, normB) {

if (this.normalizedAll(new Array(normR, normG, normB))) {

this.penColor = this.newRgbColor(255 * normR, 255 * normG, 255 * normB);



} else {

this.err("setPenColor", "some values out of range, please double-check.");

}

};





this.setPenWeight = function(w) {

if (w > 0) {

this.penWeight = Math.max(w, 1);



} else {

this.err("setPenWeight", "value out of range, please double-check.");

}

};





this.setOpacity = function(normO) {

if (this.normalized(normO)) {

this.opacity = normO;



} else {

this.err("setOpacity", "value out of range, please double-check.");

}

};





this.drawLine = function(x1,y1, x2,y2) {

// pseudo draw a line by filling a selection that encompasses the end points

if (this.inRangeAll(new Array(x1, x2), this.w) && this.inRangeAll(new Array(y1, y2), this.h)) {

// set a 'thickness' for the line

var d = Math.max(this.penWeight, 2) * .5;

// declare some endpoint variables to set below

var p1, p2, p3, p4;



// lines of different orientations require different ordered selection points

if (x1 == x2) {

// vertical line

if (y1 < y2) {

p1 = new Array(x1-d, y1-d);

p2 = new Array(x1+d, y1-d);

p3 = new Array(x2+d, y2+d);

p4 = new Array(x2-d, y2+d);

} else {

p1 = new Array(x1-d, y1+d);

p2 = new Array(x1+d, y1+d);

p3 = new Array(x2+d, y2-d);

p4 = new Array(x2-d, y2-d);

}



} else if (y1 == y2) {

// horizontal line

if (x1 < x2) {

p1 = new Array(x1-d, y1-d);

p2 = new Array(x2+d, y1-d);

p3 = new Array(x2+d, y2+d);

p4 = new Array(x1-d, y2+d);

} else {

p1 = new Array(x1+d, y1-d);

p2 = new Array(x2-d, y1-d);

p3 = new Array(x2-d, y2+d);

p4 = new Array(x0+d, y2+d);

}



} else if ((x1 <= x2) && (y1 <= y2)) {

// slope down to right

p1 = new Array(x1-d, y1);

p2 = new Array(x1, y1-d);

p3 = new Array(x2+d, y2);

p4 = new Array(x2, y2+d);



} else if ((x1 > x2) && (y1 <= y2)) {

// slope down to left

p1 = new Array(x1+d, y1);

p2 = new Array(x1, y1-d);

p3 = new Array(x2-d, y2);

p4 = new Array(x2, y2+d);



} else if ((x1 <= x2) && (y1 > y2)) {

// slope up to right

p1 = new Array(x1-d, y1);

p2 = new Array(x1, y1+d);

p3 = new Array(x2+d, y2);

p4 = new Array(x2, y2-d);



} else { // ((x1 > x2) && (y1 > y2))

// slope up to left

p1 = new Array(x1+d, y1);

p2 = new Array(x1, y1+d);

p3 = new Array(x2-d, y2);

p4 = new Array(x2, y2-d);

}



var r = new Array(p1, p2, p3, p4);

this.fillRegion(this.D, r, this.penColor, this.opacity*100);



} else {

//this.err("drawLine", "some values out of range, please double-check.");

}

};





// private methods

this.err = function(label, msg) {

// pops up error message

alert("ERR " + label + "() : " + msg);

};





this.inRange = function(val, limit) {

// checks that value is in range [0,1]

var ans = true;

var v = parseFloat(val);

ans = ans && (v != Number.NaN);

if (ans) {

ans = ans && ((v >= 0) && (v <= limit));

}

return ans;

};





this.inRangeAll = function(valArray, limit) {

// checks validity of all values in array

var ans = true;

for (var i = 0; i < valArray.length; i++) {

ans = ans && this.inRange(valArray[i], limit);

}

return ans;

};





this.normalized = function(val) {

// checks that value is in range [0,1]

var ans = true;

var v = parseFloat(val);

ans = ans && (v != Number.NaN);

if (ans) {

ans = ans && ((v >= 0) && (v <= 1));

}

return ans;

};





this.normalizedAll = function(valArray) {

// checks validity of all values in array

var ans = true;

for (var i = 0; i < valArray.length; i++) {

ans = ans && this.normalized(valArray[i]);

}

return ans;

};





this.newDocument = function(width, height, unitType) {

// create and return new document of specified dimensions



// save original units setting

var origUnits = preferences.rulerUnits;

// set new units and create document with them

preferences.rulerUnits = unitType;

var D = documents.add(width, height);

// restore original units

preferences.rulerUnits = origUnits;

// return new document

return D;

};





this.newRgbColor = function(r, g, b) {

// returns SolidColor object set to rgb values given



var C = new SolidColor();



C.rgb.red = r;

C.rgb.green = g;

C.rgb.blue = b;



return C;

};



this.newRectRegion = function(L, T, R, B) {

// returns array of consecutive point arrays (a region)

// region can be specified CW or CCW



var TL = new Array(L, T);

var TR = new Array(R, T);

var BR = new Array(R, B);

var BL = new Array(L, B);



return new Array(TL, TR, BR, BL);

};





this.fillRegion = function(docRef, region, color, opacity) {

// works on current layer

// selects and fills region with color



docRef.selection.select(region, SelectionType.REPLACE);

docRef.selection.fill(color, ColorBlendMode.NORMAL, opacity*100, false);

docRef.selection.deselect();

};







// private properties

// need to occur after functions they call are defined

this.w = w;

this.h = w;

this.D = this.newDocument(w, h, units);

this.penColor = this.newRgbColor(0, 0, 0);



return this;

}