8 typedef struct Operator Operator;
9 typedef struct Token Token;
10 typedef struct Constant Constant;
11 typedef struct Code Code;
48 void add(void) { sp--; *sp += *(sp+1); }
49 void sub(void) { sp--; *sp -= *(sp+1); }
50 void mul(void) { sp--; *sp *= *(sp+1); }
51 void div(void) { sp--; *sp /= *(sp+1); }
52 void mod(void) { sp--; *sp = fmod(*sp, *(sp+1)); }
53 void pot(void) { sp--; *sp = pow(*sp, *(sp+1)); }
54 void osin(void) { *sp = sin(*sp); }
55 void ocos(void) { *sp = cos(*sp); }
56 void otan(void) { *sp = tan(*sp); }
57 void oasin(void) { *sp = asin(*sp); }
58 void oacos(void) { *sp = acos(*sp); }
59 void oatan(void) { *sp = atan(*sp); }
60 void osqrt(void) { *sp = sqrt(*sp); }
61 void oexp(void) { *sp = exp(*sp); }
62 void olog(void) { *sp = log10(*sp); }
63 void oln(void) { *sp = log(*sp); }
72 "+", OBINARY, 0, 0, add,
73 "-", OBINARY, 0, 0, sub,
74 "*", OBINARY, 0, 100, mul,
75 "/", OBINARY, 0, 100, div,
76 "%", OBINARY, 0, 100, mod,
77 "^", OBINARY, 1, 200, pot,
78 "sin", OUNARY, 0, 300, osin,
79 "cos", OUNARY, 0, 300, ocos,
80 "tan", OUNARY, 0, 300, otan,
81 "asin", OUNARY, 0, 300, oasin,
82 "acos", OUNARY, 0, 300, oacos,
83 "atan", OUNARY, 0, 300, oatan,
84 "sqrt", OUNARY, 0, 300, osqrt,
85 "exp", OUNARY, 0, 300, oexp,
86 "log", OUNARY, 0, 300, olog,
87 "ln", OUNARY, 0, 300, oln,
106 double xmin = -10, xmax = 10;
107 double ymin = -10, ymax = 10;
118 Image *colors[nelem(icolors)];
122 int picx = 640, picy = 480;
124 typedef struct FRectangle FRectangle;
126 double xmin, xmax, ymin, ymax;
135 v = mallocz(size, 1);
137 sysfatal("emalloc: %r");
138 setmalloctag(v, getcallerpc(&size));
145 if(c->len >= c->cap) {
147 c->p = realloc(c->p, sizeof(Op) * c->cap);
149 sysfatal("realloc: %r");
157 t->next = opstackbot;
169 sysfatal("stack underflow");
170 opstackbot = t->next;
172 sysfatal("non-operator pop");
173 o.type = t->op->type;
185 opstackbot = t->next;
201 t = emalloc(sizeof(*t));
204 t->val = strtod(*s, s);
222 for(o = ops; o < ops + nelem(ops); o++)
223 if(strncmp(*s, o->s, strlen(o->s)) == 0) {
229 for(c = consts; c < consts + nelem(consts); c++)
230 if(strncmp(*s, c->s, strlen(c->s)) == 0) {
236 sysfatal("syntax error at %s", *s);
241 parse(Code *c, char *s)
260 if(t->op->type == OBINARY)
261 while(opstackbot != nil && opstackbot->type == TOP &&
262 (opstackbot->op->prec > t->op->prec ||
263 t->op->rassoc && opstackbot->op->prec == t->op->prec))
271 while(opstackbot != nil && opstackbot->type == TOP)
273 if(opstackbot == nil)
274 sysfatal("mismatched parentheses");
279 sysfatal("unknown token type %d", t->type);
282 while(opstackbot != nil)
283 switch(opstackbot->type) {
288 sysfatal("mismatched parentheses");
290 sysfatal("syntax error");
302 for(o = c->p; o < c->p + c->len; o++)
304 case ONUMBER: case OVAR:
310 sysfatal("syntax error");
314 sysfatal("syntax error");
320 sysfatal("syntax error");
325 calc(Code *c, double x)
330 for(o = c->p; o < c->p + c->len; o++)
338 case OUNARY: case OBINARY:
341 if(*sp < gymin) gymin = *sp;
342 if(*sp > gymax) gymax = *sp;
347 convx(Rectangle *r, int x)
349 return (xmax - xmin) * (x - r->min.x) / (r->max.x - r->min.x) + xmin;
353 deconvx(Rectangle *r, double dx)
355 return (dx - xmin) * (r->max.x - r->min.x) / (xmax - xmin) + r->min.x + 0.5;
359 convy(Rectangle *r, int y)
361 return (ymax - ymin) * (r->max.y - y) / (r->max.y - r->min.y) + ymin;
365 deconvy(Rectangle *r, double dy)
367 return (ymax - dy) * (r->max.y - r->min.y) / (ymax - ymin) + r->min.y + 0.5;
371 pixel(int x, int y, int c)
376 if(x >= picx || y >= picy || x < 0 || y < 0)
378 p = imagedata + (picx * y + x) * 3;
379 p[0] = icolors[c] >> 24;
380 p[1] = icolors[c] >> 16;
381 p[2] = icolors[c] >> 8;
383 draw(screen, Rect(x, y, x + 1, y + 1), colors[c], nil, ZP);
384 if(ptinrect(Pt(x, y), screen->r))
385 pixels[picx * (y - screen->r.min.y) + (x - screen->r.min.x)] = 1;
390 drawinter(Code *co, Rectangle *r, double x1, double x2, int n, int c)
396 ix1 = deconvx(r, x1);
397 ix2 = deconvx(r, x2);
401 iy1 = deconvy(r, y1);
406 iy2 = deconvy(r, y2);
409 if(isNaN(y1) || isNaN(y2))
413 if(iy2 >= iy1 - 1 && iy2 <= iy1 + 1)
415 if(iy1 > r->max.y && iy2 > r->max.y)
417 if(iy1 < r->min.y && iy2 < r->min.y)
419 drawinter(co, r, x1, (x1 + x2) / 2, n + 1, c);
420 drawinter(co, r, (x1 + x2) / 2, x2, n + 1, c);
424 drawgraph(Code *co, Rectangle *r, int c)
428 for(x = r->min.x; x < r->max.x; x++)
429 drawinter(co, r, convx(r, x), convx(r, x + 1), 0, c);
433 tickfmt(double d, double m, int n, char *fmt)
439 e2 = log10(fabs(m + n * d));
441 if(e2 >= 4 || e2 < -3){
442 x = ceil(e1-log10(d)-1);
443 snprint(fmt, 32, "%%.%de", x);
446 snprint(fmt, 32, "%%.%df", x);
451 xticklabel(char *fmt, double g, int p, int x, int y)
457 snprint(buf, sizeof(buf), fmt, g);
459 lr.max = addpt(lr.min, stringsize(display->defaultfont, buf));
460 lr = rectsubpt(lr, Pt(Dx(lr) / 2-1, 0));
461 if(lr.max.y >= screen->r.max.y){
463 lr = rectsubpt(lr, Pt(0, lr.min.y - ny));
465 if(rectinrect(lr, screen->r) && (lr.min.x > x || lr.max.x <= x)){
466 string(screen, lr.min, display->black, ZP, display->defaultfont, buf);
473 yticklabel(char *fmt, double g, int p, int x, int y)
479 snprint(buf, sizeof(buf), fmt, g);
481 lr.max = stringsize(display->defaultfont, buf);
482 lr = rectaddpt(lr, Pt(x-Dx(lr)-2, p - Dy(lr) / 2));
483 if(lr.min.x < screen->r.min.x){
485 lr = rectsubpt(lr, Pt(lr.min.x - nx, 0));
487 if(rectinrect(lr, screen->r) && (lr.min.y > y || lr.max.y <= y)){
488 string(screen, lr.min, display->black, ZP, display->defaultfont, buf);
495 calcm(double min, double max, int e, double *dp, double *mp)
500 if((e & 1) != 0) d *= 5;
502 if(min < 0 && max > 0)
511 if(dp != nil) *dp = d;
512 if(mp != nil) *mp = m;
513 return (max-m)*0.999/d;
517 ticks(double min, double max, double *dp, double *mp)
524 e = 2 * ceil(log10(max - min));
528 n = calcm(min, max, e, nil, nil);
530 if(n < 10) m = 10.0 / n;
538 calcm(min, max, beste, dp, mp);
539 return (max - *mp) / *dp;
546 double dx, dy, mx, my;
551 if(xmin < 0 && xmax > 0)
552 x = deconvx(&screen->r, 0);
554 x = screen->r.min.x+5;
555 line(screen, Pt(x, screen->r.min.y), Pt(x, screen->r.max.y), Endarrow, 0, 0, display->black, ZP);
556 if(ymin < 0 && ymax > 0)
557 y = deconvy(&screen->r, 0);
559 y = screen->r.max.y-5;
560 line(screen, Pt(screen->r.min.x, y), Pt(screen->r.max.x, y), 0, Endarrow, 0, display->black, ZP);
561 nx = ticks(xmin, xmax, &dx, &mx);
562 tickfmt(dx, mx, nx, fmt);
563 for(i = 0; i <= nx; i++){
564 p = deconvx(&screen->r, dx*i+mx);
565 if(xticklabel(fmt, dx*i+mx, p, x, y))
566 line(screen, Pt(p, y), Pt(p, y-5), 0, 0, 0, display->black, ZP);
568 ny = ticks(ymin, ymax, &dy, &my);
569 tickfmt(dy, my, ny, fmt);
570 for(i = 0; i <= ny; i++){
571 p = deconvy(&screen->r, dy*i+my);
572 if(yticklabel(fmt, dy*i+my, p, x, y))
573 line(screen, Pt(x, p), Pt(x+5, p), 0, 0, 0, display->black, ZP);
584 memset(pixels, 0, picx * picy);
585 for(i = 0; i < nfns; i++)
586 drawgraph(&fns[i], &screen->r, i % nelem(icolors));
589 flushimage(display, 1);
595 fprint(2, "usage: fplot [-a] [-c [-s size]] [-r range] functions ...\n");
604 double xmin_, xmax_, ymin_, ymax_;
608 if(Dx(r) < 1 || Dy(r) < 1)
610 zoomst = realloc(zoomst, sizeof(FRectangle) * (nzoomst + 1));
611 if(zoomst == nil) sysfatal("realloc: %r");
612 zoomst[nzoomst++] = (FRectangle){xmin, xmax, ymin, ymax};
613 xmin_ = convx(&screen->r, r.min.x);
614 xmax_ = convx(&screen->r, r.max.x);
615 ymin_ = convy(&screen->r, r.max.y);
616 ymax_ = convy(&screen->r, r.min.y);
621 draw(screen, screen->r, display->white, nil, ZP);
628 if(nzoomst == 0) return;
629 xmin = zoomst[nzoomst - 1].xmin;
630 xmax = zoomst[nzoomst - 1].xmax;
631 ymin = zoomst[nzoomst - 1].ymin;
632 ymax = zoomst[nzoomst - 1].ymax;
633 zoomst = realloc(zoomst, sizeof(FRectangle) * --nzoomst);
634 if(zoomst == nil && nzoomst != 0) sysfatal("realloc: %r");
635 draw(screen, screen->r, display->white, nil, ZP);
640 parsefns(int n, char **s)
646 fns = emalloc(sizeof(*fns) * n);
647 for(i = 0; i < nfns; i++) {
648 parse(&fns[i], s[i]);
649 cur = calcstack(&fns[i]);
653 stack = emalloc(sizeof(*stack) * max);
659 while(*s && !isdigit(*s) && *s != '-') s++;
661 xmin = strtod(s, &s);
662 while(*s && !isdigit(*s) && *s != '-') s++;
664 xmax = strtod(s, &s);
665 while(*s && !isdigit(*s) && *s != '-') s++;
667 ymin = strtod(s, &s);
668 while(*s && !isdigit(*s) && *s != '-') s++;
670 ymax = strtod(s, &s);
676 while(*s && !isdigit(*s)) s++;
678 picx = strtol(s, &s, 0);
679 while(*s && !isdigit(*s)) s++;
681 picy = strtol(s, &s, 0);
689 for(i = 0; i < nelem(icolors); i++){
690 freeimage(colors[i]);
691 colors[i] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, icolors[i]);
705 /*TODO: do something more intelligent*/
706 best = (uvlong)(-1)>>1;
707 for(j = screen->r.min.y; j < screen->r.max.y; j++)
708 for(i = screen->r.min.x; i < screen->r.max.x; i++){
709 if(!pixels[(j - screen->r.min.y) * picx + (i - screen->r.min.x)]) continue;
710 d = (i - p.x) * (i - p.x) + (j - p.y) * (j - p.y);
716 ellipse(screen, bestp, 3, 3, 0, display->black, ZP);
717 x = convx(&screen->r, bestp.x);
718 y = convy(&screen->r, bestp.y);
719 besty = calc(&fns[0], x);
720 for(i = 1; i < nfns; i++){
721 ny = calc(&fns[i], x);
722 if(abs(ny - y) < abs(besty - y))
725 snprint(buf, sizeof(buf), "%#.4g %#.4g", x, besty);
726 string(screen, addpt(Pt(10, 10), screen->r.min), display->black, ZP, display->defaultfont, buf);
730 main(int argc, char **argv)
738 case 'a': aflag++; break;
739 case 'r': parserange(EARGF(usage())); break;
740 case 's': parsesize(EARGF(usage())); break;
741 case 'c': cflag++; break;
746 setfcr(getfcr() & ~(FPZDIV | FPINVAL));
747 parsefns(argc, argv);
749 imagedata = emalloc(picx * picy * 3);
750 memset(imagedata, 0xFF, picx * picy * 3);
751 print("%11s %11d %11d %11d %11d ", "r8g8b8", 0, 0, picx, picy);
752 r.min.x = r.min.y = 0;
755 for(i = 0; i < nfns; i++)
756 drawgraph(&fns[i], &r, i % nelem(icolors));
757 if(write(1, imagedata, picx * picy * 3) < picx * picy * 3)
758 sysfatal("write: %r");
760 if(initdraw(nil, nil, "fplot") < 0)
761 sysfatal("initdraw: %r");
762 einit(Emouse | Ekeyboard);
763 picx = Dx(screen->r);
764 picy = Dy(screen->r);
765 pixels = emalloc(picx * picy);
771 if((e.mouse.buttons & 1) != 0)
773 if(((lbut|e.mouse.buttons) & 2) != 0){
774 draw(screen, screen->r, display->white, nil, ZP);
777 if((e.mouse.buttons & 2) != 0)
779 if((~e.mouse.buttons & lbut & 4) != 0)
781 lbut = e.mouse.buttons;
785 case 'q': case 127: exits(nil); break;
787 if(!isInf(ymin, 1) && !isInf(ymax, -1)){
788 zoomst = realloc(zoomst, sizeof(FRectangle) * (nzoomst + 1));
789 if(zoomst == nil) sysfatal("realloc: %r");
790 zoomst[nzoomst++] = (FRectangle){xmin, xmax, ymin, ymax};
791 ymin = gymin-0.05*(gymax-gymin);
792 ymax = gymax+0.05*(gymax-gymin);
793 draw(screen, screen->r, display->white, nil, ZP);
807 if(getwindow(display, Refnone) < 0)
808 sysfatal("getwindow: %r");
809 picx = Dx(screen->r);
810 picy = Dy(screen->r);
811 pixels = realloc(pixels, picx * picy);
812 if(pixels == nil) sysfatal("realloc: %r");