Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

An Efficient Flood Visit Algorithm


August 1994/An Efficient Flood Visit Algorithm/Listing 1

Listing 1 An algorithm which prevents a child's shadow from falling on its parent line

/*****************************************************
 * UFLOOD.C - unoptimized flood visit
 *
 * for ANSI C
 *
 * by Anton Treuenfels
 * last revision:  04/12/94
 ****************************************************/

/*****************************
 * HEADER SECTION - UFLOOD.H *
 ****************************/

#ifndef SEEN_UFLOOD
#define SEEN_UFLOOD

/* function prototype */

int uflood(int, int, int (*)(int. int));

#endif

/***************************
 * CODE SECTION - UFLOOD.C *
 **************************/

#include <stdlib.h>

#include "usrdef.h"

/* line shadow */

typedef struct shdw {
   struct shdw *next;  /* forward link */
   int lft, rgt;       /* endpoints */
   int row, par;       /* row and parent row */
   Boolean ok;         /* valid flag */
} shadow;

/* shadow variables */

static shadow *seedShadow;  /* current shadow */
static shadow *shadowHead;  /* other pending shadows */

/* visit coordinate function */

static int (*isInterior)(int, int);

/****************************************************/

/* make new shadow */

static void newshadow(int slft, int srgt, int srow, int prow) {

   shadow *new;

   if ((new = malloc(sizeof(shadow))) == NULL)
      exit(EXIT_FAILURE);

   new->lft = slft;    new->rgt = srgt;
   new->row = srow;    new->par = prow;
   new->ok = TRUE;
   new->next = shadowHead;
   shadowHead = new;
}

/* clip off ends of overlapped shadow */

static void clipshadow(shadow *s, shadow *clip) {

   if (s->lft < (clip->lft - 1))
      newshadow(s->lft, clip->lft - 2, s->row, s->par);
   if (s->rgt > (clip->rgt + 1))
      newshadow(clip->rgt + 2, s->rgt, s->row, s->par);
}

/* remove overlapped segment from shadow pair */

static void removeoverlap(shadow *o) {

   shadow *s;

   for (s = shadowHead; s; s = s->next) {
      if ((s->row == o->par) && (s->ok) &&
         (s->lft <= o->rgt) && (s->rgt >= o->lft)) {
            clipshadow(s, o);
            if (o != seedShadow)
               clipshadow(o, s);
            s->ok= o->ok = FALSE;
            return;
      }
   }
}

/* make shadows of one child line */

static void makeshadows(int lft, int rgt, int row) {

   shadow *p;

   newshadow(lft, rgt, row + 1, row);
   newshadow(lft, rgt, row - 1, row);

   removeoverlap(seedShadow);

   for (p = shadowHead; p; p = p->next) {
      if ((p->row == row) && (p->ok) &&
            !((p->lft > rgt) || (p->rgt < lft)))
         removeoverlap(p);
   }
}

/* fill all child lines found within one shadow */

static void fillshadow(void) {

   int col, row, lft;

    row = seedShadow->row;

   for (col = seedShadow->lft; col <= seedShadow->rgt;
          col++) {
      if ((*isInterior)(col, row)) {
         if ((lft = col) == seedShadow->lft) {
            while ((*isInterior)(--lft, row))
               ;
            lft++;
         }
         while ((*isInterior)(++col, row))
            ;

         makeshadows(lft, col - 1, row);
      }
   }
}

/* flood visit */

int uflood(int seedx, int seedy, int (*visit)(int, int)) {

   isInterior = visit;
   shadowHead = NULL;
   newshadow(seedx, seedx, seedy. seedy);
   while (shadowHead) {
      seedShadow = shadowHead;
      shadowHead = shadowHead-->next;
      if (seedShadow-->ok)
         fillshadow();
      free(seedShadow);
   }
}

/* End of File

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.