From 6b687b91e788b4e91a6ed30640a2477b9596b954 Mon Sep 17 00:00:00 2001 From: sanine Date: Wed, 26 Jan 2022 02:32:06 -0600 Subject: refactor: move drawing functions into drawing.h/drawing.c --- experimental/tectonics/Makefile | 4 +-- experimental/tectonics/drawing.c | 56 ++++++++++++++++++++++++++++++ experimental/tectonics/drawing.h | 10 ++++++ experimental/tectonics/geometry.h | 3 -- experimental/tectonics/output-relaxed.png | Bin 31195 -> 0 bytes experimental/tectonics/output.png | Bin 32263 -> 0 bytes experimental/tectonics/quadtree.c | 24 ------------- experimental/tectonics/tectonics.c | 3 +- experimental/tectonics/tectonics.h | 2 -- experimental/tectonics/util.c | 8 ----- experimental/tectonics/util.h | 2 -- experimental/tectonics/world.c | 21 ----------- 12 files changed, 70 insertions(+), 63 deletions(-) create mode 100644 experimental/tectonics/drawing.c create mode 100644 experimental/tectonics/drawing.h delete mode 100644 experimental/tectonics/output-relaxed.png delete mode 100644 experimental/tectonics/output.png (limited to 'experimental') diff --git a/experimental/tectonics/Makefile b/experimental/tectonics/Makefile index 877fec1..ab8fb0d 100644 --- a/experimental/tectonics/Makefile +++ b/experimental/tectonics/Makefile @@ -5,8 +5,8 @@ DEPS=tectonics.h %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) -all: world.o util.o tectonics.o quadtree.o - $(CC) -o tectonics world.o util.o tectonics.o quadtree.o $(CFLAGS) +all: world.o util.o tectonics.o quadtree.o drawing.o + $(CC) -o tectonics world.o util.o tectonics.o quadtree.o drawing.o $(CFLAGS) test: all quadtree.o quadtree.test.o tests.o $(CC) -o tests world.o util.o quadtree.o quadtree.test.o tests.o $(CFLAGS) diff --git a/experimental/tectonics/drawing.c b/experimental/tectonics/drawing.c new file mode 100644 index 0000000..91f873b --- /dev/null +++ b/experimental/tectonics/drawing.c @@ -0,0 +1,56 @@ +#include +#include + +#include "tectonics.h" +#include "geometry.h" +#include "drawing.h" + +void get_cairo_size(cairo_t *cr, int *width, int *height) +{ + cairo_surface_t *surface = cairo_get_target(cr); + *width = cairo_image_surface_get_width(surface); + *height = cairo_image_surface_get_height(surface); +} + + +void draw_world(cairo_t *cr, struct world_t *world) +{ + int width, height; + get_cairo_size(cr, &width, &height); + double r = ((double)width) / (100*sqrt(world->n_points)); + cairo_set_source_rgba(cr, 1, 0, 0, 1); + + for (int i=0; in_points; i++) { + struct point_t *pt = world->points + i; + double xc = pt->x * width; + double yc = pt->y * height; + cairo_arc(cr, xc, yc, r, 0, 2*M_PI); + cairo_stroke(cr); + } + + //draw_quadtree(cr, &(world->tree)); +} + + +void draw_quadtree(cairo_t *cr, struct quadtree_node_t *node) +{ + int width, height; + get_cairo_size(cr, &width, &height); + + cairo_set_source_rgba(cr, 0, 0, 1, 1); + struct point_t center = node->region.center; + double half_dim = node->region.half_dim; + + double x = (center.x - half_dim) * width; + double y = (center.y - half_dim) * height; + + cairo_rectangle(cr, x, y, 2*half_dim * width, 2*half_dim * height); + cairo_stroke(cr); + + if (node->nw != NULL) { + draw_quadtree(cr, node->nw); + draw_quadtree(cr, node->ne); + draw_quadtree(cr, node->sw); + draw_quadtree(cr, node->se); + } +} diff --git a/experimental/tectonics/drawing.h b/experimental/tectonics/drawing.h new file mode 100644 index 0000000..e1a1126 --- /dev/null +++ b/experimental/tectonics/drawing.h @@ -0,0 +1,10 @@ +#ifndef DRAWING_H +#define DRAWING_H + +#include + +void get_cairo_size(cairo_t *cr, int *width, int *height); +void draw_quadtree(cairo_t *cr, struct quadtree_node_t *root); +void draw_world(cairo_t *cr, struct world_t *world); + +#endif diff --git a/experimental/tectonics/geometry.h b/experimental/tectonics/geometry.h index 09bd7c8..de01042 100644 --- a/experimental/tectonics/geometry.h +++ b/experimental/tectonics/geometry.h @@ -2,7 +2,6 @@ #define GEOMETRY_H #include -#include struct point_t { double x, y; @@ -60,8 +59,6 @@ int quadtree_get_closest(struct quadtree_node_t *root, struct point_t *points, struct point_t pt); -void draw_quadtree(cairo_t *cr, struct quadtree_node_t *root); - /* free a quadtree structure * * note that this will *NOT* free any point userdata! diff --git a/experimental/tectonics/output-relaxed.png b/experimental/tectonics/output-relaxed.png deleted file mode 100644 index f53c0d1..0000000 Binary files a/experimental/tectonics/output-relaxed.png and /dev/null differ diff --git a/experimental/tectonics/output.png b/experimental/tectonics/output.png deleted file mode 100644 index 1fddaf3..0000000 Binary files a/experimental/tectonics/output.png and /dev/null differ diff --git a/experimental/tectonics/quadtree.c b/experimental/tectonics/quadtree.c index 820844d..45f11ac 100644 --- a/experimental/tectonics/quadtree.c +++ b/experimental/tectonics/quadtree.c @@ -167,30 +167,6 @@ int quadtree_get_closest(struct quadtree_node_t *node, } -void draw_quadtree(cairo_t *cr, struct quadtree_node_t *node) -{ - int width, height; - get_cairo_size(cr, &width, &height); - - cairo_set_source_rgba(cr, 0, 0, 1, 1); - struct point_t center = node->region.center; - double half_dim = node->region.half_dim; - - double x = (center.x - half_dim) * width; - double y = (center.y - half_dim) * height; - - cairo_rectangle(cr, x, y, 2*half_dim * width, 2*half_dim * height); - cairo_stroke(cr); - - if (node->nw != NULL) { - draw_quadtree(cr, node->nw); - draw_quadtree(cr, node->ne); - draw_quadtree(cr, node->sw); - draw_quadtree(cr, node->se); - } -} - - void quadtree_free(struct quadtree_node_t *node) { if (node->nw != NULL) { diff --git a/experimental/tectonics/tectonics.c b/experimental/tectonics/tectonics.c index e43a425..7d5342a 100644 --- a/experimental/tectonics/tectonics.c +++ b/experimental/tectonics/tectonics.c @@ -1,6 +1,7 @@ #include #include "tectonics.h" +#include "drawing.h" #define X_RES 1024 #define Y_RES X_RES @@ -14,7 +15,7 @@ int main() create_world(&world, 10000); if (world.points == NULL) return 1; - render_world(cr, &world); + draw_world(cr, &world); free_world(&world); cairo_destroy(cr); diff --git a/experimental/tectonics/tectonics.h b/experimental/tectonics/tectonics.h index de5b18a..bbde9dd 100644 --- a/experimental/tectonics/tectonics.h +++ b/experimental/tectonics/tectonics.h @@ -3,7 +3,6 @@ #include #include -#include #include "geometry.h" @@ -22,7 +21,6 @@ struct world_t { void create_world(struct world_t *world, int n_points); void free_world(struct world_t *world); -void render_world(cairo_t *cr, struct world_t *world); void run_world_step(struct world_t *world); #endif diff --git a/experimental/tectonics/util.c b/experimental/tectonics/util.c index 0bbe880..aa528fb 100644 --- a/experimental/tectonics/util.c +++ b/experimental/tectonics/util.c @@ -3,14 +3,6 @@ #include "util.h" -void get_cairo_size(cairo_t *cr, int *width, int *height) -{ - cairo_surface_t *surface = cairo_get_target(cr); - *width = cairo_image_surface_get_width(surface); - *height = cairo_image_surface_get_height(surface); -} - - double rand01() { return ((double) rand())/RAND_MAX; diff --git a/experimental/tectonics/util.h b/experimental/tectonics/util.h index 1a5a505..762229b 100644 --- a/experimental/tectonics/util.h +++ b/experimental/tectonics/util.h @@ -1,10 +1,8 @@ #ifndef UTIL_H #define UTIL_H -#include #include "geometry.h" -void get_cairo_size(cairo_t *cr, int *width, int *height); double rand01(); double distance(struct point_t p1, struct point_t p2); diff --git a/experimental/tectonics/world.c b/experimental/tectonics/world.c index 5aead24..5aa485c 100644 --- a/experimental/tectonics/world.c +++ b/experimental/tectonics/world.c @@ -131,24 +131,3 @@ void free_world(struct world_t *world) } free(world->points); } - - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - -void render_world(cairo_t *cr, struct world_t *world) -{ - int width, height; - get_cairo_size(cr, &width, &height); - double r = ((double)width) / (100*sqrt(world->n_points)); - cairo_set_source_rgba(cr, 1, 0, 0, 1); - - for (int i=0; in_points; i++) { - struct point_t *pt = world->points + i; - double xc = pt->x * width; - double yc = pt->y * height; - cairo_arc(cr, xc, yc, r, 0, 2*M_PI); - cairo_stroke(cr); - } - - //draw_quadtree(cr, &(world->tree)); -} -- cgit v1.2.1