#include #include #include #include #define WIDTH 30 #define HEIGHT 10 #define K 3 #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5)) #define min(X, Y) ((X) < (Y) ? (X) : (Y)) int startx = 0; int starty = 0; struct list_elm { int x; int y; int len; struct list_elm* next; }; struct pt { int x; int y; }; struct list_elm* add_new_point(struct list_elm* head, MEVENT* event); void drop_into_array(struct list_elm* head, int points_x[], int points_y[]); void kmeans(struct pt centers[],struct list_elm* head); void kmeans_with_pts(struct pt centers[], int k, int len, int points_x[], int points_y[]); struct pt* find_centroid(int len, int points_x[], int points_y[]); void display_points(int len, int points_x[], int points_y[]); void print_menu(WINDOW *menu_win, int highlight); void random_point_assign(struct pt center[], int k, int len, int points_x[], int points_y[]); void group_points(struct pt centers[], int groupings[], int k, int len, int points_x[], int point_y[]); double rate_cluster(struct pt centers[],int groupings[], int k,int len,int points_x[],int points_y[]); void make_new_centers(struct pt centers[], int groupings[],int k, int len, int points_x[], int points_y[]); double dist(int x1, int y1, int x2, int y2); int min_pt(struct pt centers[], int x, int y, int k); // find index of closet center double cdf(double x, double m, double s); int main(){ WINDOW *menu_win; int highlight = 1; int choice = 0; int c; MEVENT event; // struct list_elm* curr; struct list_elm* head = NULL; initscr(); clear(); noecho(); cbreak(); /* Line buffering disabled. pass on everything */ startx = (80 - WIDTH) / 2; starty = (24 - HEIGHT) / 2; menu_win = newwin(HEIGHT, WIDTH, starty, startx); keypad(menu_win, TRUE); mvprintw(0, 0, "Use the mouse to make points. Hit space to cluster points"); refresh(); curs_set(0); //print_menu(menu_win, highlight); wrefresh(menu_win); mousemask(ALL_MOUSE_EVENTS, NULL); while(1) { c = wgetch(menu_win); switch(c) { case KEY_MOUSE: getmouse(&event); mvprintw(24,0,"Mouse at %d %d",event.x,event.y); mvprintw(event.y,event.x,"x"); head = add_new_point(head,&event); refresh(); break; case 32: clear(); mvprintw(0, 0, "Use the mouse to make points. Hit space to cluster points"); mvprintw(24,0,"Space pressed\n"); refresh(); if (head != NULL) { struct pt centers[K]; kmeans(centers,head); head = NULL; } refresh(); break; default: break; } //print_menu(menu_win, highlight); wrefresh(menu_win); if(c == 10) /* User did a choice come out of the infinite loop */ break; } clrtoeol(); refresh(); endwin(); return 0; } void kmeans(struct pt centers[],struct list_elm* head) { int len = head->len; int points_x[len]; int points_y[len]; drop_into_array(head,points_x,points_y); head = NULL; kmeans_with_pts(centers, K, len, points_x, points_y); display_points(len,points_x,points_y); start_color(); use_default_colors(); // assume_default_colors(-1,-1); init_pair(1, COLOR_RED, COLOR_WHITE); attron(COLOR_PAIR(1)); int i=0; for (i=0; ix,c->y,points_x[i],points_y[i]); } return sum; } void group_points(struct pt centers[], int groupings[], int k, int len, int points_x[], int points_y[]) { int i=0; for(i=0; ilen = 1; } else { curr->len = head->len + 1; } curr->x = event->x; curr->y = event->y; curr->next = head; return curr; } void drop_into_array(struct list_elm* head, int points_x[], int points_y[]) { struct list_elm* curr; while (head != NULL) { points_x[head->len - 1] = head->x; points_y[head->len - 1] = head->y; curr = head->next; free(head); head = curr; } } struct pt* find_centroid(int len, int points_x[], int points_y[]) { struct pt* center = malloc(sizeof(struct pt)); int i=0; center->x=0; center->y=0; for(i=0;ix += points_x[i]; center->y += points_y[i]; } center->x = round(center->x / len); center->y = round(center->y / len); return center; } void display_points(int len, int points_x[], int points_y[]) { int i=0; for(i=0;i