Note that there are some explanatory texts on larger screens.

plurals
  1. PORefactoring a javascript array rotate function
    primarykey
    data
    text
    <p>I wrote a rotate function, but I'm not satisfied with it:</p> <pre><code>var pixels=['011','110','000']; var result=new Array(); result=['000','000','000']; var r = {x:1,y:1}; //rotating point var clock = true; //clock or counter-clock rotation for ( y=0; y&lt;(pixels.length); y++ ){ for ( x=0; x&lt;(pixels.length); x++ ){ var newx=0,newy=0; if ( clock ){ if ( x&lt; r.x &amp;&amp; y&lt; r.y ) {newx=x+2;newy=y ;}//top left if ( x==r.x &amp;&amp; y&lt; r.y ) {newx=x+1;newy=y+1;}//top if ( x&gt; r.x &amp;&amp; y&lt; r.y ) {newx=x ;newy=y+2;}//top right if ( x&lt; r.x &amp;&amp; y==r.y ) {newx=x+1;newy=y-1;}//left if ( x==r.x &amp;&amp; y==r.y ) {newx=x ;newy=y ;}//center if ( x&gt; r.x &amp;&amp; y==r.y ) {newx=x-1;newy=y+1;}//right if ( x&lt; r.x &amp;&amp; y&gt; r.y ) {newx=x ;newy=y-2;}//bottom left if ( x==r.x &amp;&amp; y&gt; r.y ) {newx=x-1;newy=y-1;}//bottom if ( x&gt; r.x &amp;&amp; y&gt; r.y ) {newx=x-2;newy=y ;}//bottom right } else { if ( x&lt; r.x &amp;&amp; y&lt; r.y ) {newx=x ;newy=y+2;}//top left if ( x==r.x &amp;&amp; y&lt; r.y ) {newx=x-1;newy=y+1;}//top if ( x&gt; r.x &amp;&amp; y&lt; r.y ) {newx=x-2;newy=y ;}//top right if ( x&lt; r.x &amp;&amp; y==r.y ) {newx=x+1;newy=y+1;}//left if ( x==r.x &amp;&amp; y==r.y ) {newx=x ;newy=y ;}//center if ( x&gt; r.x &amp;&amp; y==r.y ) {newx=x-1;newy=y-1;}//right if ( x&lt; r.x &amp;&amp; y&gt; r.y ) {newx=x+2;newy=y ;}//bottom left if ( x==r.x &amp;&amp; y&gt; r.y ) {newx=x+1;newy=y-1;}//bottom if ( x&gt; r.x &amp;&amp; y&gt; r.y ) {newx=x ;newy=y-2;}//bottom right } //inject(result,newx,newy,pixels[y][x]) } } </code></pre> <p>does someone now how to write a cleaner code for this rotate (clock and counter-clock) function ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload