~aleteoryx/9c

16b2039dcba74f8a3b3a906600a5da0fa10ac87d — glenda 37 years ago 65f91c5
wings!
1 files changed, 108 insertions(+), 11 deletions(-)

M spiral.c
M spiral.c => spiral.c +108 -11
@@ 2,45 2,142 @@
#include <libc.h>
#include <draw.h>
#include <event.h>
#include <cursor.h>

#define RAD (PI * 2)

Image *bg,*fg;

int frametime;
double spr;
int wings;
double wingrads;

vlong starttime;

void
usage(void)
{
	fprint(2,"usage: %s\n", argv0);
	fprint(2,"usage: %s [-f framerate] [-r rpm] [-w wings]\n", argv0);
	exits("usage");
}

Point
Spt(int x, int y)
SPt(int x, int y)
{
	return addpt(Pt(x, y), screen->r.min);
}

Rectangle
Srect(int x0, int y0, int x1, int y1)
SRect(int x0, int y0, int x1, int y1)
{
	return rectaddpt(Rect(x0, y0, x1, y1), screen->r.min);
}

Point
imagecenter(Image* img)
{
	return divpt(addpt(img->r.min, img->r.max), 2);
}

double
imageradius(Image* img)
{
	Point dims;
	dims = divpt(subpt(img->r.max, img->r.min), 2);
	return 1.4 + sqrt(dims.x*dims.x + dims.y*dims.y);
}

Point
vecpt(double radians, double radius)
{
	double x,y;
	x = sin(radians) * radius;
	y = cos(radians) * radius;

	return Pt(x + 1, y + 1);
}

void
drawframe(double ts)
{
	double radians, radius;
	Point center;

	radians = fmod(ts / spr, spr) * RAD;
	radius = imageradius(screen);
	center = imagecenter(screen);

	draw(screen, screen->r, bg, nil, SPt(0, 0));

	for(int i = 0; i < wings; i++)
	{
		line(screen, center, addpt(vecpt(radians, radius), center), Enddisc, Enddisc, 2, fg, Pt(0,0));
		radians += wingrads * 2;
	}
}

double
ftime(void)
{
	return ((double)(nsec() - starttime)) / 1000000000.0;
}

void
eresized(int new)
{
	if(new && getwindow(display, Refnone) < 0)
		fprint(2,"can't reattach to window\n");
	drawframe(ftime());
}

void
main(int argc, char* argv[])
{
	Image* drawsrc;
	int timer,etype;
	Event e;

	frametime = 1.0 / 30.0 * 1000;
	wings = 3;
	spr = 2;

	ARGBEGIN{
	case 'f':
		frametime = 1.0 / atof(EARGF(usage())) * 1000;
		break;
	case 'r':
		spr = 1.0 / (atof(EARGF(usage())) / 60.0);
		break;
	case 'w':
		wings = atoi(EARGF(usage()));
		break;
	default:
		usage();
	} ARGEND

	if (initdraw(nil, nil, "spiral") == -1)
	if(wings < 1) wings = 1;
	wingrads = PI / (double) wings;

	if(initdraw(nil, nil, "spiral") < 0)
		sysfatal("initdraw: %r");

	drawsrc = allocimagemix(display, DPurpleblue, DPurpleblue);
	if (!drawsrc)
	fg = allocimage(display, Rect(0,0, 1,1), GREY1, 1, DBlack);
	if(!fg)
		sysfatal("couldn't allocimage!");

	line(screen, Spt(10,10), Spt(50,50), Enddisc, Endsquare, 5, drawsrc, Spt(0,0));
	flushimage(display, 1);
	sleep(10*1000);
	bg = allocimage(display, Rect(0,0, 1,1), GREY1, 1, DWhite);
	if(!bg)
		sysfatal("couldn't allocimage!");

	starttime = nsec();

	einit(Emouse);
	timer = etimer(0, frametime);

	for(;;)
	{
		etype = event(&e);

		if (etype == timer)
			drawframe(ftime());
	}
}