# Rotated Rectangle copypixels

**URL:** <https://forum.kirupa.com/t/rotated-rectangle-copypixels/212599>\
**Category:** open source\
**Created:** [January 12, 2007, 12:39pm UTC](https://forum.kirupa.com/t/rotated-rectangle-copypixels/212599 "2007-01-12T12:39:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gvozden](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/gvozden/32/1084_2.png) [@gvozden](https://forum.kirupa.com/u/gvozden)\
**Post date:** [January 12, 2007, 12:39pm UTC](https://forum.kirupa.com/t/rotated-rectangle-copypixels/212599/1 "2007-01-12T12:39:54Z")

</div>

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

```auto
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

```auto
copyRectangle[COLOR=#000000]([/COLOR]myBitmap, myBitmap2, [COLOR=#000080]100[/COLOR], [COLOR=#000080]100[/COLOR], , [COLOR=#000080]150[/COLOR], [COLOR=#000080]200[/COLOR], angle[COLOR=#000000])[/COLOR];

```

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
