shithub: riscv

Download patch

ref: 5b2f81a5670fed4cdee3c64e89b2014125051707
parent: f102882990af8f9336824f81d24eb2ee1648c9dc
author: aiju <devnull@localhost>
date: Fri Nov 30 11:58:01 EST 2018

fplot: color different curves

--- a/sys/src/cmd/fplot.c
+++ b/sys/src/cmd/fplot.c
@@ -106,7 +106,16 @@
 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 @@
 }
 
 void
-pixel(int x, int y)
+pixel(int x, int y, int c)
 {
 	char *p;
 
@@ -366,13 +375,15 @@
 		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 @@
 	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 @@
 		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 @@
 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);
@@ -667,6 +677,17 @@
 }
 
 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)
 {
 	Event e;
@@ -693,7 +714,7 @@
 		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 {
@@ -700,6 +721,7 @@
 		if(initdraw(nil, nil, "fplot") < 0)
 			sysfatal("initdraw: %r");
 		einit(Emouse | Ekeyboard);
+		alloccolors();
 		drawgraphs();
 		for(;;) {
 			switch(event(&e)) {
@@ -736,6 +758,7 @@
 	if(new) {
 		if(getwindow(display, Refnone) < 0)
 			sysfatal("getwindow: %r");
+		alloccolors();
 		drawgraphs();
 	}
 }