Introduction to Voronoi diagram and implementation of C language

Source: Internet
Author: User

Voronoi diagrams are widely used in geometry, geography, crystallography, information systems and other disciplines. A Voronoi diagram is a continuous polygon composed of perpendicular bisector that are connected by each adjacent point in the diagram. The points in the diagram belong to the nearest polygon of the point, as shown in:



The corresponding C language implementation code for the Voronoi diagram is as follows:

#include <stdio.h> #include <stdlib.h> #include <string.h> #define N_sites 150double site[n_sites][2] ; unsigned char rgb[n_sites][3]; int size_x = 640, size_y = 480; Inline double sq2 (double x, double y)//square function{return x * x + y * y;} #define For_k for (k = 0; k < n_sites; +)//define for_kint nearest_site (double x, double y)//get the nearest site Id{int k, ret = 0;double D, dist = 0;for_k {D = SQ2 (X-site[k][0], y-site[k][1]), if (!k | | D < dist) {//get the nearest distancedist = d, ret = k;}} return ret;} /* See if a pixel are different from any neighboring ones */int At_edge (int *color, int y, int x) {int I, j, C = color[y * s Ize_x + x];for (i = y-1; I <= y + 1; i++) {if (I < 0 | | I >= size_y) continue; for (j = x-1; J <= x + 1; j + +) {if (J < 0 | | J >= size_x) continue;if (Color[i * size_x + j]! = c) return 1;}} return 0;} #define AA_RES 4/* Average over 4x4 supersampling grid */void aa_color (unsigned char *pix, int y, int x)//gET color for pix (x, y) {int i, j, n;double r = 0, g = 0, b = 0, XX, yy;for (i = 0; i < aa_res; i++) {yy = y + 1./aa_re S * i +. 5;for (j = 0; J < Aa_res; J + +) {xx = x + 1./aa_res * j +. 5;n = Nearest_site (xx, yy); R + = Rgb[n][0];g + = RGB N [1];b + = Rgb[n][2];}} Pix[0] = R/(aa_res * aa_res);p ix[1] = g/(Aa_res * aa_res);p ix[2] = b/(aa_res * aa_res);} #define For_i for (i = 0; i < size_y; i++) #define For_j for (j = 0; J < size_x; J + +) void Gen_map () {int I, J, K;int * nearest = malloc (sizeof (int) * size_y * size_x); unsigned char *ptr, *buf, color; ptr = BUF = malloc (3 * size_x * size_y); For_i for_j Nearest[i * size_x + j] = Nearest_site (j, I); For_i For_j {if (!at_edge (nearest, I, J)) memcpy (PTR, Rgb[nearest[i * size_x + j]], 3); else/* at Edge, do anti-alias raster ing */aa_color (PTR, I, j);p tr + = 3;} /* Draw Sites */for (k = 0; k < n_sites; k++) {color = (rgb[k][0]*.25 + rgb[k][1]*.6 + rgb[k][2]*.15 > 80)? 0:255 ; for (i = site[k][1]-1; I <= site[k][1] + 1; i++) {if (i < 0 | | I >= size_y) continue; for (j = site[k][0]-1; J <= Site[k][0] + 1; j + +) {if (J < 0 | | J >= size_x) continue; PTR = buf + 3 * (i * size_x + j);p tr[0] = ptr[1] = ptr[2] = color;}} printf ("p6\n%d%d\n255\n", size_x, size_y); Fflush (stdout); Fwrite (buf, size_y * size_x * 3, 1, stdout);} #define Frand (x) (rand ()/(1. + rand_max) * x) int main () {int K;for_k {site[k][0] = Frand (size_x); site[k][1] = Frand (size_ y) RGB [k][0] = Frand (n), RGB [k][1] = Frand (n), RGB [k][2] = Frand (256);} Gen_map (); return 0;}



Introduction to Voronoi diagram and implementation of C language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.