Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <stdio.h> // for putchar()
- #include <math.h> // for sqrtf()
- class Vector2
- {
- friend class Dot;
- float x, y;
- public:
- Vector2() { x = y = 0.0f; }
- Vector2(float X, float Y) { x = X; y = Y; }
- void Normalize()
- {
- float m = Length();
- x = x/m;
- y = y/m;
- }
- float Length()
- {
- return sqrtf(x*x + y*y);
- }
- COORD GetConsoleCoords()
- {
- COORD c;
- c.X = (SHORT)x;
- c.Y = (SHORT)y;
- return c;
- }
- };
- class Dot
- {
- Vector2 mCoords;
- char mChar;
- public:
- Dot() : mCoords(0.0f, 0.0f), mChar('*') {}
- Dot(float x, float y) : mCoords(x, y), mChar('*') {}
- Dot(float x, float y, char c) : mCoords(x, y), mChar(c) {}
- void Render(HANDLE hConsole)
- {
- CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
- GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
- SetConsoleCursorPosition(hConsole, mCoords.GetConsoleCoords());
- putchar(mChar);
- SetConsoleCursorPosition(hConsole, consoleInfo.dwCursorPosition);
- }
- void MoveTowards(Vector2 point, int distance)
- {
- if((int)mCoords.x == (int)point.x && (int)mCoords.y == (int)point.y)
- return;
- Vector2 v(point.x-mCoords.x, point.y-mCoords.y);
- v.Normalize();
- mCoords.x += (v.x*distance);
- mCoords.y += (v.y*distance);
- }
- void MoveTowards(Dot& dot, int distance=1)
- {
- MoveTowards(dot.mCoords, distance);
- }
- };
- void ClearConsole(HANDLE hConsole)
- {
- COORD startingPos = { 0, 0 };
- DWORD dwCharsWritten;
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo( hConsole, &csbi );
- DWORD dwConsoleSize = csbi.dwSize.X * csbi.dwSize.Y;
- FillConsoleOutputCharacter( hConsole, (TCHAR)' ',
- dwConsoleSize, startingPos, &dwCharsWritten );
- GetConsoleScreenBufferInfo( hConsole, &csbi );
- FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
- dwConsoleSize, startingPos, &dwCharsWritten );
- SetConsoleCursorPosition( hConsole, startingPos );
- }
- int main()
- {
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- // starting positions and the char used to
- // render the "dot"
- Dot a(0,0,'A'), b(50,5,'B'), c(26,26,'C');
- while(true)
- {
- ClearConsole(hConsole);
- a.Render(hConsole);
- b.Render(hConsole);
- c.Render(hConsole);
- a.MoveTowards(b);
- b.MoveTowards(c);
- c.MoveTowards(a);
- Sleep(300);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement