shithub: fractals

ref: 558a014a3f0463fdc23c8b28c49503e456b5d753
dir: /fractal.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <memdraw.h>
#include "complex.h"
#include "gen.h"

void
usage(void)
{
	fprint(2, "usage: %s [-j a,b] [-s x,y] [-h]\n", argv0);
	exits("usage");
}

void
parsejulia(Complex *c, char *str)
{
	if (!str)
		return;
	
	char *args[2];
	int n = getfields(str, args, 2, 1, ",");
	if (n != 2)
		usage();
	
	c->r = atof(args[0]);
	c->i = atof(args[1]);
}

void
parsesize(int *sizex, int *sizey, char *str)
{
	if (!str)
		return;
	
	char *args[2];
	int n = getfields(str, args, 2, 1, "x");
	if (n != 2)
		usage();
	
	*sizex = atoi(args[0]);
	*sizey = atoi(args[1]);
}

void
main(int argc, char **argv)
{
	int sizex = 256;
	int sizey = 256;
	char *s;
	SetParams params;
	DrawParams dparams;
	Memimage *img;
	
	params.iterations = 10;
	dparams.scale = 3.;
	dparams.offsetx = 0.;
	dparams.offsety = 0.;
	
	ARGBEGIN {
	case 'j':
		s = EARGF(usage());
		parsejulia(&params.c, s);
		params.julia = 1;
		break;
	case 's':
		s = EARGF(usage());
		parsesize(&sizex, &sizey, s);
		params.c.r = 0.;
		params.c.i = 0.;
		break;
	case 'z':
		dparams.scale = atof(EARGF(usage()));
		break;
	case 'x':
		dparams.offsetx = atof(EARGF(usage()));
		break;
	case 'y':
		dparams.offsety = atof(EARGF(usage()));
		break;
	case 'i':
		params.iterations = atoi(EARGF(usage()));
		break;
	case 'h':
		usage();
	} ARGEND;
	
	memimageinit();
	img = generate(sizex, sizey, params, dparams);
	if (!img)
		sysfatal("error: %r");
	
	writememimage(1, img);
	freememimage(img);
	exits(nil);
}