diff options
-rw-r--r-- | dat.h | 14 | ||||
-rw-r--r-- | fns.h | 8 | ||||
-rw-r--r-- | main.c | 11 | ||||
-rw-r--r-- | mkfile | 1 | ||||
-rw-r--r-- | stats.c | 18 |
5 files changed, 42 insertions, 10 deletions
@@ -1,11 +1,21 @@ typedef struct State State; typedef struct Derivative Derivative; +typedef struct Stats Stats; + +struct Stats +{ + double cur; + double total; + double min, avg, max; + uvlong nupdates; + + void (*update)(Stats*, double); +}; struct State { double x, v; - double acc, min, max, avg; - int nsteps; + Stats stats; }; struct Derivative @@ -1,3 +1,11 @@ +/* + * alloc + */ void *emalloc(ulong); void *erealloc(void*, ulong); Image *eallocimage(Display*, Rectangle, ulong, int, ulong); + +/* + * stats + */ +void statsupdate(Stats*, double); @@ -13,9 +13,6 @@ State state; double t, Δt; -double min(double a, double b) { return a < b? a: b; } -double max(double a, double b) { return a > b? a: b; } - /* * Dynamics stepper * @@ -153,7 +150,7 @@ redraw(void) draw(screen, screen->r, display->black, nil, ZP); drawtimestep(t); - drawbar(state.min); drawbar(state.max); drawbar(state.avg); + drawbar(state.stats.min); drawbar(state.stats.max); drawbar(state.stats.avg); fillellipse(screen, toscreen(Pt2(0,state.x,1)), 2, 2, display->white, ZP); flushimage(display, 1); @@ -176,6 +173,7 @@ resetsim(void) { memset(&state, 0, sizeof(State)); state.x = 100; + state.stats.update = statsupdate; t = 0; } @@ -275,10 +273,7 @@ threadmain(int argc, char *argv[]) integrate(&state, t, Δt); - state.acc += state.x; - state.avg = state.acc/++state.nsteps; - state.min = min(state.min, state.x); - state.max = max(state.max, state.x); + state.stats.update(&state.stats, state.x); redraw(); @@ -4,6 +4,7 @@ BIN=/$objtype/bin/games TARG=physics OFILES=\ alloc.$O\ + stats.$O\ main.$O\ HFILES=\ @@ -0,0 +1,18 @@ +#include <u.h> +#include <libc.h> +#include <draw.h> +#include "dat.h" +#include "fns.h" + +static double min(double a, double b) { return a < b? a: b; } +static double max(double a, double b) { return a > b? a: b; } + +void +statsupdate(Stats *s, double n) +{ + s->cur = n; + s->total += s->cur; + s->avg = s->total/++s->nupdates; + s->min = min(s->cur, s->min); + s->max = max(s->cur, s->max); +} |