random

:-)
git clone read: git://git.margiolis.net/random.git
Log | Files | Refs | LICENSE

nnc.cpp (2911B)


      1 #include <algorithm>
      2 #include <cmath>
      3 #include <cstdlib>
      4 #include <ctime>
      5 #include <vector>
      6 #include <curses.h>
      7 
      8 static int ymax()
      9 {
     10 	return (getmaxy(stdscr));
     11 }
     12 
     13 static int xmax()
     14 {
     15 	return (getmaxx(stdscr));
     16 }
     17 
     18 struct Color {
     19 	int x, y;
     20 	int color;
     21 
     22 	Color(int x, int y, int color)
     23 		:x(x), y(y), color(color) {}
     24 
     25 	int dist(const Color& c)
     26 	{
     27 		int dx = this->x - c.x;
     28 		int dy = this->y - c.y;
     29 
     30 		return (std::sqrt(dx * dx + dy * dy));
     31 	}
     32 };
     33 
     34 struct Blue: public Color {
     35 	Blue(int x, int y, int color)
     36 		:Color(x, y, color) {}
     37 };
     38 
     39 struct Green: public Color {
     40 	Green(int x, int y, int color)
     41 		:Color(x, y, color) {}
     42 };
     43 
     44 struct White: public Color {
     45 	White(int x, int y, int color)
     46 		:Color(x, y, color) {}
     47 };
     48 
     49 static void
     50 cursesinit()
     51 {
     52 	if (!initscr())
     53 		exit(1);
     54 	cbreak();
     55 	noecho();
     56 	curs_set(0);
     57 	start_color();
     58 	init_pair(1, COLOR_BLUE, COLOR_BLUE);
     59 	init_pair(2, COLOR_GREEN, COLOR_GREEN);
     60 	init_pair(3, COLOR_WHITE, COLOR_WHITE);
     61 }
     62 
     63 static void
     64 makepts(std::vector<Color *>& points)
     65 {
     66 	int x, y;
     67 
     68 	for (std::size_t i = 0; i < 5; i++) {
     69 		x = std::rand() % (xmax() - 1);
     70 		y = std::rand() % (ymax() - 1);
     71 		points.push_back(new Blue(x, y, 1));	
     72 	}
     73 	for (std::size_t i = 0; i < 5; i++) {
     74 		x = std::rand() % (xmax() - 1);
     75 		y = std::rand() % (ymax() - 1);
     76 		points.push_back(new Green(x, y, 2));	
     77 	}
     78 }
     79 
     80 static void
     81 makewts(std::vector<White *>& whites)
     82 {
     83 	int x, y;
     84 
     85 	for (std::size_t i = 0; i < 5; i++) {
     86 		x = std::rand() % (xmax() - 1);
     87 		y = std::rand() % (ymax() - 1);
     88 		whites.push_back(new White(x, y, 3));	
     89 	}
     90 }
     91 
     92 template<typename T> static void
     93 print(const std::vector<T *>& vec)
     94 {
     95 	for (auto& v : vec) {
     96 		attron(COLOR_PAIR(v->color));
     97 		mvaddch(v->y, v->x, ACS_CKBOARD);
     98 		attroff(COLOR_PAIR(v->color));
     99 	}
    100 }
    101 
    102 static std::vector<int> 
    103 calc_dists(const std::vector<Color *>& points, const White& w)
    104 {
    105 	std::vector<int> dists; 
    106 
    107 	for (auto& point : points)
    108 		dists.push_back(point->dist(w));
    109 	return (dists);
    110 }
    111 
    112 static void
    113 find_nn(const std::vector<Color *>& points, std::vector<White *>& whites)
    114 {
    115 	std::vector<int> dists;
    116 	int mindist;
    117 
    118 	for (const auto& point : points) {
    119 		for (auto&& white : whites) {
    120 			dists = calc_dists(points, *white);
    121 			mindist = *std::min_element(dists.begin(), dists.end());
    122 			if (point->dist(*white) == mindist)
    123 				white->color = point->color;		
    124 		}
    125 	}
    126 }
    127 
    128 template<typename T> static void
    129 dealloc(std::vector<T *>& vec)
    130 {
    131 	for (auto&& v : vec)
    132 		if (v != nullptr)
    133 			delete v;
    134 	if (!vec.empty())
    135 		vec.clear();
    136 }
    137 
    138 int
    139 main(int argc, char **argv)
    140 {
    141 	std::vector<Color *> points;
    142 	std::vector<White *> whites;
    143 
    144 	cursesinit();
    145 	std::srand(std::time(nullptr));
    146 
    147 	makepts(points);
    148 	makewts(whites);
    149 
    150 	erase();
    151 	print<Color>(points);
    152 	print<White>(whites);
    153 	refresh();
    154 	getch();
    155 
    156 	find_nn(points, whites);
    157 
    158 	erase();
    159 	print<Color>(points);
    160 	print<White>(whites);
    161 	refresh();
    162 	getch();
    163 
    164 	endwin();
    165 	dealloc<Color>(points);
    166 	dealloc<White>(whites);
    167 
    168 	return (0);
    169 }