From ef83b9d6c26c443f34f5f81cdcf1d2b596e822e5 Mon Sep 17 00:00:00 2001 From: Ian C Date: Wed, 4 Jan 2017 14:08:46 +0000 Subject: Added follow demo. --- xd.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/xd.c b/xd.c index 7246c5c..e544851 100644 --- a/xd.c +++ b/xd.c @@ -51,6 +51,7 @@ static const char id[]="$Id$"; #endif #define RAD(x) ((x)*M_PI/180.0) +#define DEG(x) ((x)/(M_PI/180.0)) #define MIN(a,b) ((a)<(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b)) @@ -163,10 +164,11 @@ static void PolyDropDemo(void); static void CraterDemo(void); static void TriangleDemo(void); static void TriangleDemo2(void); +static void FollowDemo(void); /* ---------------------------------------- GLOBAL VARS */ -#define NO_DEMOS 15 +#define NO_DEMOS 16 static int demo=NO_DEMOS-1; @@ -249,6 +251,11 @@ static DemoInfo demotbl[NO_DEMOS]= TriangleDemo2, UsesPixmap }, + { + "Following swarm", + FollowDemo, + UsesPixmap + }, }; /* ---------------------------------------- X11 VARS @@ -3648,4 +3655,99 @@ static void TriangleDemo2(void) } +/* END OF FILE */ + + +/* ---------------------------------------- FOLLOW DEMO + */ +#define FOLLOW_NUM 4000 +#define FOLLOW_SPEED 1 +#define FOLLOW_WEIGHT 0.001 + +static void FollowDemo(void) +{ + static int init=FALSE; + static double x[FOLLOW_NUM]; + static double y[FOLLOW_NUM]; + static double dx[FOLLOW_NUM]; + static double dy[FOLLOW_NUM]; + + int f,r; + int newa; + + if (!init || mouse_b&Button1Mask) + { + for(f = 0; f < FOLLOW_NUM; f++) + { + x[f] = RND(width); + y[f] = RND(height); + dx[f] = 0; + dy[f] = 0; + } + + init = TRUE; + } + + Cls(); + + for(f = 0; f < FOLLOW_NUM; f++) + { + double d_x,d_y,d; + + d_x = mouse_x - x[f]; + d_y = mouse_y - y[f]; + + if (d_x == 0) + { + d_x = 0.0001; + } + + if (d_y == 0) + { + d_y = 0.0001; + } + + if (fabs(d_x) > fabs(d_y)) + { + d = fabs(d_x); + } + else + { + d = fabs(d_y); + } + + d_x = d_x / d * FOLLOW_WEIGHT; + d_y = d_y / d * FOLLOW_WEIGHT; + + dx[f] += d_x; + dy[f] += d_y; + + if (dx[f] < -FOLLOW_SPEED) + { + dx[f] = -FOLLOW_SPEED; + } + + if (dx[f] > FOLLOW_SPEED) + { + dx[f] = FOLLOW_SPEED; + } + + if (dy[f] < -FOLLOW_SPEED) + { + dy[f] = -FOLLOW_SPEED; + } + + if (dy[f] > FOLLOW_SPEED) + { + dy[f] = FOLLOW_SPEED; + } + + x[f] += dx[f]; + y[f] += dy[f]; + + Line(x[f], y[f], x[f]-dx[f], y[f]-dy[f], red); + } +} + + /* END OF FILE */ -- cgit v1.2.3