FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   rotate BImage
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: rotate BImage  (Read 525 times)
eskimoblood

222550793222550793 WWW
rotate BImage
« on: Feb 28th, 2005, 1:49pm »

Is there a way to rotate a BImage object like:
BImage a=get (0,0,20,20);
rotate(180);
set(0,0,a);
 
fjen

WWW
Re: rotate BImage
« Reply #1 on: Feb 28th, 2005, 2:31pm »

Code:
// freely rotate a BImage
 
// fjenett, feb 2005, offenbach-main, germany
// www.florianjenett.de
 
BImage img;
 
void setup() {
  size(200,200);
  img = loadImage("house.jpg");
}
 
float rotation = 0.0;
 
void loop(){
  background(0);
  image(rotateImage(img, rotation), 0, 0);
  rotation += 0.02;
}
 
BImage rotateImage (BImage _img, float _rad)
{
  if (_rad == 0) return _img;
 
  int xm = _img.width;
  int ym = _img.height;
   
  BGraphics _bimg = new BGraphics(xm, ym); _bimg.format = RGB;
  _bimg.background(255);
  _bimg.noStroke();
   
  _bimg.translate(xm/2.0, ym/2.0);
  _bimg.rotateZ(_rad);
 
  _bimg.beginShape(QUADS);
   _bimg.texture(img);  
   _bimg.vertex( -(xm/2),  -(ym/2),0,  0,0);  
   _bimg.vertex(  (xm/2),  -(ym/2),0,  xm,0);  
   _bimg.vertex(  (xm/2),   (ym/2),0,  xm,ym);  
   _bimg.vertex( -(xm/2),   (ym/2),0,  0,ym);
  _bimg.endShape();
   
  return (BImage) _bimg;
}

 
if you rotate exactly 90, 180 or 270 degree, then you can just mirror the pixels array ... i think that's faster.
 
/F
 
eskimoblood

222550793222550793 WWW
Re: rotate BImage
« Reply #2 on: Mar 1st, 2005, 12:37pm »

Here is the code to mirror an image horizontal and vertical:
 
Code:

 void setup(){
  size(702,482);
  BImage b1=loadImage("pic1.jpg");
  BImage b2=new BImage(b1.width,b1.height );
  BImage b3=new BImage(b1.width,b1.height );
  BImage b4=new BImage(b1.width,b1.height );
  for(int j=0;j<b1.width;j++){
   for(int i=0;i<b1.height;i++){
    int indexNew;
    int indexOld=i*b1.width+j;
     
    indexNew=i*b1.width+b1.width-j-1;
    b2.pixels[indexNew]=b1.pixels[indexOld];
     
    indexNew=(b1.height-i-1)*b1.width+j;
    b3.pixels[indexNew]=b1.pixels[indexOld];
     
    indexNew=b1.pixels.length-1-(i*b1.width+j);
    b4.pixels[indexNew]=b1.pixels[indexOld];
   }
  }
  image(b1,0,0);
  image(b2,b1.width,0);
  image(b3,0,b1.height);
  image(b4,b1.width,b1.height);
 }
}
 
JohnG

WWW
Re: rotate BImage
« Reply #3 on: Mar 1st, 2005, 5:11pm »

you can actually use the rotateZ command to rotate an image:
Code:

BImage MyImage=loadImage("whatever");
float angle=0;
 
void loop()
{
  angle+=0.01;
  background(0);
  translate(width/2,height/2);
  rotateZ(angle);
  image(MyImage, -MyImage.width/2, -MyImage.height/2, MyImage.width, MyImage.height);
}

 
Much easier than trying to work out the maths for yourself.
« Last Edit: Mar 1st, 2005, 5:13pm by JohnG »  
Pages: 1 

« Previous topic | Next topic »