JCash / voronoi

A C implementation for creating 2D voronoi diagrams
MIT License
622 stars 92 forks source link

Provide simple example of usage #11

Closed abetusk closed 5 years ago

abetusk commented 6 years ago

Thanks for the great library!

It would be helpful to provide a simple example program of usage. The main.c is a little verbose and has a lot of cruft dealing with coloring and saving to an image file.

Here is an example program:

// to compile:
//
// gcc jc_voronoi_example.c -I../src -o jc_voronoi_example  -lm
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define JC_VORONOI_IMPLEMENTATION
// If you wish to use doubles
//#define JCV_REAL_TYPE double
//#define JCV_FABS fabs
//#define JCV_ATAN2 atan2
#include "jc_voronoi.h"

#define NPOINT 10

int main(int argc, char **argv) {
  int i;
  jcv_rect bounding_box = { { 0.0, 0.0 }, { 1.0, 1.0 } };
  jcv_diagram diagram;
  jcv_point points[NPOINT];
  const jcv_site *sites;
  jcv_graphedge *graph_edge;

  memset(&diagram, 0, sizeof(jcv_diagram));

  srand(0);
  for (i=0; i<NPOINT; i++) {
    points[i].x = (float)rand()/(1.0 + RAND_MAX);
    points[i].y = (float)rand()/(1.0 + RAND_MAX);
  }

  // seed sites
  //
  for (i=0; i<NPOINT; i++) {
    printf("%f %f\n\n", points[i].x, points[i].y);
  }

  jcv_diagram_generate(NPOINT, (const jcv_point *)points, &bounding_box, &diagram);

  sites = jcv_diagram_get_sites(&diagram);
  for (i=0; i<diagram.numsites; i++) {

    graph_edge = sites[i].edges;
    while (graph_edge) {
      printf("%f %f\n", graph_edge->pos[0].x, graph_edge->pos[0].y);
      printf("%f %f\n", graph_edge->pos[1].x, graph_edge->pos[1].y);
      printf("\n");

      graph_edge = graph_edge->next;
    }

  }

  jcv_diagram_free(&diagram);
}

The generated seed sites:

0.840188 0.394383
0.783099 0.798440
0.911647 0.197551
0.335223 0.768230
0.277775 0.553970
0.477397 0.628871
0.364784 0.513401
0.952230 0.916195
0.635712 0.717297
0.141603 0.606969

Visualizing the seed sites and edges with gnuplot:

jcv_voronoi_example

This assumes you're in an example subdirectory, say, to compile and run. The number of points is hard coded and it creates the points randomly in a 1x1 box, but the above example gets across clearly how to set up, use and get useful information out of the library. Presumably this 'double covers' the half edges but for illustration purposes I don't think that's a problem.

I'd be happy to submit a pull request if that's helpful.

JCash commented 6 years ago

Hi @abetusk !

Thanks for idea! I took your example and added it to a src/examples folder. I only added a comment about the redundant edges, and also fixed some compiler issues. It's also added to the tests to make sure it always works. It required some maintenance overall since it was the first example, so I took it upon myself to organize it.

Here is the PR: https://github.com/JCash/voronoi/pull/13 I've already pushed to the dev branch, but if you see any changes you'd like, let me know :)

abetusk commented 6 years ago

Hi @JCash, glad you like the idea!

You might want to consider:

JCash commented 5 years ago

Updated the example folder with a README with some brief info