My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*2>/dev/null 2>/dev/null 2>/dev/null 2>/dev/null 2>/dev/null

#############################################################

this=$( which ${0} )
that=$( basename ${this} | sed 's,.cpp$,,')

c++ -O6 ${this} -o ${that} -lm && ./${that} ${*}

#############################################################
exit

http://en.wikipedia.org/wiki/Voronoi_diagram

*/

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>

typedef enum { RANDOM, GRID, HEX } PointMode;

PointMode toPointMode( int n ) {
return (
1 == n ? GRID
: (
2 == n ? HEX
: (
RANDOM
)
)
);
}

PointMode toPointMode( char *s ) {
return toPointMode( atoi( s ) );
}

struct Color {
int r, g, b;
Color() {
this->zero();
}
void zero() {
this->r = this->g = this->b = 0;
}
// TODO: configure color choices from the outside
int random255() {
return rand() % 255;
}
// TODO: configure color choices from the outside
void random() {
this->r = this->random255();
this->g = this->random255();
this->b = this->random255();
}
void copy( Color &that ) {
this->r = that.r;
this->g = that.g;
this->b = that.b;
}
void add( Color &that ) {
this->r += that.r;
this->g += that.g;
this->b += that.b;
}
void div( float f ) {
if ( 0 != f ) {
r /= f;
g /= f;
b /= f;
}
}
int equals( Color &that ) {
return this->r == that.r && this->g == that.g && this->b == that.b;
}
int average( Color &that ) {
this->r = ( this->r + that.r ) / 2;
this->g = ( this->g + that.g ) / 2;
this->b = ( this->b + that.b ) / 2;
}
void print( FILE *fp ) {
fprintf( fp, "%c%c%c", r, g, b );
}
void cap() {
if ( r > 255 ) r = 255;
if ( g > 255 ) g = 255;
if ( b > 255 ) b = 255;
if ( r < 0 ) r = 0;
if ( g < 0 ) g = 0;
if ( b < 0 ) b = 0;
}
};

struct ColoredPoint {
int x, y;
Color color;
ColoredPoint( int x = 0 , int y = 0 ) {
set( x, y );
}
void set( int x, int y ) {
this->x = x;
this->y = y;
this->color.random();
}
void random( int w, int h ) {
this->set( rand() % w, rand() % h );
}
/*
this distance calculation wraps around
*/
int distanceSquared( float x, float y, int w, int h ) {
float xdist = fabs( this->x - x );
float ydist = fabs( this->y - y );

if ( xdist > w / 2 ) {
if ( this->x < x ) {
xdist = this->x + ( w - x );
} else {
xdist = x + ( w - this->x );
}
}
if ( ydist > h / 2 ) {
if ( this->y < y ) {
ydist = this->y + ( h - y );
} else {
ydist = y + ( h - this->y );
}
}
return xdist * xdist + ydist * ydist;
}

void rotate( float a_cos, float a_sin, int w, int h ) {
int dx = x - w / 2;
int dy = y - h / 2;
int fx = dx * a_cos + dy * a_sin;
int fy = dy * a_cos + dx * a_sin;
x = fx;
y = fy;
}
};

