about summary refs log tree commit diff stats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c13
1 files changed, 5 insertions, 8 deletions
diff --git a/main.c b/main.c
index f09307b..86d72fa 100644
--- a/main.c
+++ b/main.c
@@ -165,7 +165,6 @@ main(int argc, char *argv[])
 	int i;
 	unsigned int mask;
 	fd_set rd;
-	Bool readin = True;
 	Window w;
 	XEvent ev;
 	XSetWindowAttributes wa;
@@ -252,8 +251,7 @@ main(int argc, char *argv[])
 	/* main event loop, reads status text from stdin as well */
 	while(running) {
 		FD_ZERO(&rd);
-		if(readin)
-			FD_SET(STDIN_FILENO, &rd);
+		FD_SET(STDIN_FILENO, &rd);
 		FD_SET(ConnectionNumber(dpy), &rd);
 
 		i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0);
@@ -269,12 +267,11 @@ main(int argc, char *argv[])
 						(handler[ev.type])(&ev); /* call handler */
 				}
 			}
-			if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
-				readin = NULL != fgets(stext, sizeof(stext), stdin);
-				if(readin)
-					stext[strlen(stext) - 1] = 0;
+			if(FD_ISSET(STDIN_FILENO, &rd)) {
+				if(!fgets(stext, sizeof(stext), stdin))
+					break;
 				else 
-					strcpy(stext, "broken pipe");
+					stext[strlen(stext) - 1] = 0;
 				drawstatus();
 			}
 		}
t-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
function mandelIter(cx, cy, maxIter) {
    let x = 0.0;
    let y = 0.0;
    let xx = 0;
    let yy = 0;
    let xy = 0;

    let i = maxIter;
    while (i-- && xx + yy <= 4) {
        xy = x * y;
        xx = x * x;
        yy = y * y;
        x = xx - yy + cx;
        y = xy + xy + cy;
    }
    return maxIter - i;
}

function mandelbrot(canvas, xmin, xmax, ymin, ymax, iterations) {
    const width = canvas.width;
    const height = canvas.height;
    const ctx = canvas.getContext('2d');
    const img = ctx.getImageData(0, 0, width, height);
    const pix = img.data;

    const pixels = Array.from({ length: width * height }, (_, index) => {
        const ix = index % width;
        const iy = Math.floor(index / width);
        const x = xmin + (xmax - xmin) * ix / (width - 1);
        const y = ymin + (ymax - ymin) * iy / (height - 1);
        const i = mandelIter(x, y, iterations);

        if (i > iterations) {
            return [0, 0, 0, 255];
        } else {
            const c = 3 * Math.log(i) / Math.log(iterations - 1.0);
            if (c < 1) {
                const shade = Math.floor(255 * c);
                return [shade, shade, shade, 255];
            } else if (c < 2) {
                const shade = Math.floor(255 * (c - 1));
                return [shade, shade, shade, 255];
            } else {
                const shade = Math.floor(255 * (c - 2));
                return [shade, shade, shade, 255];
            }
        }
    });

    pixels.forEach(([r, g, b, a], index) => {
        const ppos = 4 * index;
        pix[ppos] = r;
        pix[ppos + 1] = g;
        pix[ppos + 2] = b;
        pix[ppos + 3] = a;
    });

    ctx.putImageData(img, 0, 0);
}

const canvas = document.getElementById('mandelbrot');
canvas.width = 900;
canvas.height = 600;

mandelbrot(canvas, -2, 1, -1, 1, 1000);