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