-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
316 lines (262 loc) · 7.57 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
typedef char BOOL;
#define YES 1
#define NO 0
typedef struct {
unsigned char r, g, b;
} color_t;
typedef struct {
double offset;
double x_axis;
double y_axis;
double x_scale;
double y_scale;
} plot_info_t;
typedef struct {
double start;
double end;
double max;
double min;
double step;
char *frmt;
BOOL no_axes;
int window_width;
int window_height;
color_t plot_col;
color_t axes_col;
color_t back_col;
BOOL cust_bounds;
double x_tick;
double y_tick;
BOOL autotick;
BOOL plot_on_top;
BOOL discreet;
BOOL aspect;
} function_t;
function_t blank_function = {-10, 10, 0, 0, 0, NULL, NO, 600, 400, {255, 255, 255}, {255, 0 , 255}, {0, 0, 0}, NO, 0, 0, NO, NO, NO};
SDL_Surface *initSDL(char *func, int width, int height)
{
char title[80];
strcpy(title, "Plot: ");
strcat(title, func);
SDL_Init( SDL_INIT_VIDEO );
SDL_WM_SetCaption(title, NULL);
return SDL_SetVideoMode( width, height, 32, SDL_SWSURFACE );
}
color_t makeColor(unsigned char r, unsigned char g, unsigned char b)
{
color_t col = {r, g, b};
return col;
}
function_t parseArgs(int argc, char *argv[])
{
function_t func = blank_function;
func.frmt = argv[1];
int i;
for(i = 0; i < argc; i++)
{
char *curr = argv[i];
char *next = i < argc - 1 ? argv[i + 1] : NULL;
if(strcmp(curr, "--range") == 0)
sscanf(next, "%lf,%lf", &func.start, &func.end);
else if(strcmp(curr, "--bounds") == 0)
{
sscanf(next, "%lf,%lf", &func.min, &func.max);
func.cust_bounds = YES;
}
else if(strcmp(curr, "--noaxes") == 0)
func.no_axes = YES;
else if(strcmp(curr, "--plotcolor") == 0)
{
sscanf(next, "%hhu,%hhu,%hhu", &func.plot_col.r, &func.plot_col.g, &func.plot_col.b);
}
else if(strcmp(curr, "--axescolor") == 0)
{
sscanf(next, "%hhu,%hhu,%hhu", &func.axes_col.r, &func.axes_col.g, &func.axes_col.b);
}
else if(strcmp(curr, "--backcolor") == 0)
{
sscanf(next, "%hhu,%hhu,%hhu", &func.back_col.r, &func.back_col.g, &func.back_col.b);
}
else if(strcmp(curr, "--window") == 0)
sscanf(next, "%d,%d", &func.window_width, &func.window_height);
else if(strcmp(curr, "--xtick") == 0)
func.x_tick = atof(next);
else if(strcmp(curr, "--ytick") == 0)
func.y_tick = atof(next);
else if(strcmp(curr, "--autotick") == 0)
func.autotick = YES;
else if(strcmp(curr, "--plotontop") == 0)
func.plot_on_top = YES;
else if(strcmp(curr, "--discreet") == 0)
func.discreet = YES;
else if(strcmp(curr, "--aspect") == 0)
func.aspect = YES;
}
func.step = (func.end - func.start) / func.window_width;
return func;
}
int less(int a, int b)
{
return a < b ? a : b;
}
int great(int a, int b)
{
return a > b ? a : b;
}
int abs(int a)
{
return a < 0 ? a * -1 : a;
}
double greatf(double a, double b)
{
return a > b ? a : b;
}
void drawPlot(function_t *func, plot_info_t info, double values[], SDL_Surface *screen)
{
color_t plot = func->plot_col;
int i;
double orig_y_scale = ((func->max - func->min) / func->window_height);
for(i = 0; i < func->window_width; i++)
{
//int ypt = func->window_height - ((values[i] / info.y_scale) + (info.offset / info.y_scale));
int ypt = func->window_height - (values[i] / info.y_scale) - (info.offset / orig_y_scale);
int ht = 1;
if(!func->discreet)
{
//int npt = (i < func->window_width - 1 ? func->window_height - ((values[i + 1] / info.y_scale) + (info.offset / info.y_scale)) : ypt);
int npt = (i < func->window_width - 1 ? func->window_height - ((values[i + 1] / info.y_scale)) - (info.offset / orig_y_scale): ypt);
ht = abs(great(ypt, npt) - less(npt, ypt));
ht = ht == 0 ? 1 : ht;
ypt = less(npt, ypt);
}
SDL_Rect dest = {i, ypt, 1, ht};
SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, plot.r, plot.g, plot.b));
SDL_Flip(screen);
}
}
void drawTicks(function_t *func, plot_info_t info, SDL_Surface *screen)
{
color_t axes = func->axes_col;
if(func->x_tick)
{
double step = func->x_tick / info.x_scale;
double xt;
for(xt = info.x_axis; xt < func->window_width; xt += step)
{
SDL_Rect dest = {xt, info.y_axis - 2, 1, 5};
SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, axes.r, axes.g, axes.b));
}
for(xt = info.x_axis; xt >= 0; xt -= step)
{
SDL_Rect dest = {xt, info.y_axis - 2, 1, 5};
SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, axes.r, axes.g, axes.b));
}
}
if(func->y_tick)
{
double step = func->y_tick / info.y_scale;
double yt;
for(yt = info.y_axis; yt < func->window_height; yt += step)
{
SDL_Rect dest = {info.x_axis - 2, yt, 5, 1};
SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, axes.r, axes.g, axes.b));
}
for(yt = info.y_axis; yt > 0; yt -= step)
{
SDL_Rect dest = {info.x_axis - 2, yt, 5, 1};
SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, axes.r, axes.g, axes.b));
}
}
}
void drawAxes(function_t *func, plot_info_t info, SDL_Surface *screen)
{
if(func->no_axes)
return;
color_t axes = func->axes_col;
SDL_Rect x_axis_rect = {info.x_axis, 0, 1, func->window_height};
SDL_Rect y_axis_rect = {0, info.y_axis, func->window_width, 1};
SDL_FillRect(screen, &x_axis_rect, SDL_MapRGB(screen->format, axes.r, axes.g, axes.b));
SDL_FillRect(screen, &y_axis_rect, SDL_MapRGB(screen->format, axes.r, axes.g, axes.b));
}
void plotFunction(function_t *func, double values[], SDL_Surface *screen)
{
plot_info_t info;
info.offset = (func->min < 0 ? func->min * -1 : 0);
info.x_axis = (func->start < 0 && func->end > 0 ? ((-1 * func->start) / (-1 * func->start + func->end)) * func->window_width : -1);
info.y_axis = (func->min < 0 && func->max > 0 ? (func->max / (func->max + func->min * -1) * func->window_height) : -1);
info.x_scale = (func->end - func->start) / func->window_width;
info.y_scale = (func->aspect ? info.x_scale : (func->max - func->min) / func->window_height);
/*
if(func->aspect)
info.offset = info.x_axis;*/
color_t back = func->back_col;
SDL_Rect back_rect = {0, 0, func->window_width, func->window_height};
SDL_FillRect(screen, &back_rect, SDL_MapRGB(screen->format, back.r, back.g, back.b));
if(func->plot_on_top)
{
drawTicks(func, info, screen);
drawAxes(func, info, screen);
}
drawPlot(func, info, values, screen);
if(func->plot_on_top)
return;
drawTicks(func, info, screen);
drawAxes(func, info, screen);
}
int main(int argc, char *argv[])
{
function_t func = parseArgs(argc, argv);
putenv("SDL_VIDEO_CENTERED=1");
SDL_Surface *screen = initSDL(argv[1], func.window_width, func.window_height);
char call[400];
sprintf(call, "node ~/bin/parse.js \"%s\" %lf %lf %lf %d", func.frmt, func.start, func.end, func.step, func.window_width);
FILE *result = popen(call, "r");
double values[func.window_width];
int i;
for(i = 0; i < func.window_width; i++)
{
fscanf(result, "%lf", &values[i]);
if(func.cust_bounds)
continue;
if(values[i] > func.max)
func.max = values[i];
else if(values[i] < func.min)
func.min = values[i];
}
pclose(result);
if(!func.cust_bounds)
{
func.min -= ((func.max - func.min) / func.window_height) * 40;
func.max += ((func.max - func.min) / func.window_height) * 40;
}
if(func.autotick)
{
int tickCount = func.window_width / 20;
double x = (func.end - func.start) / tickCount;
tickCount = func.window_height / 20;
double y = (func.max - func.min) / tickCount;
double val = greatf(x, y);
func.x_tick = val;
func.y_tick = val;
}
plotFunction(&func, values, screen);
SDL_Flip(screen);
SDL_Event evt;
while(SDL_WaitEvent(&evt))
{
switch(evt.type)
{
case SDL_QUIT:
SDL_Quit();
return 0;
break;
}
}
SDL_FreeSurface(screen);
SDL_Quit();
return 0;
}