Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // asd.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "windows.h"
- #include <time.h>
- #include <math.h>
- #include <conio.h>
- #define clamp(x, a, b) min(max(x, a), b);
- class Console
- {
- private:
- HANDLE handle;
- int width, height;
- public:
- Console() : handle(GetStdHandle(STD_OUTPUT_HANDLE)), width(80), height(25)
- {
- }
- void put(int x, int y, int ch)
- {
- if ((x < 0) || (y < 0) || (x >= width) || (y >= height))
- return;
- COORD pos;
- pos.X = x, pos.Y = y;
- SetConsoleCursorPosition(handle, pos);
- putchar(ch);
- }
- };
- Console console;
- float randf()
- {
- return (float)rand() / (float)RAND_MAX;
- }
- float signf(float x)
- {
- return (x > 0.0) ? 1.0f : (x < 0.0) ? -1.0f : 0.0f;
- }
- void turbulent_line_impl(int curX, int curY, int targetX, int targetY, float turb_coef, int ch, int guard)
- {
- console.put(curX, curY, ch);
- if ((curX == targetX) && (curY == targetY))
- return;
- enum Direction { LEFT, RIGHT, UP, DOWN, LUP, RUP, LDOWN, RDOWN, N_DIRECTIONS };
- Direction dir;
- if (guard > 0)
- {
- guard--;
- float dx = (float)(targetX - curX);
- float dy = (float)(targetY - curY);
- float raw_proba_up = max((dx != 0.0) ? -dy / fabs(dx) : signf(-dy), 0.0f);
- float raw_proba_dn = max((dx != 0.0) ? dy / fabs(dx) : signf(dy), 0.0f);
- float raw_proba_lt = max((dy != 0.0) ? -dx / fabs(dy) : signf(-dx), 0.0f);
- float raw_proba_rt = max((dy != 0.0) ? dx / fabs(dy) : signf(dx), 0.0f);
- float proba_up = raw_proba_up;
- float proba_dn = proba_up + raw_proba_dn;
- float proba_lt = proba_dn + raw_proba_lt;
- float proba_rt = proba_lt + raw_proba_rt;
- float proba_lup = proba_rt + (float)_hypot(raw_proba_lt, raw_proba_up);
- float proba_ldn = proba_lup + (float)_hypot(raw_proba_lt, raw_proba_dn);
- float proba_rup = proba_ldn + (float)_hypot(raw_proba_rt, raw_proba_up);
- float proba_sum = proba_rup + (float)_hypot(raw_proba_rt, raw_proba_dn);
- if (randf() < turb_coef * proba_sum)
- dir = (Direction)(rand() % N_DIRECTIONS);
- else
- {
- float rn = randf() * proba_sum;
- dir =
- (rn < proba_up) ? UP :
- (rn < proba_dn) ? DOWN :
- (rn < proba_lt) ? LEFT :
- (rn < proba_rt) ? RIGHT :
- (rn < proba_lup) ? LUP :
- (rn < proba_ldn) ? LDOWN :
- (rn < proba_rup) ? RUP :
- RDOWN;
- }
- } else
- {
- dir =
- (targetX > curX) ?
- (targetY > curY) ? RDOWN : (targetY < curY) ? RUP : RIGHT :
- (targetX < curX) ?
- (targetY > curY) ? LDOWN : (targetY < curY) ? LUP : LEFT :
- (targetY > curY) ? DOWN : UP;
- }
- switch (dir)
- {
- case UP: curY--; break;
- case DOWN: curY++; break;
- case LEFT: curX--; break;
- case RIGHT: curX++; break;
- case LUP: curX--, curY--; break;
- case LDOWN: curX--, curY++; break;
- case RUP: curX++, curY--; break;
- case RDOWN: curX++, curY++; break;
- }
- turbulent_line_impl(curX, curY, targetX, targetY, turb_coef, ch, guard);
- }
- void turbulent_line(int curX, int curY, int targetX, int targetY, int ch, float turb_coef = 0.3)
- {
- int guard = 5 * (1 + abs(curX - targetX) + abs(curY - targetY));
- turb_coef = 0.125f * clamp(turb_coef, 0.0f, 1.0f);
- turbulent_line_impl(curX, curY, targetX, targetY, turb_coef, ch, guard);
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- srand((unsigned int)time(0));
- const int N = 3;
- const unsigned char syms[N] = {'#', '*', '@'};
- for (int i = 0; i < N; i++)
- turbulent_line(10, 10, 60, 20, syms[i]);
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement