#include <time.h>
#include <stdlib.h>
#include "draw.h"

extern double drand48(void);

int dx0=16, dy0=16, dx1=16, dy1=16;
struct line {int x0, y0, x1, y1; };

void brown(int min, int *x, int *dx) {
    int j = *x + *dx;
    if(j < min) {
        *dx = drand48() * 8 + 3;
        j = min;
    }
    else if(j >= xy_size) {
        *dx = -drand48() * 8 - 3;
        j = xy_size - 1;
    }
    *x = j;
}

#define N 100

struct line lines[N] = {0, 128, 255, 255};

int main(void) {
    int n=0;
    int i;
    srand48(time(NULL));
    xy_init();
    xy_factor = 4; xy_shift=85;
    while(1) {
        struct line li;
        for(i=0; i<N; i++) {
            li = lines[i];
            for(int q=0; q<5; q++) {
                xy_nextline();
                if(li.x0 > li.x1) {
                    xy_moveto(li.x0, li.y0);
                    xy_draw_line(li.x0, li.y0, li.x1, li.y1);
                } else {
                    xy_moveto(li.x1, li.y1);
                    xy_draw_line(li.x1, li.y1, li.x0, li.y0);
                }
            }
        }
        li = lines[n];
        n = (n + 1) % N;
        brown(0, &li.x0, &dx0);
        brown(0, &li.x1, &dx1);
        brown(32, &li.y0, &dy0);
        brown(32, &li.y1, &dy1);
        printf("%3d %3d %3d %3d  ", li.x0, li.y0, li.x1, li.y1);
        printf("%3d %3d %3d %3d\n", dx0, dy0, dx1, dy1);
        lines[n] = li;
        xy_flipscreen();
    }
}
