Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 2.31 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Draw rotated image on canvas
  2. void Image::imageApply(const Image& source,const Point& origin_dest,const Point& origin_source,const Point& direction)
  3. {
  4. Matrix22<double> R=transformRotationCreate(direction);
  5. Point source_new_size=sqrt(2)*((Point){source.widthGet(),source.heightGet()});
  6. Point blit_start=origin_dest;
  7. for(unsigned int k=0;k<source_new_size.y;k++)
  8.     {
  9.     for(unsigned int l=0;l<source_new_size.x;l++)
  10.         {
  11.         Point point_source=(Point){l,k};
  12.         Point point_dest=point_source-origin_source;
  13.         point_dest*=R;
  14.         point_dest+=blit_start;
  15.  
  16.         if(point_source.rectangleInIs((Point){0,0},(Point){source.widthGet(),source.heightGet()})
  17.             &&point_dest.rectangleInIs((Point){0,0},(Point){widthGet(),heightGet()}))
  18.             {
  19.             (*this)(point_dest)=source(point_source);
  20.             }
  21.         }
  22.     }
  23. }
  24.        
  25. template<class T>
  26. struct Matrix22
  27. {
  28. T xx;
  29. T xy;
  30. T yx;
  31. T yy;
  32. };
  33.        
  34. inline Matrix22<double> transformRotationCreate(const Vector2d<double>& direction)
  35. {
  36. return (Matrix22<double>){direction.x, -direction.y, direction.y, direction.x};
  37. }
  38.        
  39. Vector2d<T>& operator*=(const Matrix22<T>& M)
  40.     {
  41.     x=x*M.xx + y*M.xy;
  42.     y=x*M.yx + y*M.yy;
  43.     return *this;
  44.     }
  45.        
  46. Vector2d<T>& operator*=(const Matrix22<T>& M)
  47.     {
  48.     T x_old=x;  //Need to save old x value (Stupid error but anyone does so sometimes)
  49.     x=x*M.xx + y*M.xy;
  50.     y=x_old*M.yx + y*M.yy;
  51.     return *this;
  52.     }
  53.        
  54. void Image::imageApply(const Image& source,const Point& origin_dest,const Point& origin_source,const Point& direction)
  55. {
  56. Matrix22<double> R=transformRotationCreate(direction);
  57. Point blit_start=origin_dest-(Point){source.sizeMaxGet(),source.sizeMaxGet()};
  58. Point blit_end=origin_dest+(Point){source.sizeMaxGet(),source.sizeMaxGet()};
  59.  
  60. for(unsigned int k=blit_start.y;k<blit_end.y;k++)
  61.     {
  62.     for(unsigned int l=blit_start.x;l<blit_end.x;l++)
  63.         {
  64.         Point point_dest=(Point){l,k};
  65.         Point point_source=R*(point_dest - origin_dest) + origin_source;
  66.         if(point_source.rectangleInIs((Point){0,0},(Point){source.widthGet(),source.heightGet()} ))
  67.             {
  68.             float alpha_source=source(point_source).alpha;
  69.             (*this)(point_dest)=(1.0f-alpha_source)*(*this)(point_dest)
  70.                                 + alpha_source*source(point_source);
  71.             }
  72.         }
  73.     }
  74. }