class MakeScreenImage – the code

class MakeScreenImage{
  Vector[] mappedVectors;                        // needs our improved class Vector
    
  MakeScreenImage(){
  }
   
 //  often images result from two steps: first a transformation maps the pixel position
  
 Vector mapVector(float i,float j){
   Vector r=new Vector(i,height-j);
   r.mult(r).mult(0.001);
   return r;
 }
 
 //   then the color of the pixel is a function of this new position 
 
  color colorFromVector(Vector in){
   return ripples(in.x,in.y);
 }
 
 // thus essentially
 //        pixels[i+jWidth]=colorFromVector(mapVector(i,j));
 //
 // which we can accelerate using 
 // a two-step method for images with complicated geometry
 
 //  first prepare pixel coordinate vectors to the input image, 
 //  typiccally using screenCoordinate and inputCoordinate objects
 // and map the vectors

//  first step: setup the vectors

 void createMappedVectors(){
   int size=width*height;
   mappedVectors=new Vector[size]; 
   int i,j,jWidth;
   for(j=0;j<height;j++){
    jWidth=j*width;
    for (i=0;i<width;i++){
      mappedVectors[i+jWidth]=mapVector(i,j);
    }
  }  
 }
 
 //  second step: use prepared vectors to make the screen image
 
 void imageFromVectors(){
   int size=width*height;
   loadPixels();
   for(int i=0;i<size;i++){
     pixels[i]=colorFromVector(mappedVectors[i]);
   }
   updatePixels();
 }
 
 //----------------------------------------------------------------------
 //  for comparision: direct method, without preparation
 //  and for images without mappings
 
 void direct(){
  loadPixels();
  int i,j,jWidth;
  for(j=0;j<height;j++){
    jWidth=j*width;
    for (i=0;i<width;i++){
      pixels[i+jWidth]=colorFromVector(mapVector(i,j));
    }
  }  
  updatePixels();
  }
}
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