/*- * init_swirl * * Initialise things for swirling * * - win is the window to draw in */ void init_swirl(ModeInfo * mi) { Display *display = MI_DISPLAY(mi); Window window = MI_WINDOW(mi); swirlstruct *sp; /* does the swirls array exist? */ if (swirls == NULL) { int i; /* allocate an array, one entry for each screen */ if ((swirls = (swirlstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (swirlstruct))) == NULL) return; /* initialise them all */ for (i = 0; i < MI_NUM_SCREENS(mi); i++) initialise_swirl(&swirls[i]); } /* get a pointer to this swirl */ sp = &(swirls[MI_SCREEN(mi)]); sp->mi = mi; /* get window parameters */ sp->width = MI_WIDTH(mi); sp->height = MI_HEIGHT(mi); sp->depth = MI_DEPTH(mi); sp->rdepth = sp->depth; sp->visual = MI_VISUAL(mi); if (sp->depth > 16) sp->depth = 16; /* initialise image for speeding up drawing */ if (!initialise_image(display, sp)) { free_swirl(display, sp); return; } MI_CLEARWINDOW(mi); if (!sp->gc) { if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) { XColor color; #ifndef STANDALONE sp->fg = MI_FG_PIXEL(mi); sp->bg = MI_BG_PIXEL(mi); #endif sp->blackpixel = MI_BLACK_PIXEL(mi); sp->whitepixel = MI_WHITE_PIXEL(mi); if ((sp->cmap = XCreateColormap(display, window, MI_VISUAL(mi), AllocNone)) == None) { free_swirl(display, sp); return; } XSetWindowColormap(display, window, sp->cmap); (void) XParseColor(display, sp->cmap, "black", &color); (void) XAllocColor(display, sp->cmap, &color); MI_BLACK_PIXEL(mi) = color.pixel; (void) XParseColor(display, sp->cmap, "white", &color); (void) XAllocColor(display, sp->cmap, &color); MI_WHITE_PIXEL(mi) = color.pixel; #ifndef STANDALONE (void) XParseColor(display, sp->cmap, background, &color); (void) XAllocColor(display, sp->cmap, &color); MI_BG_PIXEL(mi) = color.pixel; (void) XParseColor(display, sp->cmap, foreground, &color); (void) XAllocColor(display, sp->cmap, &color); MI_FG_PIXEL(mi) = color.pixel; #endif sp->colors = (XColor *) NULL; sp->ncolors = 0; } if ((sp->gc = XCreateGC(display, MI_WINDOW(mi), (unsigned long) 0, (XGCValues *) NULL)) == None) { free_swirl(display, sp); return; } } MI_CLEARWINDOW(mi); /* Set up colour map */ sp->direction = (LRAND() & 1) ? 1 : -1; if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) { if (sp->colors != NULL) { if (sp->ncolors && !sp->no_colors) free_colors(display, sp->cmap, sp->colors, sp->ncolors); free(sp->colors); sp->colors = (XColor *) NULL; } sp->ncolors = MI_NCOLORS(mi); if (sp->ncolors < 2) sp->ncolors = 2; if (sp->ncolors <= 2) sp->mono_p = True; else sp->mono_p = False; if (sp->mono_p) sp->colors = (XColor *) NULL; else if ((sp->colors = (XColor *) malloc(sizeof (*sp->colors) * (sp->ncolors + 1))) == NULL) { free_swirl(display, sp); return; } sp->cycle_p = has_writable_cells(mi); if (sp->cycle_p) { if (MI_IS_FULLRANDOM(mi)) { if (!NRAND(8)) sp->cycle_p = False; else sp->cycle_p = True; } else { sp->cycle_p = cycle_p; } } if (!sp->mono_p) { if (!(LRAND() % 10)) make_random_colormap( #if STANDALONE display, MI_WINDOW(mi), #else mi, #endif sp->cmap, sp->colors, &sp->ncolors, True, True, &sp->cycle_p); else if (!(LRAND() % 2)) make_uniform_colormap( #if STANDALONE display, MI_WINDOW(mi), #else mi, #endif sp->cmap, sp->colors, &sp->ncolors, True, &sp->cycle_p); else make_smooth_colormap( #if STANDALONE display, MI_WINDOW(mi), #else mi, #endif sp->cmap, sp->colors, &sp->ncolors, True, &sp->cycle_p); } XInstallColormap(display, sp->cmap); if (sp->ncolors < 2) { sp->ncolors = 2; sp->no_colors = True; } else sp->no_colors = False; if (sp->ncolors <= 2) sp->mono_p = True; if (sp->mono_p) sp->cycle_p = False; } if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) { if (sp->mono_p) { sp->cur_color = MI_BLACK_PIXEL(mi); } } /* resolution starts off chunky */ sp->resolution = MIN_RES + 1; /* calculate the pixel step for this resulution */ sp->r = (1 << (sp->resolution - 1)); /* how many knots? */ sp->n_knots = random_no((unsigned int) MI_COUNT(mi) / 2) + MI_COUNT(mi) + 1; /* what type of knots? */ sp->knot_type = ALL; /* for now */ /* use two_plane mode occaisionally */ if (random_no(100) <= TWO_PLANE_PCNT) { sp->two_plane = sp->first_plane = True; sp->max_resolution = 2; } else sp->two_plane = False; /* fix the knot values */ if (!create_knots(sp)) { free_swirl(display, sp); return; } /* we are off */ sp->started = True; sp->drawing = False; }
/*--------------- Clustering by K-Mean Method ----------------------*/ bool k_Mean(int cluster_no, Double2D obs, int* obs_c, int dim, int pattern_no) { int s,t; int check; int flag,no=0,n; double dist,dmin; bool success=true; if(cluster_no > pattern_no){ obs_c[pattern_no] = random_no(cluster_no); } else{ Cluster *cl = new Cluster[cluster_no]; for(int i=0;i<cluster_no;i++){ cl[i].vec = AllocDouble_1D(dim); cl[i].acc = AllocDouble_1D(dim); } for(int i=0; i<cluster_no; i++){ cl[i].c = i; n = random_no(pattern_no); for(int j=0; j< dim; j++) cl[i].vec[j] = obs[j][n]; } flag = 0; check = 0; while(!flag && check<CHECK){ check++; for(int i=0; i<cluster_no; i++){ cl[i].patterns = 0; for(int k=0; k< dim; k++) cl[i].acc[k]= 0.0; } for(n=0; n < pattern_no; n++){ dmin=0.0; for(s=0; s<cluster_no; s++){ for(t=0, dist=0.0; t < dim; t++) dist += (obs[t][n]-cl[s].vec[t])*(obs[t][n]-cl[s].vec[t]); if(!s || (dist < dmin)){ dmin = dist; no = s; } } obs_c[n] = cl[no].c; for(int k=0; k<dim; k++) cl[no].acc[k] += obs[k][n]; cl[no].patterns++; } flag = update_cluster(cl,cluster_no,dim); } for(int i=0;i<cluster_no;i++){ FreeDouble_1D(cl[i].vec); FreeDouble_1D(cl[i].acc); } for(s=0; s<cluster_no; s++){ //printf("%d %d %f\n",s,cl[s].patterns,cl[s].vec[0]); if (cl[s].patterns==0) success=false; } delete [] cl; } return success; }
/*- * create_knots * * Initialise the array of knot * * swirl is the swirl data */ static Bool create_knots(swirlstruct *sp) { int k; Bool orbit, wheel, picasso, ray, hook; KNOT_P knot; /* create array for knots */ if (sp->knots) free(sp->knots); if ((sp->knots = (KNOT_P) calloc(sp->n_knots, sizeof (KNOT))) == NULL) { return False; } /* no knots yet */ orbit = wheel = picasso = ray = hook = False; /* what types do we have? */ if ((int) sp->knot_type & (int) ALL) { orbit = wheel = ray = hook = True; } else { if ((int) sp->knot_type & (int) ORBIT) orbit = True; if ((int) sp->knot_type & (int) WHEEL) wheel = True; if ((int) sp->knot_type & (int) PICASSO) picasso = True; if ((int) sp->knot_type & (int) RAY) ray = True; if ((int) sp->knot_type & (int) HOOK) hook = True; } /* initialise each knot */ knot = sp->knots; for (k = 0; k < sp->n_knots; k++) { /* position */ knot->x = random_no((unsigned int) sp->width); knot->y = random_no((unsigned int) sp->height); /* mass */ knot->m = random_no(MASS) + 1; /* can be negative */ if (random_no(100) > 50) knot->m *= -1; /* type */ knot->t = NONE; while (knot->t == NONE) { /* choose a random one from the types available */ switch (random_no(4)) { case 0: if (orbit) knot->t = ORBIT; break; case 1: if (wheel) knot->t = WHEEL; break; case 2: if (picasso) knot->t = PICASSO; break; case 3: if (ray) knot->t = RAY; break; case 4: if (hook) knot->t = HOOK; break; } } /* if two planes, do same for second plane */ if (sp->two_plane) { knot->T = NONE; while (knot->T == NONE || knot->T == knot->t) { /* choose a different type */ switch (random_no(4)) { case 0: if (orbit) knot->T = ORBIT; break; case 1: if (wheel) knot->T = WHEEL; break; case 2: if (picasso) knot->T = PICASSO; break; case 3: if (ray) knot->T = RAY; break; case 4: if (hook) knot->T = HOOK; break; } } } /* next knot */ knot++; } return True; }