ChaosJavascript
//*********************************************************
image = new SimpleImage("abby.jpg");
maxNumberOfVertices = 20;
numberOfVertices = 3;
maxIterations = 100000; // number of points plotted
dim = getMinDimension(); // dim is minimum of image height, width
magnification = 0.9; // fraction of dim / 2 used for plot
plotRatio = 0.5; // fraction of distance from curr. pt. to vertex
xUnit = magnification * (dim / 2);
yUnit = xUnit; // number of pixels for 1 unit of distance
color = new Array(maxNumberOfVertices); // color for each vertex
for (rep = 0; rep < maxNumberOfVertices; rep++){
color[rep] = new Array(3);
}
color[0] = [255, 0, 0]; // each color is an array of 3 integers
color[1] = [0, 255, 0]; // in [0, 255] for red, green, and blue
color[2] = [0, 0, 255];
color[3] = [255, 111, 255];
color[4] = [138, 111, 226];
color[5] = [100, 55, 180];
color[6] = [0, 155, 255];
color[7] = [0, 255, 255];
color[8] = [155, 155, 155];
color[9] = [255, 155, 255];
color[10] = [200, 055, 155];
color[11] = [55, 55, 255];
color[12] = [55, 255, 155];
color[13] = [0, 0, 55];
color[14] = [0, 0, 55];
color[15] = [0, 0, 255];
color[16] = [0, 255, 0];
color[17] = [255, 0, 155];
color[18] = [55, 155, 155];
color[19] = [55, 55, 255];
function setVerticies() {
vertex = new Array(numberOfVertices); // array for vertex coords
for (rep = 0; rep < numberOfVertices; rep++){
vertex[rep] = new Array(2); // array of 2 coordinates per vertex
}
vertex[0][0] = 0.0; // x = x'cosT - y'sinT
vertex[0][1] = 1.0; // y = x'sinT + y'cosT
angle = 2.0 * Math.PI / numberOfVertices;
theta = angle; // rotate vertex[0] theta to obtain other vertices
for (rep = 1; rep < numberOfVertices; rep++){
vertex[rep][0] = vertex[0][0] * Math.cos(theta) - vertex[0][1] * Math.sin(theta);
vertex[rep][1] = vertex[0][0] * Math.sin(theta) + vertex[0][1] * Math.cos(theta);
theta += angle;
}
currVx = vertex[0][0]; // inital point for start of game
currVy = vertex[0][1];
nextVx = currVx;
nextVy = currVy;
}
function getMinDimension()
{
if (image.height < image.width) // use min of height and width
return image.height; // for image dimension in order
return image.width; // to fit within the given image
}
function convertImageToWhite()
{
for (pixel: image) { // set RGB value to 255 for all pixels
pixel.setRed(255);
pixel.setGreen(255);
pixel.setBlue(255);
}
}
function getNextCoordinate(next, vert, plotRatio)
{ // add to the current coordinate the plotRatio
// of the distance to the chosen vertex
return next + (vert - next) * plotRatio;
}
function drawPixel(pix, col)
{ /* create math fucntions for calculating color based on col
red = Math.floor( 3.5 * pix.getX() * (1+col) % 256 );
green = Math.floor( 6.5 * pix.getX() * (1+col) % 256 );
blue = Math.floor( 8.5 * pix.getX() * (1+col) % 256 );
pix.setRed(red);
pix.setGreen(green);
pix.setBlue(blue);
*/
pix.setRed(color[col][0]);
pix.setGreen(color[col][1]); // use preset color values
pix.setBlue(color[col][2]);
}
function convertCoordToXPixel( coord )
{ // convert coord to pixel value for creative displays
// return image.width / 2 + 1.5*Math.sqrt(coord*coord)*xUnit*coord;
return image.width / 2 + xUnit * coord; // traditional
}
function convertCoordToYPixel( coord )
{ // convert coord to pixel value for creative displays
// return image.height / 2 - 1.1*Math.sin(1.2*Math.PI*coord) * yUnit * coord;
return image.height / 2 - yUnit * coord; // traditional
}
convertImageToWhite();
setVerticies();
for (rep = 0; rep < maxIterations; rep++) {
vert = Math.floor( Math.random() * numberOfVertices );
currVx = getNextCoordinate(currVx, vertex[vert][0], plotRatio);
currVy = getNextCoordinate(currVy, vertex[vert][1], plotRatio);
pixelX = Math.floor( convertCoordToXPixel(currVx) );
pixelY = Math.floor( convertCoordToYPixel(currVy) );
if (pixelX >= 0 && pixelX < image.width && pixelY >= 0 && pixelY < image.height )
{
pixel = image.getPixel(pixelX, pixelY );
drawPixel(pixel, vert);
}
}
print(image);
//*********************************************************