Ricksmuseum

Interpoleren bij EllipseToSquare

Category: Globe4D, Processing, Multimedia, Tech, General — Rick Companje — 26 October 2005 @ 11:20

Van zowel Michiel als Loren kreeg ik de tip te gaan interpoleren bij mijn EllipseToSquare ‘oefening’ in plaats van pixels uit te rekken met het line commando.

Wat ik nu dus doe is alle pixels van nieuwe square langs lopen en met een formule benaderen welke kleurcode uit de ellipse daar bij hoort. Ik doe dus precies het omgekeerde dan daarvoor.

Source:
http://companje.nl/mediatech/mms/ EllipseToSquareUsingInterpolation/

Eerst deed ik dit:

void ellipseImageToSquare(float centerX, float centerY, int dx, int dy, PImage img, boolean debug, boolean showOrgImage) {
  float xOnEllipse,xOnSquare,pixelWidth;
  float radiusX=dx/2, radiusY=dy/2; //use radius instead of diameter

  //scan all horizontal lines in image  
  for (float y=-radiusY; y<radiusY; y+=1) {
  
    // calculate xpos of ellipse
    outline for certain y
    xOnEllipse = radiusX * sin(acos(y/radiusY));  

    // scan all pixels in
    // ellipse whitin one line
    for (float x=-xOnEllipse; x<xOnEllipse; x++) {
      
      // read color of pixel from
      //image and set color for drawing
      stroke(img.get(int(centerX-x),int(centerY-y)));
      
      // calculate new xpos of pixel.
      // Not in ellipse anymore but
      //in a square
      pixelWidth = radiusX / xOnEllipse;
      xOnSquare = x * pixelWidth;
      
      // draw lines to stretch
      // pixels if needed
      line(centerX-xOnSquare,centerY-y,centerX-xOnSquare-pixelWidth,centerY-y);
    }
  }  

Nu doe ik dit:

void ellipseImageToSquare(float centerX, float centerY, int dx, int dy, PImage img, boolean debug, boolean showOrgImage) {
  float xOnEllipse,pixelWidth,xInEllipse;
  float radiusX=dx/2, radiusY=dy/2; //use radius instead of diameter
  color c;
  
  //scan all horizontal lines in square  
  for (float y=-radiusY; y<radiusY; y+=1) {
  
    //calculate xpos of ellipse
    // outline for certain y
    xOnEllipse = radiusX * sin(acos(y/radiusY));
    
    //calculate width per pixel by
    //dividing radiusX by de x on the bord
    pixelWidth = radiusX / xOnEllipse;  
    
    //loop all pixels in square
    for (float x=-radiusX; x<radiusX; x++) {

      //wich pixel from the ellipse do
      // we need to get the color from       
      xInEllipse = x / pixelWidth;
      
      // read color of pixel from
      // image and set the color of
      // the pixel on screen
      c = img.get(int(centerX-xInEllipse),int(centerY-y));      
      set(int(centerX-x), int(centerY-y), c);
    }
  }

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.