#include #include #include uchar p[256]; #define SKEW 0.211324865405 /* (3 - √3) / 6 */ #define UNSKEW 0.366025403784 /* (√3 - 1) / 2 */ double dot(int h, double x, double y){ double t; t = 0.5 - x * x - y * y; if(t > 0.0){ t *= t *= t; switch(h & 3){ case 0: return t * (x + y); case 1: return t * (y - x); case 2: return t * (x - y); case 3: return t * (-x - y); } } return 0.0; } double simplex(double x, double y){ double t; int xgty, i, j; t = (x + y) * UNSKEW; i = (int)(x + t); j = (int)(y + t); t = (i + j) * SKEW; x += t - i; y += t - j; xgty = x > y; return 70.0 * (dot(p[i + p[j & 255] & 255], x, y) + dot(p[i + xgty + p[j + !xgty & 255] & 255], x + SKEW - xgty, y + SKEW - !xgty) + dot(p[i + 1 + p[j + 1 & 255] & 255], x + SKEW + SKEW - 1.0, y + SKEW + SKEW - 1.0)); } void loadgrey(Image *i, double t){ uchar bgr[3]; bgr[1] = bgr[2] = 128 + 128 * t; bgr[0] = 220 + 35 * t; loadimage(i, i->r, bgr, 3); } void shuffle(void){ int i, j, k; srand(truerand()); for(i = 0; i < 256; i++) p[i] = i; while(i){ j = nrand(i--); k = p[j]; p[j] = p[i]; p[i] = k; } } void main(int, char**){ Image *px; Rectangle r; double x, y; if(initdraw(nil, nil, nil) < 0) sysfatal("initdraw: %r"); px = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, DNofill); if(px == nil) sysfatal("allocimage: %r"); shuffle(); y = 0.0; r.min.y = screen->r.min.y; while(r.min.y < screen->r.max.y){ r.max.y = r.min.y + 1; x = 0.0; r.min.x = screen->r.min.x; while(r.min.x < screen->r.max.x){ r.max.x = r.min.x + 1; loadgrey(px, simplex(x, y) * 0.5 + simplex(x * 2.0, y * 2.0) * 0.25 + simplex(x * 4.0, y * 4.0) * 0.125 + simplex(x * 8.0, y * 8.0) * 0.0625 ); draw(screen, r, px, nil, ZP); r.min.x = r.max.x; x += 0.003; } r.min.y = r.max.y; y += 0.003; } flushimage(display, 1); sleep(999999); }