8 typedef struct Operator Operator;
9 typedef struct Token Token;
10 typedef struct Constant Constant;
11 typedef struct Code Code;
48 void omax(void) { sp--; if(sp[1]>*sp) *sp = sp[1]; }
49 void omin(void) { sp--; if(sp[1]<*sp) *sp = sp[1]; }
50 void add(void) { sp--; *sp += *(sp+1); }
51 void sub(void) { sp--; *sp -= *(sp+1); }
52 void mul(void) { sp--; *sp *= *(sp+1); }
53 void div(void) { sp--; *sp /= *(sp+1); }
54 void mod(void) { sp--; *sp = fmod(*sp, *(sp+1)); }
55 void pot(void) { sp--; *sp = pow(*sp, *(sp+1)); }
56 void osin(void) { *sp = sin(*sp); }
57 void ocos(void) { *sp = cos(*sp); }
58 void otan(void) { *sp = tan(*sp); }
59 void oasin(void) { *sp = asin(*sp); }
60 void oacos(void) { *sp = acos(*sp); }
61 void oatan(void) { *sp = atan(*sp); }
62 void osqrt(void) { *sp = sqrt(*sp); }
63 void oexp(void) { *sp = exp(*sp); }
64 void olog(void) { *sp = log10(*sp); }
65 void oln(void) { *sp = log(*sp); }
74 "max", OBINARY, 0, 0, omax,
75 "min", OBINARY, 0, 0, omax,
76 "+", OBINARY, 0, 100, add,
77 "-", OBINARY, 0, 100, sub,
78 "*", OBINARY, 0, 200, mul,
79 "/", OBINARY, 0, 200, div,
80 "%", OBINARY, 0, 200, mod,
81 "^", OBINARY, 1, 300, pot,
82 "sin", OUNARY, 0, 400, osin,
83 "cos", OUNARY, 0, 400, ocos,
84 "tan", OUNARY, 0, 400, otan,
85 "asin", OUNARY, 0, 400, oasin,
86 "acos", OUNARY, 0, 400, oacos,
87 "atan", OUNARY, 0, 400, oatan,
88 "sqrt", OUNARY, 0, 400, osqrt,
89 "exp", OUNARY, 0, 400, oexp,
90 "log", OUNARY, 0, 400, olog,
91 "ln", OUNARY, 0, 400, oln,
110 double xmin = -10, xmax = 10;
111 double ymin = -10, ymax = 10;
122 Image *colors[nelem(icolors)];
126 int picx = 640, picy = 480;
128 typedef struct FRectangle FRectangle;
130 double xmin, xmax, ymin, ymax;
139 v = mallocz(size, 1);
141 sysfatal("emalloc: %r");
142 setmalloctag(v, getcallerpc(&size));
149 if(c->len >= c->cap) {
151 c->p = realloc(c->p, sizeof(Op) * c->cap);
153 sysfatal("realloc: %r");
161 t->next = opstackbot;
173 sysfatal("stack underflow");
174 opstackbot = t->next;
176 sysfatal("non-operator pop");
177 o.type = t->op->type;
189 opstackbot = t->next;
205 t = emalloc(sizeof(*t));
208 t->val = strtod(*s, s);
226 for(o = ops; o < ops + nelem(ops); o++)
227 if(strncmp(*s, o->s, strlen(o->s)) == 0) {
233 for(c = consts; c < consts + nelem(consts); c++)
234 if(strncmp(*s, c->s, strlen(c->s)) == 0) {
240 sysfatal("syntax error at %s", *s);
245 parse(Code *c, char *s)
264 if(t->op->type == OBINARY)
265 while(opstackbot != nil && opstackbot->type == TOP &&
266 (opstackbot->op->prec > t->op->prec ||
267 !t->op->rassoc && opstackbot->op->prec == t->op->prec))
275 while(opstackbot != nil && opstackbot->type == TOP)
277 if(opstackbot == nil)
278 sysfatal("mismatched parentheses");
283 sysfatal("unknown token type %d", t->type);
286 while(opstackbot != nil)
287 switch(opstackbot->type) {
292 sysfatal("mismatched parentheses");
294 sysfatal("syntax error");
306 for(o = c->p; o < c->p + c->len; o++)
308 case ONUMBER: case OVAR:
314 sysfatal("syntax error");
318 sysfatal("syntax error");
324 sysfatal("syntax error");
329 calc(Code *c, double x)
334 for(o = c->p; o < c->p + c->len; o++)
342 case OUNARY: case OBINARY:
345 if(*sp < gymin) gymin = *sp;
346 if(*sp > gymax) gymax = *sp;
351 convx(Rectangle *r, int x)
353 return (xmax - xmin) * (x - r->min.x) / (r->max.x - r->min.x) + xmin;
357 deconvx(Rectangle *r, double dx)
359 return (dx - xmin) * (r->max.x - r->min.x) / (xmax - xmin) + r->min.x + 0.5;
363 convy(Rectangle *r, int y)
365 return (ymax - ymin) * (r->max.y - y) / (r->max.y - r->min.y) + ymin;
369 deconvy(Rectangle *r, double dy)
371 return (ymax - dy) * (r->max.y - r->min.y) / (ymax - ymin) + r->min.y + 0.5;
375 pixel(int x, int y, int c)
380 if(x >= picx || y >= picy || x < 0 || y < 0)
382 p = imagedata + (picx * y + x) * 3;
383 p[0] = icolors[c] >> 24;
384 p[1] = icolors[c] >> 16;
385 p[2] = icolors[c] >> 8;
387 draw(screen, Rect(x, y, x + 1, y + 1), colors[c], nil, ZP);
388 if(ptinrect(Pt(x, y), screen->r))
389 pixels[picx * (y - screen->r.min.y) + (x - screen->r.min.x)] = 1;
394 drawinter(Code *co, Rectangle *r, double x1, double x2, int n, int c)
400 ix1 = deconvx(r, x1);
401 ix2 = deconvx(r, x2);
405 iy1 = deconvy(r, y1);
410 iy2 = deconvy(r, y2);
413 if(isNaN(y1) || isNaN(y2))
417 if(iy2 >= iy1 - 1 && iy2 <= iy1 + 1)
419 if(iy1 > r->max.y && iy2 > r->max.y)
421 if(iy1 < r->min.y && iy2 < r->min.y)
423 drawinter(co, r, x1, (x1 + x2) / 2, n + 1, c);
424 drawinter(co, r, (x1 + x2) / 2, x2, n + 1, c);
428 drawgraph(Code *co, Rectangle *r, int c)
432 for(x = r->min.x; x < r->max.x; x++)
433 drawinter(co, r, convx(r, x), convx(r, x + 1), 0, c);
437 tickfmt(double d, double m, int n, char *fmt)
443 e2 = log10(fabs(m + n * d));
445 if(e2 >= 4 || e2 < -3){
446 x = ceil(e1-log10(d)-1);
447 snprint(fmt, 32, "%%.%de", x);
450 snprint(fmt, 32, "%%.%df", x);
455 xticklabel(char *fmt, double g, int p, int x, int y)
461 snprint(buf, sizeof(buf), fmt, g);
463 lr.max = addpt(lr.min, stringsize(display->defaultfont, buf));
464 lr = rectsubpt(lr, Pt(Dx(lr) / 2-1, 0));
465 if(lr.max.y >= screen->r.max.y){
467 lr = rectsubpt(lr, Pt(0, lr.min.y - ny));
469 if(rectinrect(lr, screen->r) && (lr.min.x > x || lr.max.x <= x)){
470 string(screen, lr.min, display->black, ZP, display->defaultfont, buf);
477 yticklabel(char *fmt, double g, int p, int x, int y)
483 snprint(buf, sizeof(buf), fmt, g);
485 lr.max = stringsize(display->defaultfont, buf);
486 lr = rectaddpt(lr, Pt(x-Dx(lr)-2, p - Dy(lr) / 2));
487 if(lr.min.x < screen->r.min.x){
489 lr = rectsubpt(lr, Pt(lr.min.x - nx, 0));
491 if(rectinrect(lr, screen->r) && (lr.min.y > y || lr.max.y <= y)){
492 string(screen, lr.min, display->black, ZP, display->defaultfont, buf);
499 calcm(double min, double max, int e, double *dp, double *mp)
504 if((e & 1) != 0) d *= 5;
506 if(min < 0 && max > 0)
515 if(dp != nil) *dp = d;
516 if(mp != nil) *mp = m;
517 return (max-m)*0.999/d;
521 ticks(double min, double max, double *dp, double *mp)
528 e = 2 * ceil(log10(max - min));
532 n = calcm(min, max, e, nil, nil);
534 if(n < 10) m = 10.0 / n;
542 calcm(min, max, beste, dp, mp);
543 return (max - *mp) / *dp;
550 double dx, dy, mx, my;
555 if(xmin < 0 && xmax > 0)
556 x = deconvx(&screen->r, 0);
558 x = screen->r.min.x+5;
559 line(screen, Pt(x, screen->r.min.y), Pt(x, screen->r.max.y), Endarrow, 0, 0, display->black, ZP);
560 if(ymin < 0 && ymax > 0)
561 y = deconvy(&screen->r, 0);
563 y = screen->r.max.y-5;
564 line(screen, Pt(screen->r.min.x, y), Pt(screen->r.max.x, y), 0, Endarrow, 0, display->black, ZP);
565 nx = ticks(xmin, xmax, &dx, &mx);
566 tickfmt(dx, mx, nx, fmt);
567 for(i = 0; i <= nx; i++){
568 p = deconvx(&screen->r, dx*i+mx);
569 if(xticklabel(fmt, dx*i+mx, p, x, y))
570 line(screen, Pt(p, y), Pt(p, y-5), 0, 0, 0, display->black, ZP);
572 ny = ticks(ymin, ymax, &dy, &my);
573 tickfmt(dy, my, ny, fmt);
574 for(i = 0; i <= ny; i++){
575 p = deconvy(&screen->r, dy*i+my);
576 if(yticklabel(fmt, dy*i+my, p, x, y))
577 line(screen, Pt(x, p), Pt(x+5, p), 0, 0, 0, display->black, ZP);
588 memset(pixels, 0, picx * picy);
589 for(i = 0; i < nfns; i++)
590 drawgraph(&fns[i], &screen->r, i % nelem(icolors));
593 flushimage(display, 1);
599 fprint(2, "usage: fplot [-a] [-c [-s size]] [-r range] functions ...\n");
608 double xmin_, xmax_, ymin_, ymax_;
612 if(Dx(r) < 1 || Dy(r) < 1)
614 zoomst = realloc(zoomst, sizeof(FRectangle) * (nzoomst + 1));
615 if(zoomst == nil) sysfatal("realloc: %r");
616 zoomst[nzoomst++] = (FRectangle){xmin, xmax, ymin, ymax};
617 xmin_ = convx(&screen->r, r.min.x);
618 xmax_ = convx(&screen->r, r.max.x);
619 ymin_ = convy(&screen->r, r.max.y);
620 ymax_ = convy(&screen->r, r.min.y);
625 draw(screen, screen->r, display->white, nil, ZP);
632 if(nzoomst == 0) return;
633 xmin = zoomst[nzoomst - 1].xmin;
634 xmax = zoomst[nzoomst - 1].xmax;
635 ymin = zoomst[nzoomst - 1].ymin;
636 ymax = zoomst[nzoomst - 1].ymax;
637 zoomst = realloc(zoomst, sizeof(FRectangle) * --nzoomst);
638 if(zoomst == nil && nzoomst != 0) sysfatal("realloc: %r");
639 draw(screen, screen->r, display->white, nil, ZP);
644 parsefns(int n, char **s)
650 fns = emalloc(sizeof(*fns) * n);
651 for(i = 0; i < nfns; i++) {
652 parse(&fns[i], s[i]);
653 cur = calcstack(&fns[i]);
657 stack = emalloc(sizeof(*stack) * max);
663 while(*s && !isdigit(*s) && *s != '-') s++;
665 xmin = strtod(s, &s);
666 while(*s && !isdigit(*s) && *s != '-') s++;
668 xmax = strtod(s, &s);
669 while(*s && !isdigit(*s) && *s != '-') s++;
671 ymin = strtod(s, &s);
672 while(*s && !isdigit(*s) && *s != '-') s++;
674 ymax = strtod(s, &s);
680 while(*s && !isdigit(*s)) s++;
682 picx = strtol(s, &s, 0);
683 while(*s && !isdigit(*s)) s++;
685 picy = strtol(s, &s, 0);
693 for(i = 0; i < nelem(icolors); i++){
694 freeimage(colors[i]);
695 colors[i] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, icolors[i]);
709 /*TODO: do something more intelligent*/
710 best = (uvlong)(-1)>>1;
711 for(j = screen->r.min.y; j < screen->r.max.y; j++)
712 for(i = screen->r.min.x; i < screen->r.max.x; i++){
713 if(!pixels[(j - screen->r.min.y) * picx + (i - screen->r.min.x)]) continue;
714 d = (i - p.x) * (i - p.x) + (j - p.y) * (j - p.y);
720 ellipse(screen, bestp, 3, 3, 0, display->black, ZP);
721 x = convx(&screen->r, bestp.x);
722 y = convy(&screen->r, bestp.y);
723 besty = calc(&fns[0], x);
724 for(i = 1; i < nfns; i++){
725 ny = calc(&fns[i], x);
726 if(abs(ny - y) < abs(besty - y))
729 snprint(buf, sizeof(buf), "%#.4g %#.4g", x, besty);
730 string(screen, addpt(Pt(10, 10), screen->r.min), display->black, ZP, display->defaultfont, buf);
734 main(int argc, char **argv)
742 case 'a': aflag++; break;
743 case 'r': parserange(EARGF(usage())); break;
744 case 's': parsesize(EARGF(usage())); break;
745 case 'c': cflag++; break;
750 setfcr(getfcr() & ~(FPZDIV | FPINVAL));
751 parsefns(argc, argv);
753 imagedata = emalloc(picx * picy * 3);
754 memset(imagedata, 0xFF, picx * picy * 3);
755 print("%11s %11d %11d %11d %11d ", "r8g8b8", 0, 0, picx, picy);
756 r.min.x = r.min.y = 0;
759 for(i = 0; i < nfns; i++)
760 drawgraph(&fns[i], &r, i % nelem(icolors));
761 if(write(1, imagedata, picx * picy * 3) < picx * picy * 3)
762 sysfatal("write: %r");
764 if(initdraw(nil, nil, "fplot") < 0)
765 sysfatal("initdraw: %r");
766 einit(Emouse | Ekeyboard);
767 picx = Dx(screen->r);
768 picy = Dy(screen->r);
769 pixels = emalloc(picx * picy);
775 if((e.mouse.buttons & 1) != 0)
777 if(((lbut|e.mouse.buttons) & 2) != 0){
778 draw(screen, screen->r, display->white, nil, ZP);
781 if((e.mouse.buttons & 2) != 0)
783 if((~e.mouse.buttons & lbut & 4) != 0)
785 lbut = e.mouse.buttons;
789 case 'q': case 127: exits(nil); break;
791 if(!isInf(ymin, 1) && !isInf(ymax, -1)){
792 zoomst = realloc(zoomst, sizeof(FRectangle) * (nzoomst + 1));
793 if(zoomst == nil) sysfatal("realloc: %r");
794 zoomst[nzoomst++] = (FRectangle){xmin, xmax, ymin, ymax};
795 ymin = gymin-0.05*(gymax-gymin);
796 ymax = gymax+0.05*(gymax-gymin);
797 draw(screen, screen->r, display->white, nil, ZP);
811 if(getwindow(display, Refnone) < 0)
812 sysfatal("getwindow: %r");
813 picx = Dx(screen->r);
814 picy = Dy(screen->r);
815 pixels = realloc(pixels, picx * picy);
816 if(pixels == nil) sysfatal("realloc: %r");