static void textbox(char **textlines) { int items = 0, i; int height, width; rect R; rect tR; int cx, cy; char *msg1 = "Click or press any key to continue"; for (items = width = 0; textlines[items]; items++) width = max(width, StringWidth(textlines[items]) + 16); width = max(width, StringWidth(msg1) + 16); Centers(&sR, &cx, &cy); height = FontHeight * (items + 1) + 16; R.Xmin = cx - width / 2; R.Xmax = R.Xmin + width - 1; R.Ymin = cy - height / 2; R.Ymax = R.Ymin + height - 1; framelines(&R); BasicCenteredBox(&tR, width, height, DARKGRAY, textlines[0], WHITE); TextAlign(alignLeft, alignTop); for (i = 1; i < items; i++) { MoveTo(tR.Xmin + 4, tR.Ymin + 8 + FontHeight * i); PenColor(WHITE); BackColor(DARKGRAY); DrawString(textlines[i]); } MoveTo(cx, tR.Ymin + 8 + FontHeight * i + 4); TextAlign(alignCenter, alignTop); DrawString(msg1); while (1) { event e; KeyEvent(true, &e); if (e.ASCII || e.ScanCode || (e.State & 0x700)) break; } WaitForNothing(); PopRect(&i); framelines(&R); }
static int closest_left(int Y) { int closest = -1; int distance = 32767; int i; for(i=0;i<main_items;i++) { int cx,cy; int dist; Centers(mainR[i],&cx,&cy); dist = abs(cy - Y); if (dist < distance) { distance = dist; closest = i; } } return closest; }
void PaintQuitButton(int inout) { rect R = QuitButtonR; int cx, cy; HideCursor(); RasterOp(zREPz); Centers(&R, &cx, &cy); PaintRadioButton(&R, inout, inout, ""); TextAlign(alignCenter, alignTop); PenColor(WHITE); BackColor(inout ? RED : DARKGRAY); MoveTo(cx, R.Ymin + 4); DrawString("Alt-X"); MoveTo(cx, R.Ymin + FontHeight + 4); DrawString("to Exit"); PushButton(&R, inout); ExtraHilite(&R, inout); if (inout) DoublePress(&R, true, RED); ShowCursor(); }
void henon_tweaker(void) { int i; char chosen_file[128]; rect R; rect tR; int last_time; int keyword; int current_item; int centerx, centery; int height, width; int row; static int lefters[] = { 1, 2, 0, 4, 3, 5, 12, 6, 7, 8, 9, 10, 11, 15, 13, 14 }; static int righters[] = { 1, 2, 0, 4, 3, 5, 7, 8, 9, 10, 11, 12, 6, 14, 15, 13 }; static int uppers[] = { 13, 14, 15, 0, 2, 3, 5, 5, 5, 5, 5, 5, 5, 6, 8, 11 }; static int downers[] = { 3, 3, 4, 5, 5, 8, 13, 13, 13, 14, 14, 15, 15, 0, 1, 2 }; our_flocktype = flocktype; our_tracetype = tracetype; henon_slider.value = fha; height = 2 + FontHeight + 8 + 2 * (3 * FontHeight / 2) + slider_height(&henon_slider) + 4 + 2 * FontHeight + 4; width = 2 * sR.Xmax / 3; HideCursor(); PushCursorPosition(); BasicCenteredBox(&tR, width, height, LIGHTGRAY, "Hnon Tweaks", BLACK); Centers(&tR, ¢erx, ¢ery); PushMouseRectLimit(&tR); items = 0; R.Xmin = tR.Xmin + 4 + StringWidth("Flock Size: ") + 4; R.Xmax = tR.Xmax - 4; R.Ymin = row = tR.Ymin + FontHeight + 8; R.Ymax = R.Ymin + FontHeight + 4; JString("Flock size: ", R.Xmin - 2, R.Ymin + 2, BLACK, LIGHTGRAY, alignRight, alignTop); CreateRadioPanel(&R, SML, flocktypeR, 3, our_flocktype); for (i = 0; i < 3; i++) bR[items++] = &flocktypeR[i]; OffsetRect(&R, 0, 3 * FontHeight / 2); R.Xmin = centerx; row = R.Ymin; JString("Trace Type: ", R.Xmin - 2, R.Ymin + 2, BLACK, LIGHTGRAY, alignRight, alignTop); CreateRadioPanel(&R, PL, tracetypeR, 2, our_tracetype); for (i = 0; i < 2; i++) bR[items++] = &tracetypeR[i]; row += 3 * FontHeight / 2; create_slider(&henon_slider, &tR, 4, row - tR.Ymin); bR[items++] = &henon_slider.bR; bR[items++] = &henon_slider.TB.nR; for (i = 0; i < 6; i++) bR[items++] = &henon_slider.zR[i]; if (!fancyflag) { R = henon_slider.tR; InsetRect(&R, 1, 1); GrayOut(&R); } row = henon_slider.tR.Ymax + FontHeight; R.Xmin = tR.Xmin + 4; R.Xmax = tR.Xmax - 4; R.Ymin = row; R.Ymax = row + FontHeight + 4; CreateRadioPanel(&R, standard_button_texts, doitR, 3, -1); for (i = 0; i < 3; i++) bR[items++] = &doitR[i]; current_item = items - 1; push(current_item, true); move_to_corner(bR[current_item]); ShowCursor(); while (1) { event e; int n = KeyEvent(false, &e); int button = (e.State & 0x700) >> 8; int X = e.CursorX; int Y = e.CursorY; int last_item = current_item; slider *current_slider = NULL; if (fancyflag && XYInRect(X, Y, &henon_slider.tR)) current_slider = &henon_slider; keyword = 0; if (n) last_time = e.Time; else { if (e.Time - last_time > 5) { if (button) n = 1; else last_time = e.Time; } } if (n) { keyword = ShiftArrows(&e); if (button == swRight) keyword = 0x1b; if (!keyword) { if (e.ASCII && e.ASCII != 0xe0) keyword = e.ASCII; else keyword = e.ScanCode << 8; } if (button == swLeft) { keyword = 0x0d; if (current_slider) { if (XYInRect(X, Y, ¤t_slider->bR)) { PushMouseRectLimit(¤t_slider->sR); while (button == swLeft) { reposition_slider_X(current_slider, X, false); KeyEvent(false, &e); button = (e.State & 0x700) >> 8; X = e.CursorX; Y = e.CursorY; } PopMouseRect(); keyword = 0; } else if (XYInRect(X, Y, ¤t_slider->sR)) { reposition_slider_X(current_slider, X, false); keyword = 0; } } }
int * KMeans(gsl_matrix *mat, int nclusters, double *bic, double *aic) { int *labels = NULL, *bestlabels = NULL, *list = NULL; int i, j, s, dim, iter, nvectors, maxiter = 1000; double xmax, dmin, d, nx, mx, best, xbic = 0; double rss = 0; gsl_vector *kmean[N], *kmean_sav[N]; gsl_vector *vec = NULL, *tmp = NULL; unsigned long int seed; gsl_rng *rx = NULL; const gsl_rng_type *T = NULL; /* random */ seed = 35521738; gsl_rng_env_setup(); T = gsl_rng_default; rx = gsl_rng_alloc(T); gsl_rng_set(rx, (unsigned long int)seed); /* alloc */ xmax = VRepnMaxValue(VDoubleRepn); dim = mat->size1; /* vector length */ nvectors = mat->size2; /* num vectors (matrix columns) */ vec = gsl_vector_calloc(dim); tmp = gsl_vector_calloc(dim); labels = (int *) VCalloc(nvectors, sizeof(int)); bestlabels = (int *) VCalloc(nvectors, sizeof(int)); list = (int *) VCalloc(nclusters, sizeof(int)); for(i = 0; i < nclusters; i++) { kmean[i] = gsl_vector_calloc(dim); kmean_sav[i] = gsl_vector_calloc(dim); } /* ini */ best = VRepnMaxValue(VDoubleRepn); /* best = 1.0e+999 !!!!!!!!!!!!!! */ for(s = 0; s < 50; s++) { /* try several starting values */ Centers(nvectors, nclusters, list, rx); for(i = 0; i < nclusters; i++) gsl_matrix_get_col(kmean[i], mat, list[i]); /* iterations */ for(iter = 0; iter < maxiter; iter++) { /* get nearest neighbour */ for(j = 0; j < nvectors; j++) { gsl_matrix_get_col(vec, mat, j); dmin = xmax; for(i = 0; i < nclusters; i++) { d = dist(kmean[i], vec); if(d < dmin) { dmin = d; labels[j] = i; } } } /* update cluster means */ for(i = 0; i < nclusters; i++) { gsl_vector_memcpy(kmean_sav[i], kmean[i]); gsl_vector_set_zero(kmean[i]); nx = 0; for(j = 0; j < nvectors; j++) { if(labels[j] != i) continue; gsl_matrix_get_col(vec, mat, j); gsl_vector_add(kmean[i], vec); nx++; } gsl_vector_scale(kmean[i], 1.0 / nx); } /* stop iterations if no significant changes occurr */ d = 0; for(i = 0; i < nclusters; i++) d += dist(kmean[i], kmean_sav[i]); if(d < 1.0e-10) break; } /* residual sum of squares, RSS */ rss = 0; for(i = 0; i < nclusters; i++) { for(j = 0; j < nvectors; j++) { if(labels[j] != i) continue; gsl_matrix_get_col(vec, mat, j); rss += dist(kmean[i], vec); } } if(rss < best) { best = rss; for(j = 0; j < nvectors; j++) bestlabels[j] = labels[j]; } } /* Bayesian information criterion (not very useful) */ nx = (double)nvectors; mx = (double)nclusters; xbic = nx * log(best) + mx * log(nx); (*bic) = log(best) + log(nx) * mx / nx; (*aic) = log(best) + 2.0 * mx / nx; (*bic) = xbic; return bestlabels; }
void create_slider_base_width(slider * s, rect * mR, int x, int y, int floater, int tinies, int frame, int numbers, int width) { rect R; int row; double range, foffset; int offset; point p; int zwidth; int zepps; int i; R.Xmin = mR->Xmin + x; R.Xmax = mR->Xmax - x; R.Ymin = mR->Ymin + y; R.Ymax = R.Ymin + slider_height(s); s->tR = R; s->bubblewidth = width; #ifndef NOFRAME /* Box the whole thing */ if (frame) { PenColor(BUTTONFRAME); FrameRect(&R); } #endif row = R.Ymin + 2; #ifndef NOTITLE if (s->title) { int cx; cx = R.Xmin + (R.Xmax - R.Xmin) / 2; PenColor(BLACK); BackColor(MENUBACK); TextAlign(alignCenter, alignTop); MoveTo(cx, row); DrawString(s->title); row += FontHeight + 4; } #else row += FontHeight + 4; #endif #ifndef NONUMBERS if (numbers) { int w = 8 * 10; R.Xmin = s->tR.Xmin + 4; R.Xmax = R.Xmin + w - 1; R.Ymin = row - FontHeight / 4; R.Ymax = R.Ymin + FontHeight + 4; s->TB.nR = R; PaintNumberBoxEntry(&s->TB, s->value, (floater) ? GS_FLOAT : GS_UNSIGNED); s->sR.Xmin = R.Xmax + 8; } else { s->sR.Xmin = s->tR.Xmin + 2; } #else s->sR.Xmin = s->tR.Xmin + 2; #endif s->sR.Xmax = s->tR.Xmax - 2; s->sR.Ymin = row; s->sR.Ymax = row + (4 * FontHeight) / 5; PenColor(BUTTONBACK); PaintRect(&s->sR); PushButton(&s->sR, true); /* * Now figure out where we want the slider piece to be. We XOR the * thing into place, to make it easier to move it. And we just paint * it by drawing a 10-wide vertical line. */ range = s->max - s->min; foffset = s->value - s->min; offset = foffset / range * (s->sR.Xmax - s->sR.Xmin); p.X = s->sR.Xmin + offset; p.X = max(s->sR.Xmin + 5, p.X); p.X = min(s->sR.Xmax - 5, p.X); R.Xmin = p.X - width / 2; R.Xmax = p.X + width / 2 - 1; R.Ymin = s->sR.Ymin + 2; R.Ymax = s->sR.Ymax - 2; while (R.Xmin < s->sR.Xmin + 2) OffsetRect(&R, 1, 0); while (R.Xmax > s->sR.Xmax - 2) OffsetRect(&R, -1, 0); s->bR = R; PenColor(LIGHTGRAY); PaintRect(&s->bR); PushButton(&s->bR, false); /* Now position and paint the value box. */ s->old_value = s->value; /* create the zed box */ #ifdef NOTINIES if (tinies) { int cx, cy; Centers(&s->sR, &cx, &cy); zwidth = (s->sR.Xmax - s->sR.Xmin - 10) / 6; row = s->tR.Ymax - FontHeight / 2 - 4; for (i = 0; i < 3; i++) { rect R1, R2; R1.Xmin = cx + i * zwidth + 2 * (i + 1); R1.Xmax = R1.Xmin + zwidth - 1; R1.Ymin = row; R1.Ymax = row + FontHeight / 2; R2.Xmax = cx - i * zwidth - 2 * (i + 1); R2.Xmin = R2.Xmax - zwidth + 1; R2.Ymin = row; R2.Ymax = row + FontHeight / 2; s->zR[3 + i] = R1; s->zR[2 - i] = R2; } zepps = -3; for (i = 0; i < 6; i++) { rect R; int cy; int zwidth3 = zwidth / 3; int zstart; int dashwidth = zwidth3 - 4; R = s->zR[i]; dashwidth = min(dashwidth, s->zR[i].Ymax - s->zR[i].Ymin - 4); Centers(&R, &cx, &cy); PenColor(8); PaintRect(&R); PushButton(&R, false); PenColor(7); switch (zepps) { case -3: case 3: zstart = cx - (3 * dashwidth / 2 + 4); dash_or_plus(zstart, cy + 1, dashwidth, zepps == 3); zstart = cx - dashwidth / 2; dash_or_plus(zstart, cy + 1, dashwidth, zepps == 3); zstart = cx + dashwidth / 2 + 4; dash_or_plus(zstart, cy + 1, dashwidth, zepps == 3); break; case -2: case 2: zstart = R.Xmin + zwidth / 2 - dashwidth - 2; dash_or_plus(zstart, cy + 1, dashwidth, zepps == 2); zstart = R.Xmin + zwidth / 2 + 2; dash_or_plus(zstart, cy + 1, dashwidth, zepps == 2); break; case -1: case 1: zstart = R.Xmin + zwidth / 2 - dashwidth / 2; dash_or_plus(zstart, cy + 1, dashwidth, zepps == 1); break; } zepps++; if (zepps == 0) zepps++; } } #endif }
void InfoBox(void) { rect tR; int row; int width = 2 * sR.Xmax / 3; int height; int cx, cy; char tbuf[128]; int i; long l = realfarcoreleft(); for (i = 0; infomsgs[i]; i++); height = (i + 5) * FontHeight + FontHeight; HideCursor(); strcpy(tbuf,PROGRAM_NAME); for(i=0;tbuf[i];i++) if (tbuf[i] == '_') tbuf[i] = ' '; BasicCenteredBox(&tR, width, height, DARKGRAY, tbuf, WHITE); Centers(&tR, &cx, &cy); row = tR.Ymin + FontHeight + 8; TextAlign(alignCenter, alignTop); MoveTo(cx, row); PenColor(WHITE); BackColor(DARKGRAY); #ifdef RELEASE sprintf(tbuf, "Version 1.00"); #else sprintf(tbuf, "Version %3.2f " __DATE__, VERSION); #endif DrawString(tbuf); row += FontHeight; for (i = 0; infomsgs[i]; i++, row += FontHeight) { MoveTo(cx, row); DrawString(infomsgs[i]); } row += FontHeight; sprintf(tbuf, "%ld bytes free", l); MoveTo(cx, row); DrawString(tbuf); row += FontHeight; MoveTo(cx, row); DrawString("Press any key or click to continue"); while (1) { event e; if (KeyEvent(false, &e)) break; } PopRect(&i); ShowCursor(); }