/* * hilbert-eggx.c --- Hilbert 曲線描画 * * コンパイル: egg hilbert-eggx.c -o hilbert-eggx */ #include <stdio.h> #include <stdlib.h> /* atoi() */ #include <math.h> /* pow() */ #include <eggxlib.h> /* EGGX のウィンドウ番号 */ int win; /* ペンの現在位置 */ double cur_x, cur_y; void move2(double x, double y) { eggx_line(win, x, y, PENUP); cur_x = x; cur_y = y; } void draw2(double x, double y) { eggx_line(win, x, y, PENDOWN); cur_x = x; cur_y = y; } void rdraw2(double dx, double dy) { draw2(cur_x + dx, cur_y + dy); } void hilbert(int n, double dx, double dy, int s) { if (--n >= 0) { hilbert(n, -s * dy, s * dx, -s); rdraw2(-s * dy, s * dx); hilbert(n, dx, dy, s); rdraw2(dx, dy); hilbert(n, dx, dy, s); rdraw2(s * dy, -s * dx); hilbert(n, s * dy, -s * dx, -s); } } int main(int argc, char *argv[]) { int n; double ds; n = (argc > 1) ? atoi(argv[1]) : 3; win = eggx_gopen(657, 657); eggx_gsetbgcolor(win, "white"); eggx_gclr(win); eggx_newcolor(win, "black"); eggx_window(win, -1.0 / 8, -15.0 / 64, 9.0 / 8, 65.0 / 64); eggx_drawstr(win, 0.15, -0.2, 24, 0, "Hilbert function"); eggx_drawrect(win, 0.0, 0.0, 1.0, 1.0); ds = pow(2.0, - (double)n); move2(ds / 2, ds / 2); hilbert(n, ds, 0.0, 1); getchar(); eggx_gclose(win); return 0; }