struct Voronoi {
int w, h, n;
int antialias;
PointMode mode;
const char *ppm_filename;
float rotation;
float kolor;

Voronoi() {
w = 256;
h = 256;
n = 64;
antialias = 1;
mode = RANDOM;
ppm_filename = "voronoi.ppm";
rotation = 0;
kolor = 0;
}

int usage( char *name ) {
char *basename = strrchr( name, '/' );
if ( NULL != basename ) {
name = &( basename[ 1 ] );
}
printf(
"Usage: %s [options]\n"
"Generate fun Voronoi diagrams\n"
"\n"
"\t-w width width of the image: default is 256\n"
"\t-h width height of the image: default is 256\n"
"\t-f filename filename to write to: default is voronoi.ppm\n"
"\t-m MODE 0 is random, 1 is grid and 2 is hexagons: default is random(0)\n"
"\t-n POINTS number of points to use: default is 64\n"
"\t for non-random mode, it should be a perfect square\n"
"\t and width and height should be divisible by sqrt(n)\n"
"\t eg: if n is 64, width and height could be 256 cuz 0 = 256 %% 8\n"
"\t-a ANTI enable, disable antialiasing: default is on(1)\n"
"\t-k KOLOR this enables a weird color mode, try -k 63: default is 0(off)\n"
"\t-r RADIANS rotate the points... kinda: default is 0\n"
"\n"
"eg: %s -m 2 -k 57 -r 0 && xv -root -quit voronoi.ppm\n"
"eg: %s && xv -root -quit voronoi.ppm\n"
, name
, name
, name
);
return 0;
}

void closestColor( std::vector< ColoredPoint > points, int x, int y, Color &accumulator ) {
n = points.size();
accumulator.zero();

float d = 0;
int closest_distance = 0;
int distance[ n ];

// find the closest point to this point (x,y)
for ( int i = 0 ; i < n ; i++ ) {
distance[ i ] = points[ i ].distanceSquared( x, y, w, h );
if ( 0 == i || distance[ i ] < closest_distance ) {
closest_distance = distance[ i ];
}
}

// sometimes a point is closer to more than one point
for ( int i = 0 ; i < n ; i++ ) {
if ( 0 == kolor ) {
if ( distance[ i ] == closest_distance ) {
d++;
accumulator.add( points[ i ].color );
}
} else {
float f = kolor * ( 1 / float( 1 + distance[ i ] ) );
accumulator.r += points[ i ].color.r * f ;
accumulator.g += points[ i ].color.g * f ;
accumulator.b += points[ i ].color.b * f ;
}
}
accumulator.cap();


accumulator.div( d );
}

int parseArgs( int argc, char *argv[] ) {
int parsedArgs = 1;
for ( int i = 1 ; i < argc && parsedArgs ; i++ ) {
if ( '-' == argv[ i ][ 0 ] && i < argc - 1 ) {
switch ( argv[ i ][ 1 ] ) {
case 'w': w = atoi( argv[ ++i ] ); break;
case 'h': h = atoi( argv[ ++i ] ); break;
case 'n': n = atoi( argv[ ++i ] ); break;
case 'a': antialias = atoi( argv[ ++i ] ); break;
case 'm': mode = toPointMode( argv[ ++i ] ); break;
case 'k': kolor = atof( argv[ ++i ] ); break;
case 'r': rotation = atof( argv[ ++i ] ); break;
case 'f': ppm_filename = argv[ ++i ]; break;
default: parsedArgs = !parsedArgs;
}
} else {
parsedArgs = !parsedArgs;
}
}
return parsedArgs;
}

void initPoints( std::vector< ColoredPoint > &points ) {
int xi = w / floor( sqrt( n ) );
int yi = h / floor( sqrt( n ) );
int q = 0;

switch ( mode ) {
case GRID:
for ( int y = 0 ; y < h ; y += yi ) {
for ( int x = 0 ; x < w ; x += xi ) {
points.push_back( ColoredPoint( x + xi /2, y + yi / 2) );
}
}
break;
case HEX:
for ( int y = 0 ; y < h ; y += yi ) {
for ( int x = ( 0 == q++ % 2 ? 0 : xi / 2 ) ; x < w ; x += xi ) {
points.push_back( ColoredPoint( x, y ) );
}
}
break;
default:
for ( int i = 0 ; i < n ; i++ ) {
ColoredPoint point;
point.random( w, h );
points.push_back( point );
}
}
}

int main( int argc, char *argv[] ) {
int parsedArgs = parseArgs( argc, argv );
if ( !parsedArgs ) {
return usage( argv[ 0 ] );
}
printf( "%dx%d n=%d f=%s\n", w, h, n, ppm_filename );

std::vector< ColoredPoint > points;
initPoints( points );

Color current;
Color previous;

FILE *fp = fopen( ppm_filename, "w" );
fprintf( fp, "P6\n" "%d %d\n" "255\n", w, h );

float a_cos = cos( rotation );
float a_sin = sin( rotation );

for ( int i = 0 ; i < points.size() ; i++ ) {
points[ i ].rotate( a_cos, a_sin, w, h );
}

for ( int y = 0 ; y < h ; y++ ) {
for ( int x = 0 ; x < w ; x++ ) {
closestColor( points, x, y, current );
if ( antialias && 0 != x ) {
// sort of a trailing average to "antialias"
current.average( previous );
previous.copy( current );
}
current.print( fp );
}
}

fclose( fp );

return 0;
}
};

int
main( int argc, char *argv[] ) {
srand( time( NULL ) );
Voronoi diagram;
diagram.main( argc, argv );
}

Change log

r16 by brianin3d on Jun 6, 2009   Diff
add
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 7592 bytes, 328 lines

File properties

svn:executable
*
Powered by Google Project Hosting