fplot: color different curves

This commit is contained in:
aiju 2018-11-30 16:58:01 +00:00
parent f102882990
commit 5b2f81a567

View file

@ -106,7 +106,16 @@ Token *opstackbot;
double xmin = -10, xmax = 10; double xmin = -10, xmax = 10;
double ymin = -10, ymax = 10; double ymin = -10, ymax = 10;
double gymin, gymax; double gymin, gymax;
Image *color; int icolors[] = {
DBlack,
0xCC0000FF,
0x00CC00FF,
0x0000CCFF,
0xFF00CCFF,
0xFFAA00FF,
0xCCCC00FF,
};
Image *colors[nelem(icolors)];
int cflag, aflag; int cflag, aflag;
char *imagedata; char *imagedata;
int picx = 640, picy = 480; int picx = 640, picy = 480;
@ -358,7 +367,7 @@ deconvy(Rectangle *r, double dy)
} }
void void
pixel(int x, int y) pixel(int x, int y, int c)
{ {
char *p; char *p;
@ -366,13 +375,15 @@ pixel(int x, int y)
if(x >= picx || y >= picy || x < 0 || y < 0) if(x >= picx || y >= picy || x < 0 || y < 0)
return; return;
p = imagedata + (picx * y + x) * 3; p = imagedata + (picx * y + x) * 3;
p[0] = p[1] = p[2] = 0; p[0] = icolors[c] >> 24;
p[1] = icolors[c] >> 16;
p[2] = icolors[c] >> 8;
} else } else
draw(screen, Rect(x, y, x + 1, y + 1), color, nil, ZP); draw(screen, Rect(x, y, x + 1, y + 1), colors[c], nil, ZP);
} }
void void
drawinter(Code *co, Rectangle *r, double x1, double x2, int n) drawinter(Code *co, Rectangle *r, double x1, double x2, int n, int c)
{ {
double y1, y2; double y1, y2;
int iy1, iy2; int iy1, iy2;
@ -384,12 +395,12 @@ drawinter(Code *co, Rectangle *r, double x1, double x2, int n)
y1 = calc(co, x1); y1 = calc(co, x1);
if(!isNaN(y1)) { if(!isNaN(y1)) {
iy1 = deconvy(r, y1); iy1 = deconvy(r, y1);
pixel(ix1, iy1); pixel(ix1, iy1, c);
} }
y2 = calc(co, x2); y2 = calc(co, x2);
if(!isNaN(y2)) { if(!isNaN(y2)) {
iy2 = deconvy(r, y2); iy2 = deconvy(r, y2);
pixel(ix2, iy2); pixel(ix2, iy2, c);
} }
if(isNaN(y1) || isNaN(y2)) if(isNaN(y1) || isNaN(y2))
return; return;
@ -401,17 +412,17 @@ drawinter(Code *co, Rectangle *r, double x1, double x2, int n)
return; return;
if(iy1 < r->min.y && iy2 < r->min.y) if(iy1 < r->min.y && iy2 < r->min.y)
return; return;
drawinter(co, r, x1, (x1 + x2) / 2, n + 1); drawinter(co, r, x1, (x1 + x2) / 2, n + 1, c);
drawinter(co, r, (x1 + x2) / 2, x2, n + 1); drawinter(co, r, (x1 + x2) / 2, x2, n + 1, c);
} }
void void
drawgraph(Code *co, Rectangle *r) drawgraph(Code *co, Rectangle *r, int c)
{ {
int x; int x;
for(x = r->min.x; x < r->max.x; x++) for(x = r->min.x; x < r->max.x; x++)
drawinter(co, r, convx(r, x), convx(r, x + 1), 0); drawinter(co, r, convx(r, x), convx(r, x + 1), 0, c);
} }
void void
@ -563,12 +574,11 @@ void
drawgraphs(void) drawgraphs(void)
{ {
int i; int i;
color = display->black;
gymin = Inf(1); gymin = Inf(1);
gymax = Inf(-1); gymax = Inf(-1);
for(i = 0; i < nfns; i++) for(i = 0; i < nfns; i++)
drawgraph(&fns[i], &screen->r); drawgraph(&fns[i], &screen->r, i % nelem(icolors));
if(!aflag) if(!aflag)
drawaxes(); drawaxes();
flushimage(display, 1); flushimage(display, 1);
@ -666,6 +676,17 @@ parsesize(char *s)
picy = strtol(s, &s, 0); picy = strtol(s, &s, 0);
} }
void
alloccolors(void)
{
int i;
for(i = 0; i < nelem(icolors); i++){
freeimage(colors[i]);
colors[i] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, icolors[i]);
}
}
void void
main(int argc, char **argv) main(int argc, char **argv)
{ {
@ -693,13 +714,14 @@ main(int argc, char **argv)
r.max.x = picx; r.max.x = picx;
r.max.y = picy; r.max.y = picy;
for(i = 0; i < nfns; i++) for(i = 0; i < nfns; i++)
drawgraph(&fns[i], &r); drawgraph(&fns[i], &r, i % nelem(icolors));
if(write(1, imagedata, picx * picy * 3) < picx * picy * 3) if(write(1, imagedata, picx * picy * 3) < picx * picy * 3)
sysfatal("write: %r"); sysfatal("write: %r");
} else { } else {
if(initdraw(nil, nil, "fplot") < 0) if(initdraw(nil, nil, "fplot") < 0)
sysfatal("initdraw: %r"); sysfatal("initdraw: %r");
einit(Emouse | Ekeyboard); einit(Emouse | Ekeyboard);
alloccolors();
drawgraphs(); drawgraphs();
for(;;) { for(;;) {
switch(event(&e)) { switch(event(&e)) {
@ -736,6 +758,7 @@ eresized(int new)
if(new) { if(new) {
if(getwindow(display, Refnone) < 0) if(getwindow(display, Refnone) < 0)
sysfatal("getwindow: %r"); sysfatal("getwindow: %r");
alloccolors();
drawgraphs(); drawgraphs();
} }
} }