image = new SimpleImage("leaves.jpg");
image2 = new SimpleImage("paris.jpg");
image2.setSameSize(image);
maxNumberOfVertices = 20;
numberOfVertices = 6;
maxIterations = 100000; // number of points plotted
dim = getMinDimension(); // dim is minimum of image height, width
magnification = 1.0; // fraction of dim / 2 used for plot
plotRatio = .4; // fraction of distance from curr. pt. to vertex
var XSHIFT = 140;
var YSHIFT = -20;
var XMOVE = -45;
var YMOVE = -80;
var backRed = 245;
var backGreen = 245;
var backBlue = 255;
var redEnhance = 30;
var greenEnhance = 30;
var blueEnhance = 70;
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] = [0, 0, 0]; // each color is an array of 3 integers
color[1] = [15, 15, 15]; // in [0, 255] for red, green, and blue
color[2] = [25, 25, 25];
color[3] = [45, 45, 45];
color[4] = [65, 65, 65];
color[5] = [85, 85, 85];
color[6] = [105, 105, 105];
color[7] = [125, 125, 125];
color[8] = [135, 135, 135];
color[9] = [155, 155, 155];
color[10] = [160, 160, 160];
color[11] = [165, 165, 165];
color[12] = [175, 175, 175];
color[13] = [185, 185, 185];
color[14] = [205, 205, 205];
color[15] = [215, 215, 215];
color[16] = [225, 225, 225];
color[17] = [235, 235, 235];
color[18] = [245, 245, 245];
color[19] = [255, 255, 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 convertImageToSolid(r, g, b)
{
for (pixel: image) { // set RGB value to 255 for all pixels
pixel.setRed(r);
pixel.setGreen(g);
pixel.setBlue(b);
}
}
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)
{
var x = pix.getX();
var y = pix.getY();
if (x >= 0 && x < image.width &&
y >= 0 && y < image.height &&
x >= 0 && x < image2.width &&
y >= 0 && y < image2.height )
{
pix2 = image2.getPixel( x, y );
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
}
function transferPixels(image1, xs1, ys1, xe1, ye1, image2, xs2, ys2)
{
for (x = 0; x < xe1 - xs1; x++) {
for (y = 0; y < ye1 - ys1; y++) {
if (x >= 0 && x < image1.width &&
y >= 0 && y < image1.height &&
x >= 0 && x < image2.width &&
y >= 0 && y < image2.height )
{
pix = image1.getPixel(x + xs1, y + ys1);
r1 = pix.getRed();
g1 = pix.getGreen();
b1 = pix.getBlue();
pix2 = image2.getPixel(x + xs2, y + ys2);
r2 = pix2.getRed();
g2 = pix2.getGreen();
b2 = pix2.getBlue();
pix2.setRed( pix.getRed() );
pix2.setGreen( pix.getGreen() );
pix2.setBlue( pix.getBlue() );
}
}
}
}
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) );
if (pixelX + XSHIFT >= 0 && pixelX + XSHIFT < image.width &&
pixelY + YSHIFT >= 0 && pixelY + YSHIFT < image.height )
{
pixel = image.getPixel(pixelX + XSHIFT, pixelY + YSHIFT );
drawPixel(pixel, vert);
}
}
print(image);
/*
transferPixels(image, 350, 10, 630, image.height, image2, 380, 10);
print(image2);
transferPixels(image, 0, 100, 150, image.height/2, image, image.width/10, image.height/4);
print(image);
*/