aboutsummaryrefslogtreecommitdiffstats
path: root/demo.c
blob: ff6ed3a521a7fb9ed7d3582d8cb0bab53bb080ca (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* See LICENSE file for copyright and license details. */
#include "common.h"


int
main(void)
{
#define P(I) (points[I].x - 0), (points[I].y - 0)
	/* TODO test drawing outside of raster */

	RTGRPBLIB_RASTER *raster;
	double *image;
	size_t x, y, i, j, n;
	size_t width = 40;
	size_t height = 30;
	struct {
		double x, y;
	} points[] = {
#if 0
		{20, 5},
		{30, 15},
		{20, 25},
		{10, 15}
#elif 1
		{20, 5},
		{35, 15},
		{20, 25},
		{5, 15}
#elif 1
		{5.25, 5.25},
		{35.75, 5.25},
		{35.75, 25.75},
		{5.25, 25.75}
#else
		/* for circular arch*/
		{20, 15},
		{15, 10},
		{100 * M_PI/180, 400 * M_PI/180}
#endif
	};

	raster = rtgrpblib_create_raster(width, height);
	image  = calloc(width * height, sizeof(*image));

	rtgrpblib_set_draftness(raster, -1);

#if 1
	n = sizeof(points) / sizeof(*points);
	for (i = 0; i < n; i++) {
		j = (i + 1) % n;
		rtgrpblib_draw_linear_bezier(raster, P(i), P(j));
	}
#elif 1
	rtgrpblib_draw_quadratic_bezier(raster, P(2), P(3), P(0));
	rtgrpblib_draw_linear_bezier(raster, P(0), P(2));
#elif 1
	rtgrpblib_draw_cubic_bezier(raster, P(0), P(1), P(2), P(3));
	rtgrpblib_draw_linear_bezier(raster, P(3), P(0));
#else
	rtgrpblib_draw_circular_arc(raster, P(0), P(1), P(2));
	rtgrpblib_draw_linear_bezier(raster,
	                             points[0].x + points[1].x * cos(points[2].y),
	                             points[0].y + points[1].y * sin(points[2].y),
	                             points[0].x,
	                             points[0].y);
	rtgrpblib_draw_linear_bezier(raster,
	                             points[0].x,
	                             points[0].y,
	                             points[0].x + points[1].x * cos(points[2].x),
	                             points[0].y + points[1].y * sin(points[2].x));
#endif

	rtgrpblib_fill_shapes(image, width, raster);

#if 1
	printf("\nOutline (area)\n");
	for (y = 0; y < height; y++) {
		for (x = 0; x < width; x++)
			printf(raster->cells[y * width + x].cell_coverage ? "%+.1lf " : " 0   ",
			       raster->cells[y * width + x].cell_coverage);
		printf("\n");
	}
	printf("\nOutline (shadow)\n");
	for (y = 0; y < height; y++) {
		for (x = 0; x < width; x++)
			printf(raster->cells[y * width + x].opposite_coverage ? "%+.1lf " : " 0   ",
			       raster->cells[y * width + x].opposite_coverage);
		printf("\n");
	}
	printf("\nFill\n");
	for (y = 0; y < height; y++) {
		for (x = 0; x < width; x++) {
			unsigned val = (unsigned)(image[y * width + x] * 255 + 0.5);
			printf("%02X ", val - (val == 128));
		}
		printf("\n");
	}
#else
	printf("P2\n# %s\n%zu %zu\n255\n", "Note: no gamma correction applied", width, height);
	for (y = 0; y < height; y++) {
		for (x = 0; x < width; x++)
			printf("%i ", (int)(image->cells[y * width + x] * 255 + 0.5));
		printf("\n");
	}
#endif

	free(raster);
	free(image);
	return 0;
}