-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwind.c
168 lines (144 loc) · 3.61 KB
/
wind.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <ncurses.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <signal.h>
#define WIND_ROWS 20
#define WIND_COLUMNS 14
#define PRESURE_AIRSPEED_CONSTANT .2
#define WIND_MOMENTUM .2
#define DELAY 8000 //frame delay
typedef struct {
double pressure;
double fx, fy;
} WindNode;
int maxx, maxy;
WindNode nodes[WIND_ROWS*WIND_COLUMNS];
int frame;
int run = 1;
void killer(int dummy){
run=0;
endwin();
}
int random_number(int min_num, int max_num);
float random_float(float min, float max);
int power(int base, unsigned int exp) ;
void update();
void debug_printNodes(){
char temp[50];
int color;
for(int y=0; y< WIND_ROWS; y++){
for(int x=0; x< WIND_COLUMNS; x++){
sprintf(temp,"%.3f", nodes[x + y*WIND_COLUMNS].fx);
color = 120 + (int)(nodes[x + y*WIND_COLUMNS].fx * 7);
if(color >= 140){
color= 139;
}else if(color<100){
color= 100;
}
attron(COLOR_PAIR(color));
mvaddstr(y*2 + 1,x*8+1,temp);
}
}
refresh();
}
void update(){
int row, col;
double diff;
frame++;
//sinusoidal inflow from left
for(int y = 0; y<WIND_ROWS; y++){
nodes[y*WIND_COLUMNS].pressure = 60 + 40*sin(frame/35.0);
}
//calculate airflow
for(int i=0; i < WIND_ROWS*WIND_COLUMNS; i++){
if(i / WIND_COLUMNS != WIND_ROWS - 1){
//verticle
diff = nodes[i].pressure - nodes[i+WIND_COLUMNS].pressure;
nodes[i].fy = (WIND_MOMENTUM * nodes[i].fy) + ((1-WIND_MOMENTUM) * diff * PRESURE_AIRSPEED_CONSTANT);
}
if(i % WIND_COLUMNS != WIND_COLUMNS - 1){
//horizontal
diff = nodes[i].pressure - nodes[i+1].pressure;
nodes[i].fx = (WIND_MOMENTUM * nodes[i].fx) + ((1-WIND_MOMENTUM) * diff * PRESURE_AIRSPEED_CONSTANT);
}else{
//right edge (emulate no presure for drain)
diff = nodes[i].pressure;
nodes[i].fx = (WIND_MOMENTUM * nodes[i].fx) + ((1-WIND_MOMENTUM) * diff * PRESURE_AIRSPEED_CONSTANT);
}
}
//sinusoidal inflow from left (THIS TEST OVERRIDES FIRST FOR LOOP)
for(int y = 0; y<WIND_ROWS; y++){
// nodes[y*WIND_COLUMNS].fx += 1 * sin(frame/500.0)
}
//update pressure
for(int i=0; i < WIND_ROWS * WIND_COLUMNS; i++){
nodes[i].pressure -= nodes[i].fy + nodes[i].fx;
if(i % WIND_COLUMNS > 0) nodes[i].pressure += nodes[i - 1].fx;
if(i >= WIND_COLUMNS) nodes[i].pressure += nodes[i - WIND_COLUMNS].fy;
}
}
int main(int arg , char *argc[]){
signal(SIGINT, killer);
initscr();
noecho();
cbreak();
nodelay(stdscr, 1);
curs_set(FALSE);
start_color();
getmaxyx(stdscr, maxy, maxx);
init_color(1,1000,1000,1000);
for(int i=0;i<40;i++){
init_color(10+i, 25*i, 0, 25*(40-i));
init_pair(100 + i, 1 , 10+ i);
}
for(int i=0; i < WIND_ROWS*WIND_COLUMNS; i++){
nodes[i].pressure = random_number(0,100);
}
while(run){
erase();
update();
debug_printNodes();
usleep(DELAY);
}
}
int random_number(int min_num, int max_num)
{
int result=0,low_num=0,hi_num=0;
if(min_num<max_num)
{
low_num=min_num;
hi_num=max_num;
}else{
low_num=max_num;
hi_num=min_num;
}
//srand(time(NULL));
result = (rand()%(hi_num-low_num))+low_num;
return result;
}
float random_float(float min_num, float max_num)
{
float result=0,low_num=0,hi_num=0;
if(min_num<max_num)
{
low_num=min_num;
hi_num=max_num;
}else{
low_num=max_num;
hi_num=min_num;
}
//srand(time(NULL));
result = ( (float) rand() / (float) RAND_MAX) * (hi_num-low_num) + low_num;
return result;
}
int power(int base, unsigned int exp) {
int i, result = 1;
for (i = 0; i < exp; i++)
result *= base;
return result;
}