kaleidoscope – code of the main tab

PImage inputImage,outputImage;
Coordinates screenCoordinates,inputCoordinates,outputCoordinates;
ColorLookup inputColor;
MakeScreenImage makeScreenImage;
MakeOutputImage makeOutputImage;
Kaleidoscope kaleidoscope;
int lastMouseX,lastMouseY;
boolean dragged=false;

void setup() {
  size(600, 600);
  smooth();
  readImage();
  int outputMagnification=1;                                  // no magnification for blog
                                                              //  choose large value (about 10+ ?) for printing
  outputCoordinates=new Coordinates(outputMagnification);
  screenCoordinates=new Coordinates(1);
  screenCoordinates.setUnitLength(1);
  kaleidoscope=new Kaleidoscope();    
  //  and now use your preferred kaleidoscope
  //--------------------------------------------------------------
//  kaleidoscope.chooseRectangle(50,120);
//  kaleidoscope.chooseSquare(80);
//  kaleidoscope.chooseEquiTriangle(80);
  kaleidoscope.chooseRightTriangle(100);
  //--------------------------------------------
  makeScreenImage=new MakeScreenImage();
  makeScreenImage.createMappedVectors();
}

void draw(){ 
  inputImage=getImage();
  if(inputImage!=null){
    noLoop();
    if(inputCoordinates==null) {
      inputCoordinates=new Coordinates(inputImage);  //create coordinates, only once
      inputCoordinates.setUnitLength(1);
      inputColor=new ColorLookup(inputImage);        //  create object that looks up colors
    }
    makeScreenImage.imageFromVectors();
  }
}

//  special events
//----------------------------------------------------------------------------------


void mouseMoved(){
  lastMouseX=mouseX;
  lastMouseY=mouseY;
}

void mouseReleased(){
  redraw();
}

void mouseDragged(){
  if(!dragged){
    dragged=true;
    lastMouseX=mouseX;
    lastMouseY=mouseY;
    return;
  }
  if(screenCoordinates==null) {    
    lastMouseX=mouseX;
    lastMouseY=mouseY;
    return;
  }
  if(mouseButton==LEFT) inputCoordinates.shiftInputOrigin(new Vector(mouseX-lastMouseX,-(mouseY-lastMouseY)));
  if(mouseButton==RIGHT) inputCoordinates.addAngle(atan2(-(lastMouseY-height/2),lastMouseX-width/2)-atan2(-(mouseY-height/2),mouseX-width/2));
  lastMouseX=mouseX;
  lastMouseY=mouseY;
  redraw();
}

void mouseWheel(MouseEvent event){
  float factor=1.1;
  if (event.getAmount()>0) inputCoordinates.multiplyScale(1/factor);
  else inputCoordinates.multiplyScale(factor);
  redraw();
}

void keyPressed(){
  if(key==CODED){
    float factor=1.5,delta=0.1;
    if (keyCode==UP) inputCoordinates.multiplyScale(1/factor);
    if (keyCode==DOWN) inputCoordinates.multiplyScale(factor);
    redraw();
    return;
  }
  if(key=='s'){
    if(writing){
      println(" *** writing outputImage - not finished");
      return;
    }
    println("saving");
    if(outputImage==null){
      println("create output image");
      outputImage=outputCoordinates.createPImage();
      makeOutputImage=new MakeOutputImage(outputImage);
    }
    outputCoordinates.copyTransformations(screenCoordinates);
    makeOutputImage.tent();
    println("output image finished");
    write(outputImage);
  }
}

//  this defines the image
//  needs appropriate changes in MakeOutputImage and MakeScreenImage

Vector mapping(Vector vec){
  vec=kaleidoscope.make(vec);
  return vec;
}
This entry was posted in programming and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s