Refactoring a javascript array rotate function
Posted
by ideotop
on Stack Overflow
See other posts from Stack Overflow
or by ideotop
Published on 2010-04-04T17:20:38Z
Indexed on
2010/04/04
17:23 UTC
Read the original article
Hit count: 314
JavaScript
I wrote a rotate function, but I'm not satisfied with it:
var pixels=['011','110','000'];
var result=new Array();
result=['000','000','000'];
for ( y=0; y<(pixels.length); y++ ){
for ( x=0; x<(pixels.length); x++ ){
var newx=0,newy=0;
if ( clock ){
if ( x< r.x && y< r.y ) {newx=x+2;newy=y ;}//top left
if ( x==r.x && y< r.y ) {newx=x+1;newy=y+1;}//top
if ( x> r.x && y< r.y ) {newx=x ;newy=y+2;}//top right
if ( x< r.x && y==r.y ) {newx=x+1;newy=y-1;}//left
if ( x==r.x && y==r.y ) {newx=x ;newy=y ;}//center
if ( x> r.x && y==r.y ) {newx=x-1;newy=y+1;}//right
if ( x< r.x && y> r.y ) {newx=x ;newy=y-2;}//bottom left
if ( x==r.x && y> r.y ) {newx=x-1;newy=y-1;}//bottom
if ( x> r.x && y> r.y ) {newx=x-2;newy=y ;}//bottom right
} else {
if ( x< r.x && y< r.y ) {newx=x ;newy=y+2;}//top left
if ( x==r.x && y< r.y ) {newx=x-1;newy=y+1;}//top
if ( x> r.x && y< r.y ) {newx=x-2;newy=y ;}//top right
if ( x< r.x && y==r.y ) {newx=x+1;newy=y+1;}//left
if ( x==r.x && y==r.y ) {newx=x ;newy=y ;}//center
if ( x> r.x && y==r.y ) {newx=x-1;newy=y-1;}//right
if ( x< r.x && y> r.y ) {newx=x+2;newy=y ;}//bottom left
if ( x==r.x && y> r.y ) {newx=x+1;newy=y-1;}//bottom
if ( x> r.x && y> r.y ) {newx=x ;newy=y-2;}//bottom right
}
//inject(result,newx,newy,pixels[y][x])
}
}
does someone now how to write a cleaner code for this rotate (clock and counter-clock) function ?
© Stack Overflow or respective owner