summaryrefslogtreecommitdiff
path: root/src/rotrect.c
blob: f01c29788b95870f21bb54604c98c0f6bc55df6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "rotrect.h"
#include "cfg.h"
#include "lzy.h"
#include <math.h>

static void
rotate(double *x, double *y, double angle)
{
	const double s = sin(angle);
	const double c = cos(angle);
	const double ox = *x;
	const double oy = *y;

	*x = ox * c - oy * s;
	*y = ox * s + oy * c;
}

Rect
rotrect_create(double width, double height, double angle)
{
	double xs[4] = {
		width / 2.0,
		width / 2.0,
		-width / 2.0,
		-width / 2.0,
	};
	double ys[4] = {
		-height / 2.0,
		height / 2.0,
		height / 2.0,
		-height / 2.0,
	};
	for (int i = 0; i < 4; i++)
		rotate(&xs[i], &ys[i], angle);
	return (Rect){ xs[0], ys[0], xs[1], ys[1], xs[2], ys[2], xs[3], ys[3] };
}

void
rotrect_draw(Rect rect, double x, double y)
{
	const int xs[4] = {
		round(rect.x0 + x), round(rect.x1) + x,
		round(rect.x2 + x), round(rect.x3) + x
	};
	const int ys[4] = {
		round(rect.y0 + y), round(rect.y1) + y,
		round(rect.y2 + y), round(rect.y3) + y,
	};
	(void)LZY_DrawLine(xs[0], ys[0], xs[1], ys[1]);
	(void)LZY_DrawLine(xs[1], ys[1], xs[2], ys[2]);
	(void)LZY_DrawLine(xs[2], ys[2], xs[3], ys[3]);
	(void)LZY_DrawLine(xs[3], ys[3], xs[0], ys[0]);
}

void
rotrect(double x, double y, double width, double height, double angle)
{
	rotrect_draw(rotrect_create(width, height, angle), x, y);
}