Chaos

image = new SimpleImage("flowers.jpg");

image2 = new SimpleImage("flowers.jpg");

numberOfVertices = 4;

maxIterations = 1000; // number of points plotted

magnification = 0.8; // fraction of dim / 2 used for plot

plotRatio = 0.5; // fraction of distance from curr. pt. to vertex

maxNumberOfVertices = 20;

dim = getMinDimension(); // dim is minimum of image height, width

var XSHIFT = 0;

var YSHIFT = 20;

var backRed = 255;

var backGreen = 255;

var backBlue = 255;

xUnit = magnification * (dim / 2);

yUnit = xUnit; // number of pixels for 1 unit of distance

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 convertImageToSolid(r, g, b)

{

for (pixel: image) { // set RGB value to 255 for all pixels

pixel.setRed(r);

pixel.setGreen(g);

pixel.setBlue(b);

}

}

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, 0, 255];

color[4] = [255, 255, 0];

color[5] = [0, 255, 255];

color[6] = [255, 155, 0];

color[7] = [155, 255, 0];

color[8] = [255, 0, 155];

color[9] = [155, 0, 255];

color[10] = [0, 255, 155];

color[11] = [0, 155, 255];

color[12] = [255, 155, 155];

color[13] = [155, 255, 155];

color[14] = [155, 155, 255];

color[15] = [0, 155, 155];

color[16] = [155, 0, 155];

color[17] = [155, 155, 0];

color[18] = [155, 155, 155];

color[19] = [255, 155, 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 getNextCoordinate(curr, vert, plotRatio)

{ // add to the current coordinate the plotRatio

// of the distance to the chosen vertex

return curr + (vert - curr) * plotRatio;

}

function drawDot(image, pix, col, expand)

{

xc = pix.getX();

yc = pix.getY();

for (x = xc-expand; x <= xc + expand; x++){

for (y = yc-expand; y <= yc + expand; y++){

if (x >= 0 && x < image.width &&

y >= 0 && y < image.height )

{

pixExpanded = image.getPixel(x, y);

drawPixel(image, pixExpanded, col);

}

}

}

}

function drawPixel(image, 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 );

*/

red = color[col][0];

green = color[col][1];

blue = color[col][2];

pix.setRed(red);

pix.setGreen(green);

pix.setBlue(blue);

/*

pix2 = image2.getPixel( pix.getX(), pix.getY() );

pix.setRed( pix2.getRed() );

pix.setGreen( pix2.getGreen() );

pix.setBlue( pix2.getBlue() );

*/

}

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

}

convertImageToSolid(backRed, backGreen, backBlue);

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) );

pixel = image.getPixel(pixelX + XSHIFT, pixelY + YSHIFT );

drawPixel(image, pixel, vert);

// drawDot(image, pixel, vert, 5); // chance # of points plotted

}

print(image);