-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboundary.c
403 lines (383 loc) · 8.35 KB
/
boundary.c
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* boundary.c --
*
* Boundary Tracing Generation Method, traces the outline of areas
* of a single color and fills them in.
*
* Copyright (c) 1994-1997 Michael R. Ganss. All Rights Reserved.
*
* See section "Copyright" of the on-line help for information on usage and
* redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
*/
#include "conf.h"
#include <tcl.h>
#include <tk.h>
#include "raster.h"
#include "fract.h"
#include "coloring.h"
#include "iterate.h"
#define RIGHT 0
#define LEFT 1
#define UP 2
#define DOWN 3
static int **screen=(int **)NULL,sizex=-1,sizey=-1,offsety=0;
/* Boundary Tracing Method
We start from pixel (x,y) representing (re,im) going right and
keeping the "wall" to our left until we get back to the starting
point. For each pixel, the next pixel visited will be computed
thus:
a) check pixel to left (looking in the current direction, i.e. if
the current direction is right, the pixel to the left is the one
above it). If it's part of the wall (i.e. offscreen or a different
color than the one we're tracking), go to b). If it's not, it is
the next pixel.
b) do same for pixel straight ahead. Go there if it's not part of
the wall.
c) ditto for pixel to the right. If this pixel is also part of the
wall, go back one pixel.
When we're back at the starting point we trace the boundary once
more. Now, whenever we move up, we plot pixels to the right of the
current one until we hit the wall.
This method was inspired by a post on sci.fractals by Maarten
Egmond ([email protected]).
*/
static void Trace(int xoffset, int yoffset, int maxx, int maxy,
int x, int y,
double re, double im)
{
int sx,sy,c,w,dir=RIGHT,fill=0,x1;
int *s;
c=FractColor(FractIterate(re,im,re,im,0)); /* the color we're tracing */
sx=x; sy=y;
do
{
if(!fill&&screen[y][x]<0) /* plot current pixel */
{
screen[y][x]=c;
RasterPutpixel(raster,x,y,c);
if(symmetric)
RasterPutpixel(raster,x,symy-(y-yoffset),c);
}
switch(dir)
{
case RIGHT:
/* check left */
if(y-1>=yoffset)
{
if((w=screen[y-1][x])<0)
{
w=FractColor(FractIterate(re,im-stepy,re,im-stepy,0));
screen[y-1][x]=w;
RasterPutpixel(raster,x,y-1,w);
if(symmetric)
RasterPutpixel(raster,x,symy-(y-1-yoffset),w);
}
if(w==c) /* go left */
{
dir=UP;
im-=stepy;
y--;
break;
}
}
/* check ahead */
if(x+1<=maxx)
{
if((w=screen[y][x+1])<0)
{
w=FractColor(FractIterate(re+stepx,im,re+stepx,im,0));
screen[y][x+1]=w;
RasterPutpixel(raster,x+1,y,w);
if(symmetric)
RasterPutpixel(raster,x+1,symy-(y-yoffset),w);
}
if(w==c) /* go ahead */
{
/* dir is still RIGHT */
re+=stepx;
x++;
break;
}
}
/* check right */
if(y+1<=maxy)
{
if((w=screen[y+1][x])<0)
{
w=FractColor(FractIterate(re,im+stepy,re,im+stepy,0));
screen[y+1][x]=w;
RasterPutpixel(raster,x,y+1,w);
if(symmetric)
RasterPutpixel(raster,x,symy-(y+1-yoffset),w);
}
if(w==c) /* go right */
{
dir=DOWN;
im+=stepy;
y++;
break;
}
}
/* go back */
if(x==sx&&y==sy) /* if direction is RIGHT and we're at the */
/* starting point, going back means return */
return;
dir=LEFT;
re-=stepx;
x--;
break;
case LEFT:
/* check left */
if(y+1<=maxy)
{
if((w=screen[y+1][x])<0)
{
w=FractColor(FractIterate(re,im+stepy,re,im+stepy,0));
screen[y+1][x]=w;
RasterPutpixel(raster,x,y+1,w);
if(symmetric)
RasterPutpixel(raster,x,symy-(y+1-yoffset),w);
}
if(w==c) /* go left */
{
dir=DOWN;
im+=stepy;
y++;
break;
}
}
/* check ahead */
if(x-1>=xoffset)
{
if((w=screen[y][x-1])<0)
{
w=FractColor(FractIterate(re-stepx,im,re-stepx,im,0));
screen[y][x-1]=w;
RasterPutpixel(raster,x-1,y,w);
if(symmetric)
RasterPutpixel(raster,x-1,symy-(y-yoffset),w);
}
if(w==c) /* go ahead */
{
/* dir is still LEFT */
re-=stepx;
x--;
break;
}
}
/* check right */
if(y-1>=yoffset)
{
if((w=screen[y-1][x])<0)
{
w=FractColor(FractIterate(re,im-stepy,re,im-stepy,0));
screen[y-1][x]=w;
RasterPutpixel(raster,x,y-1,w);
if(symmetric)
RasterPutpixel(raster,x,symy-(y-1-yoffset),w);
}
if(w==c) /* go right */
{
dir=UP;
im-=stepy;
y--;
break;
}
}
/* go back */
dir=RIGHT;
re+=stepx;
x++;
break;
case UP:
if(fill) /* fill current line by going right until we hit a */
/* pixel with a different color than c */
{
s=&screen[y][maxx];
x1=-maxx+x+1;
for(; x1!=0; x1++)
{
if(s[x1]<0)
s[x1]=c;
else
if(s[x1]!=c)
break;
}
x1+=maxx;
if(x1-2>x)
{
RasterPutline(raster,x+1,y,x1-1,c);
if(symmetric)
RasterPutline(raster,x+1,symy-y,x1-1,c);
}
}
/* check left */
if(x-1>=xoffset)
{
if((w=screen[y][x-1])<0)
{
w=FractColor(FractIterate(re-stepx,im,re-stepx,im,0));
screen[y][x-1]=w;
RasterPutpixel(raster,x-1,y,w);
if(symmetric)
RasterPutpixel(raster,x-1,symy-(y-yoffset),w);
}
if(w==c) /* go left */
{
dir=LEFT;
re-=stepx;
x--;
break;
}
}
/* check ahead */
if(y-1>=yoffset)
{
if((w=screen[y-1][x])<0)
{
w=FractColor(FractIterate(re,im-stepy,re,im-stepy,0));
screen[y-1][x]=w;
RasterPutpixel(raster,x,y-1,w);
if(symmetric)
RasterPutpixel(raster,x,symy-(y-1-yoffset),w);
}
if(w==c) /* go ahead */
{
/* dir is still UP */
im-=stepy;
y--;
break;
}
}
/* check right */
if(x+1<=maxx)
{
if((w=screen[y][x+1])<0)
{
w=FractColor(FractIterate(re+stepx,im,re+stepx,im,0));
screen[y][x+1]=w;
RasterPutpixel(raster,x+1,y,w);
if(symmetric)
RasterPutpixel(raster,x+1,symy-(y-yoffset),w);
}
if(w==c) /* go right */
{
dir=RIGHT;
re+=stepx;
x++;
break;
}
}
/* go back */
dir=DOWN;
im+=stepy;
y++;
break;
case DOWN:
/* check left */
if(x+1<=maxx)
{
if((w=screen[y][x+1])<0)
{
w=FractColor(FractIterate(re+stepx,im,re+stepx,im,0));
screen[y][x+1]=w;
RasterPutpixel(raster,x+1,y,w);
if(symmetric)
RasterPutpixel(raster,x+1,symy-(y-yoffset),w);
}
if(w==c) /* go left */
{
dir=RIGHT;
re+=stepx;
x++;
break;
}
}
/* check ahead */
if(y+1<=maxy)
{
if((w=screen[y+1][x])<0)
{
w=FractColor(FractIterate(re,im+stepy,re,im+stepy,0));
screen[y+1][x]=w;
RasterPutpixel(raster,x,y+1,w);
if(symmetric)
RasterPutpixel(raster,x,symy-(y+1-yoffset),w);
}
if(w==c) /* go ahead */
{
/* dir is still DOWN */
im+=stepy;
y++;
break;
}
}
/* check right */
if(x-1>=xoffset)
{
if((w=screen[y][x-1])<0)
{
w=FractColor(FractIterate(re-stepx,im,re-stepx,im,0));
screen[y][x-1]=w;
RasterPutpixel(raster,x-1,y,w);
if(symmetric)
RasterPutpixel(raster,x-1,symy-(y-yoffset),w);
}
if(w==c) /* go right */
{
dir=LEFT;
re-=stepx;
x--;
break;
}
}
/* go back */
dir=UP;
im-=stepy;
y--;
break;
}
if(x==sx&&y==sy) /* we're back where we started */
{
if(fill) /* we're done */
return;
fill=1; /* trace again, filling encircled area */
dir=RIGHT;
}
}
while(1);
}
void BoundaryCompute(double minr, double mini, double maxr, double maxi,
int xoffset, int yoffset, int maxx, int maxy)
{
int x,y;
double re,im;
if(maxx!=sizex||maxy!=sizey||yoffset!=offsety)
{
for(y=offsety; y<=sizey; y++)
free(screen[y]);
if(screen)
free(screen);
screen=(int **)malloc((maxy+1)*sizeof(int*));
for(y=0; y<yoffset; y++)
screen[y]=(int *)NULL;
for(y=yoffset; y<=maxy; y++)
screen[y]=(int *)malloc((maxx+1)*sizeof(int));
sizex=maxx;
sizey=maxy;
offsety=yoffset;
}
for(y=yoffset; y<=maxy; y++)
memset(screen[y],-1,(maxx+1)*sizeof(int));
for(y=yoffset, im=mini; y<=maxy; y++, im+=stepy)
{
for(x=xoffset, re=minr; x<=maxx; x++, re+=stepx)
if(screen[y][x]<0)
Trace(xoffset,yoffset,maxx,maxy,x,y,re,im);
#ifndef MULTITHREADING
FractCheckEvent();
#endif
if(abbruch) return;
}
}