PDA

View Full Version : Rotated Rectangle copypixels



gvozden
January 12th, 2007, 07:39 AM
Here is a function which copies pixels from one bitmapdata to another based on rotated rectangle // something like copyPixels but it is doing work with rotated rectangle
it has also optimisation for angle =0 then it uses copyPixels function


function copyRectangle(origBitmap:BitmapData, destBitmap:BitmapData, recX:Number, recY:Number, recW:Number, recH:Number, angle:Number) {
if (angle != 0) {
// degrees > radians
angle = angle*Math.PI/180;
// sin and cos for angle
sinVal = Math.sin(angle);
cosVal = Math.cos(angle);
// going through rectangle pixels
for (i=recX; i<(recX+recW); i++) {
for (j=recY; j<(recY+recH); j++) {
// getting position of original pixel / calculating rotation
Nx = (i-recX)*cosVal-(j-recY)*sinVal;
Ny = (j-recY)*cosVal+(i-recX)*sinVal;
Nx = Math.round(recX+Nx);
Ny = Math.round(recY+Ny);
// getting pixel value with alpha
colVal = origBitmap.getPixel32(Nx, Ny);
// setting pixel value with alpha
destBitmap.setPixel32(i-recX, j-recY, colVal);
}
}
} else {
// optimisation for angle 0, no rotation
destBitmap.copyPixels(origBitmap, new Rectangle(recX, recY, recW, recH), new Point(0, 0));
}
}

for some reason FLA files are big, and I've had this problem earlier so I can't upload 5mb FLA file, anyway I think using a function is easy just like

copyRectangle(myBitmap, myBitmap2, 100, 100, , 150, 200, angle);




examples are here
Angle Random (http://www.primatron.co.yu/as/bitmap_photo/) // using my algo
Angle Zero (http://www.primatron.co.yu/as/bitmap_photo/photo_square.html) // using copyPixles


you can see speed difference
and please don't pay attention of positions of miniphoto or how they come onto screen I wasn't paying attention to it

pixi
January 12th, 2007, 07:43 AM
thats great mate :) Nice work! -- p.s is that Antonio Gaudi's Segrada Familia in Barcelona?

gvozden
January 12th, 2007, 07:45 AM
yes, my favourite building in a world :)

Strength
January 12th, 2007, 11:30 AM
Great work! :D

CriTiCeRz
January 12th, 2007, 03:28 PM
That's nice but I don't know if it's me or what. but the first example the images are kind of crooked sometimes... other than that it's freaking awesome

Pasquale
January 12th, 2007, 10:22 PM
SWEEEEEEEEEEEEET!