aboutsummaryrefslogblamecommitdiffstats
path: root/demo.c
blob: ff6ed3a521a7fb9ed7d3582d8cb0bab53bb080ca (plain) (tree)






















                                                         
       



                         
       



                               




                                                





                                                        


                                            




                                                                 
       

                                                                  
       

                                                                    











                                                                                   







































                                                                                                    
/* 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;
}