Advertisement
Drakim

Drawing Algorithm code

Jan 6th, 2012
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         var xscale:Float = (1.0 / _context.xScale);
  2.         var yscale:Float = (1.0 / _context.yScale);
  3.  
  4.         var xoffset:Int = _context.xOrigin;
  5.         var yoffset:Int = _context.yOrigin;
  6.      
  7.         var xcos:Float = Math.cos(-_context.rotation) * xscale;
  8.         var xsin:Float = Math.sin(-_context.rotation) * xscale;
  9.         var ycos:Float = Math.cos(-_context.rotation) * yscale;
  10.         var ysin:Float = Math.sin(-_context.rotation) * yscale;
  11.        
  12.         var left:Int = -_x;
  13.         var right:Int = Screen.width-_x;
  14.         var up:Int = -_y;
  15.         var down:Int = Screen.height-_y;
  16.  
  17.         var xS:Float = left*xcos-up*ysin+xoffset;
  18.         var yS:Float = left*xsin+up*ycos+yoffset;
  19.  
  20.         var i:Int;
  21.         var j:Int = up;
  22.  
  23.         var xSR:Float;
  24.         var ySR:Float;
  25.         var color:Int;
  26.         var amount1:Int=0;
  27.         var amount2:Int=0;
  28.         var amount3:Int=0;
  29.         var amount4:Int=0;
  30.  
  31.         while({xS-=ysin;yS+=ycos;++j<down;}) { // Y-Loop
  32.           // Find the START of the x loop
  33.           amount1 = Std.int(  if(xcos<0&&xS>wClip) {(wClip-xS)/xcos+1;} else if(xcos>0&&xS<0) {-xS/xcos+1;}  );
  34.           amount2 = Std.int(  if(xsin<0&&yS>hClip) {(hClip-yS)/xsin+1;} else if(xsin>0&&yS<0) {-yS/xsin+1;}  );
  35.           amount1 = ((amount1<amount2)?amount2:amount1);
  36.        
  37.           // Find the END of the x loop
  38.           amount3 = Std.int(  if(xcos>0&&xS<wClip) {(wClip-xS)/xcos+1;} else if(xcos<0&&xS>0) {-xS/xcos+1;}  );
  39.           amount4 = Std.int(  if(xsin>0&&yS<hClip) {(hClip-yS)/xsin+1;} else if(xsin<0&&yS>0) {-yS/xsin+1;}  );
  40.           amount3 = ((amount3>amount4)?amount4:amount3);
  41.          
  42.           //_context.target.drawPixel(0xFF0000,30,_y+j);
  43.           // If START is later than END then obviously we shouldn't draw this row (this is a skip-y solution)
  44.           if(amount1>amount3) {continue;} //SKIP
  45.           //_context.target.drawPixel(0xFFFF00,30,_y+j);
  46.           // Set the starting position (this is a jump-x solution)
  47.           i=left+amount1;
  48.           xSR=xS+xcos*amount1-xcos;
  49.           ySR=yS+xsin*amount1-xsin;
  50.          
  51.           // Set the ending position (this is a jump-x solution)
  52.           right=left+amount3+1;
  53.  
  54.           // Start the loop
  55.           while({xSR+=xcos;ySR+=xsin;++i<right;}) { // X-Loop
  56.           //_context.target.drawPixel(0xFFFF00,_x+i,30);
  57.             color=getPixel(Std.int(xSR)+xClip,Std.int(ySR)+yClip);  // Get the color at a transformed position
  58.             if(color!=0xFF00FF||!transparency) {_context.target.drawPixel(color,_x-1+i+_context.xOrigin,_y-1+j+_context.yOrigin);} // Set the color at an untransformed position
  59.           }
  60.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement