Skip to content

Polar Coordinates

polar682

polar631

import processing.video.*;
Capture video;

void setup()
{
  int w = 800;
  size(int(PI*(w/2.0)), w/2, P2D);
  video = new Capture(this, w, w, 12);
}

void draw()
{
  if (video.available())
  {
    video.read();
    video.loadPixels();

    loadPixels();
    for( int y=0; y<height; y++ )
    {
      for( int x=0; x<width; x++ )
      {
        float theta = map(x, 0, width, 0, TWO_PI);
        float radius = map( (height-1-y), 0, height, 0, video.width/2.0);
        //float radius = sin( (y/float(height))*HALF_PI ) * video.width/2.0;

        int imgX = int( cos(theta) * radius + video.width/2.0 );
        int imgY = int( sin(theta) * radius + video.height/2.0 );

        pixels[ y*width + x ] = video.pixels[ imgY*video.width + imgX ];
      }
    }
    updatePixels();
  }
}

void keyPressed()
{
  if (key==' ')
  {
    saveFrame("polar###.png");
  }
}

Whipped this up quickly for a ques­tion on the Pro­cess­ing forum, and it turned out to be fun.  I have to remem­ber to dig it out again next time the nephews visit…

Shading

Pro­cess­ing hack: You can fake nice shad­ing on flat polys by gen­er­at­ing a sim­ple shadow map and tex­tur­ing it on.  On com­plex images this looks pretty good.

Here’s a sim­ple example:

shading112

Sum­mary:

  1. Ren­der a white ellipse to a sep­a­rate PGraph­ics object
  2. Blur it out
  3. Pre-calculate uv coordinates
  4. Use tint() and tex­ture() to use the shadow map (only works in P3D?)
  5. Pass the uvs to the ver­tex() calls for a shape

Code:

void setup()
{
  size( 400, 400, P3D );
  smooth();
  background(0);
  noStroke();

  // create a texture of a white shape on a black background
  float shadingSize = 64.0;
  PGraphics shading = createGraphics( (int)shadingSize, (int)((shadingSize/2.0)*tan(PI/3.0)), JAVA2D );
  shading.beginDraw();
  shading.background(0);
  shading.smooth();
  shading.fill(255);
  float radius = (shadingSize/2.0)*tan(PI/6.0);
  shading.ellipse( shading.width/2.0, shading.height-radius, radius, radius*2.5 );
  shading.filter(BLUR, 10.0);
  shading.endDraw();

  // maps the uv coordinates
  float[][] uv = new float[3][2];
  uv[0][0] = shading.width/2.0;
  uv[0][1] = 0.0f;
  uv[1][0] = shading.width;
  uv[1][1] = shading.height;
  uv[2][0] = 0.0f;
  uv[2][1] = shading.height;

  translate( width/2, height/2 );
  int numSides = 6;

  // draw a wheel, texturing each poly
  beginShape(TRIANGLE_FAN);
  texture( shading );
  tint(0);
  vertex( 0, 0, uv[0][0], uv[0][1] );
  for (int i=0; i< numSides+1; i++ )
  {
    tint( abs(cos( i * PI/numSides )) * 255, 0, abs(sin( i * PI/numSides )) * 255 );
    vertex( cos( i * TWO_PI/numSides ) * width/2, sin( i * TWO_PI/numSides ) * width/2, uv[i%2+1][0], uv[i%2+1][1]  );
  }
  endShape(CLOSE);
}

(not sure how to get a login for the real Pro­cess­ing Hacks page…)