We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
polar coordinate distortion (Read 608 times)
polar coordinate distortion
Feb 7th, 2010, 3:24am
 
Hi all,


I have been racking my brains about this but I just cannot seem to make any workable code!

I have two PImage's - source and destination - and I want to transform the source in to the blank destination. Although I could hard code it - I'd rather the function adjusts to the height and width.

I think it should be something like this - but I know I am missing something quite important!

Code:

for (int i = 0; i < des.pixels.length; i++) {
float x = i%des.width;
float y = floor(i/des.width);
float r = sqrt(pow(x,2)+pow(y,2));
float a = atan2(y,x);
int x2 = int(r*cos(radians(a))+src.width/2.0);
int y2 = int(r*sin(radians(a))+src.height/2.0);
if ( y2*src.width*x2 < src.pixels.length && y2*src.width*x2 > 0) {
des.pixels[int(y)*des.width*int(x)] = src.pixels[y2*src.width*x2];
}
}



James
Re: polar coordinate distortion
Reply #1 - Feb 7th, 2010, 12:28pm
 
Got it Smiley

*edited - just in case anyone notices!*

Code:

PImage src;


void setup() {
src = loadImage("src.jpg");
size(720, 720);
frameRate(60);
}

void draw() {

background(128);

//create destination image
PImage des = createImage(720, 720, RGB);

des.loadPixels();
src.loadPixels();

for (int x = 0; x < des.width; x++) {
for (int y = 0; y < des.height; y++) {

float r = sqrt((pow(x-des.width/2,2)+pow(y-des.width/2,2)));
float a = atan2(y-des.width/2,x-des.width/2);

int x3 = int(map(a, PI, -PI, 0, src.width));
int y3 = int(map(r, 0, des.height/2, 0, src.height));

des.set(x,y,src.get(x3,y3));
}
}

des.updatePixels();

image(des, 0, 0);

}

Page Index Toggle Pages: 1