From 479ccd0aa1ee54f48f1d2192a3f389faff4fa611 Mon Sep 17 00:00:00 2001 From: spew Date: Tue, 3 May 2016 09:29:39 -0500 Subject: [PATCH] add "snake" tracing effect to mole --- sys/man/1/games | 13 +++++++ sys/src/games/mole.c | 89 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 87 insertions(+), 15 deletions(-) diff --git a/sys/man/1/games b/sys/man/1/games index 24c92c2eb..2c30f6ce0 100644 --- a/sys/man/1/games +++ b/sys/man/1/games @@ -287,6 +287,19 @@ option sets the game to hard mode. Once the game has been completed, a message pops up with how long it took to win. Use the button 3 menu to choose a mode, or click to play again. .TP +.B mole +A molecular dynamics simulation based on the Lennard-Jones potential. +.L r +restarts the simulation. +.L f +redraws the screen. +.L R +reverses the simulation. +.L q +and +.L Del +quit the simulation. +.TP .B sokoban Guide Glenda through a room full of walls, pebbles and holes to put the pebbles in. Your goal is to arrange all pebbles into holes by diff --git a/sys/src/games/mole.c b/sys/src/games/mole.c index 6026929c9..f8ced68c2 100644 --- a/sys/src/games/mole.c +++ b/sys/src/games/mole.c @@ -3,7 +3,13 @@ #include #include -int N = 20, refresh = 0; +enum { + Kdel = 0x7f +}; + +int N = 49, + pathlen = 1000, + nosnake; double dt = 0.01, xmin = -40, @@ -11,7 +17,7 @@ double dt = 0.01, ymin = -40, ymax = 40, v0 = 0.1; - + #define mini(a,b) (((a)<(b))?(a):(b)) typedef struct Particle Particle; @@ -23,6 +29,11 @@ struct Particle { Image* col; }; +typedef struct Path Path; +struct Path { + int *x, *y; +}; + int colors[] = { DBlack, DRed, @@ -47,8 +58,9 @@ int colors[] = { DPurpleblue }; -Particle *A, *B; -Particle *prev, *cur; +Particle *A, *B; +Particle *prev, *cur; +Path *paths; void reset(void) @@ -69,18 +81,54 @@ reset(void) } } +void +reverse(void) +{ + Particle *p, *q; + Path *pa; + int i; + + draw(screen, screen->r, display->white, 0, ZP); + for(i=0;ix, 0, sizeof(int) * pathlen); + memset(pa->y, 0, sizeof(int) * pathlen); + p=prev+i; + q=cur+i; + p->vx = -q->vx; + p->vy = -q->vy; + p->prevx = p->x; + p->prevy = p->y; + p->x = q->x; + p->y = q->y; + } +} + +void +drawpath(Path *p, Image *col, int i) +{ + int j; + + if((j = i+1) == pathlen) + j = 0; + draw(screen, Rect(p->x[i], p->y[i], p->x[i]+1, p->y[i]+1), col, 0, ZP); + if(nosnake) + return; + draw(screen, Rect(p->x[j], p->y[j], p->x[j]+1, p->y[j]+1), display->white, 0, ZP); +} + void usage(void) { print("USAGE: mole options\n"); - print(" -N number of particles [20]\n"); + print(" -N number of particles [49]\n"); print(" -x left boundary [-40]\n"); print(" -X right boundary [40]\n"); print(" -y top boundary [-40]\n"); print(" -Y bottom boundary [40]\n"); print(" -t time step [0.01]\n"); print(" -v maximum start velocity [0.1]\n"); - print(" -F clear every frames (0:off) [0]\n"); + print(" -P path length [1000]\n"); exits("usage"); } @@ -89,13 +137,14 @@ main(int argc, char** argv) { int i, j; Particle *p, *q; + Path *pa; double dx, dx1, dx2, dy, dy1, dy2, R, F; char* f; #define FARG(c, v) case c: if(!(f=ARGF())) usage(); v = atof(f); break; ARGBEGIN { case 'N': if(!(f=ARGF())) usage(); N = atoi(f); break; - case 'F': if(!(f=ARGF())) usage(); refresh = atoi(f); break; + case 'P': if(!(f=ARGF())) usage(); pathlen = atoi(f); break; FARG('v', v0); FARG('x', xmin); FARG('X', xmax); @@ -105,8 +154,17 @@ main(int argc, char** argv) default: usage(); } ARGEND; + if(pathlen == 0) { + nosnake = 1; + pathlen = 1000; + } A = calloc(sizeof(Particle), N); B = calloc(sizeof(Particle), N); + paths = calloc(sizeof(Path), N); + for(pa=paths;pax = calloc(sizeof(int), pathlen); + pa->y = calloc(sizeof(int), pathlen); + } prev = A; cur = B; srand(time(0)); @@ -114,9 +172,9 @@ main(int argc, char** argv) einit(Emouse | Ekeyboard); reset(); - i=0; - while(1) { - if(refresh && ((++i)%refresh)==0) draw(screen, screen->r, display->white, 0, ZP); + for(i=0;; i++) { + if(i == pathlen) + i = 0; memset(cur, 0, sizeof(Particle) * N); for(p=prev;px = 2*p->x - p->prevx + q->ax * dt*dt; @@ -162,9 +220,9 @@ main(int argc, char** argv) if(q->y > ymax) {q->y -= ymax - ymin; q->prevy -= ymax - ymin;} if(q->y < ymin) {q->y += ymax - ymin; q->prevy += ymax - ymin;} q->col = p->col; - x = (screen->r.max.x - screen->r.min.x) * (q->x - xmin) / (xmax - xmin) + screen->r.min.x; - y = (screen->r.max.y - screen->r.min.y) * (q->y - ymin) / (ymax - ymin) + screen->r.min.y; - draw(screen, Rect(x, y, x+1, y+1), p->col, 0, ZP); + pa->x[i] = (screen->r.max.x - screen->r.min.x) * (q->x - xmin) / (xmax - xmin) + screen->r.min.x; + pa->y[i] = (screen->r.max.y - screen->r.min.y) * (q->y - ymin) / (ymax - ymin) + screen->r.min.y; + drawpath(pa, p->col, i); } Particle* tmp = prev; @@ -175,8 +233,9 @@ main(int argc, char** argv) if(ecankbd()) { switch(ekbd()) { - case 'q': exits(0); break; + case 'q': case Kdel: exits(0); break; case 'r': reset(); break; + case 'R': reverse(); break; case 'f': draw(screen, screen->r, display->white, 0, ZP); break; } }