1// This file is part of SmallBASIC
2//
3// plot routines
4//
5// This program is distributed under the terms of the GPL v2.0 or later
6// Download the GNU Public License (GPL) from www.gnu.org
7//
8// Copyright(C) 2000 Nicholas Christopoulos
9
10#include "common/sys.h"
11#include "common/var.h"
12#include "common/pproc.h"
13#include "common/device.h"
14#include "common/blib.h"
15
16/*
17 * PLOT3D xset, yset, zset
18 * PLOT3D xset, yset USE z_expr
19 * PLOT3D xset USE zy_expr
20 * PLOT3D USE xzy_expr
21 */
22
23/*
24 * PLOT4D [xset], [yset], [zset], [tset]
25 */
26
27//
28// PLOT xmin, xmax [, count] USE ...
29//
30void cmd_plot() {
31 var_num_t x, xmin = 0, xmax = 0, dx, xstep;
32 var_num_t *yt, *xt;
33 var_int_t count = 0, i, border;
34 bcip_t use_ip, exit_ip;
35 int prev_fgcolor = dev_fgcolor;
36 int prev_bgcolor = dev_bgcolor;
37
38 par_massget("FFi", &xmin, &xmax, &count);
39 if (prog_error) {
40 return;
41 }
42
43 // is there a use keyword ?
44 if (code_peek() == kwUSE) {
45 code_skipnext();
46 use_ip = code_getaddr();
47 exit_ip = code_getaddr();
48 } else {
49 rt_raise("PLOT: Missing USE keyword");
50 return;
51 }
52
53 // .................
54 border = dev_textwidth("00000");
55 dx = ABS(xmax - xmin);
56 if (count <= 0) {
57 count = os_graf_mx - border;
58 }
59 xstep = dx / (var_num_t) count;
60
61 yt = (var_num_t*) malloc(sizeof(var_num_t) * count);
62 xt = (var_num_t*) malloc(sizeof(var_num_t) * count);
63
64 // execute user's expression for each element
65 // get y values
66 if (use_ip != INVALID_ADDR) {
67 var_t v;
68
69 for (i = 0, x = xmin; i < count && !prog_error; i++, x += xstep) {
70 v_init(&v);
71 v_setreal(&v, x);
72 exec_usefunc(&v, use_ip);
73 xt[i] = x;
74 yt[i] = v_getreal(&v);
75 v_free(&v);
76 }
77
78 // jmp to correct location
79 code_jump(exit_ip);
80 }
81
82 if (!prog_error) {
83 // draw
84 chart_draw(0, 0, os_graf_mx, os_graf_my, yt, count, xt, count, 5
85 /* points */, 2 /* ruler */);
86// for ( i = 0; i < count; i ++ )
87// dev_setpixel(i, (yt[i] - ymin) * ystep);
88 }
89
90 free(xt);
91 free(yt);
92 dev_settextcolor(prev_fgcolor, prev_bgcolor);
93}
94