Advertisement
Guest User

Untitled

a guest
Feb 12th, 2010
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <stdio.h> // for putchar()
  3. #include <math.h> // for sqrtf()
  4.  
  5. class Vector2
  6.  {
  7.      friend class Dot;
  8.      float x, y;
  9.  
  10.  public:
  11.      Vector2() { x = y = 0.0f; }
  12.      Vector2(float X, float Y) { x = X; y = Y; }
  13.  
  14.      void Normalize()
  15.      {
  16.          float m = Length();
  17.          x = x/m;
  18.          y = y/m;
  19.      }
  20.  
  21.      float Length()
  22.      {
  23.          return sqrtf(x*x + y*y);
  24.      }
  25.  
  26.      COORD GetConsoleCoords()
  27.      {
  28.          COORD c;
  29.          c.X = (SHORT)x;
  30.          c.Y = (SHORT)y;
  31.          return c;
  32.      }
  33.  };
  34.  
  35. class Dot
  36. {
  37.     Vector2 mCoords;
  38.     char mChar;
  39.  
  40. public:
  41.     Dot() : mCoords(0.0f, 0.0f), mChar('*') {}
  42.     Dot(float x, float y) : mCoords(x, y), mChar('*') {}
  43.     Dot(float x, float y, char c) : mCoords(x, y), mChar(c) {}
  44.  
  45.     void Render(HANDLE hConsole)
  46.     {
  47.         CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
  48.         GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
  49.         SetConsoleCursorPosition(hConsole, mCoords.GetConsoleCoords());
  50.         putchar(mChar);
  51.         SetConsoleCursorPosition(hConsole, consoleInfo.dwCursorPosition);
  52.     }
  53.  
  54.        
  55.     void MoveTowards(Vector2 point, int distance)
  56.     {
  57.         if((int)mCoords.x == (int)point.x && (int)mCoords.y == (int)point.y)
  58.             return;
  59.         Vector2 v(point.x-mCoords.x, point.y-mCoords.y);
  60.         v.Normalize();
  61.         mCoords.x += (v.x*distance);
  62.         mCoords.y += (v.y*distance);
  63.     }
  64.  
  65.     void MoveTowards(Dot& dot, int distance=1)
  66.     {
  67.         MoveTowards(dot.mCoords, distance);
  68.     }
  69. };
  70.  
  71. void ClearConsole(HANDLE hConsole)
  72. {
  73.     COORD startingPos = { 0, 0 };
  74.     DWORD dwCharsWritten;
  75.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  76.     GetConsoleScreenBufferInfo( hConsole, &csbi );
  77.     DWORD dwConsoleSize = csbi.dwSize.X * csbi.dwSize.Y;
  78.  
  79.     FillConsoleOutputCharacter( hConsole, (TCHAR)' ',
  80.         dwConsoleSize, startingPos, &dwCharsWritten );
  81.  
  82.     GetConsoleScreenBufferInfo( hConsole, &csbi );
  83.     FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
  84.         dwConsoleSize, startingPos, &dwCharsWritten );
  85.  
  86.     SetConsoleCursorPosition( hConsole, startingPos );
  87. }
  88.  
  89. int main()
  90. {
  91.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  92.  
  93.     // starting positions and the char used to
  94.     // render the "dot"
  95.     Dot a(0,0,'A'), b(50,5,'B'), c(26,26,'C');
  96.     while(true)
  97.     {
  98.         ClearConsole(hConsole);
  99.         a.Render(hConsole);
  100.         b.Render(hConsole);
  101.         c.Render(hConsole);
  102.         a.MoveTowards(b);
  103.         b.MoveTowards(c);
  104.         c.MoveTowards(a);
  105.         Sleep(300);
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement