I haven't gone to the class of morphology for many days. Today I heard that I want to submit several graphics algorithm implementation programs. It took nearly a day to debug the program. But in the lab, I know that the teacher has no time to check. Ah, I wrote ^_^ in the white text. I laughed, but I don't know how to really understand the true meaning of the algorithm. Now, paste the program for future use. (In fact, if it is just a copy program, it is really useless to yourself, except for sometimes being able to cheat the teacher, it may be helpful to the score. That's all .)
/**
* Author: Thank you!
* Last modification date: 2006.4.14
* Email: do_while@sohu.com
* Function description:
* Graph Filling Algorithm
* Scanning line Seed Filling Algorithm;
**/
/**
* This program also needs a stack that stores the point structure.
* And graphical initialization program
* The implementation programs of these two programs are later
**/
/* Linescan. C */
# Include "graphics. H"
# Include "Dos. H"
# Include "stdio. H"
# Include "pstack. H"
/**
* Initialize the scanning line
* (X, y) must be inside the graph.
**/
Void initscan (pointstack * s, int X, int y)
{
Point P;
P. x = X;
P. Y = y;
Push (S, P );
Delay (4000 );
}
/**
* Fill this line according to the seeds, find the seeds of the upper and lower rows, and import them to the stack.
**/
Void fillthisline (pointstack * s, point seed, color fillcolor, color bordercolor)
{
Int curx = seed. X;
Int Cury = seed. Y;
Int xr = 0;
Int XL = 0;
Int tag = 0;/* respectively indicates the left and right coordinate values of the segment */
Point point;
Curx = seed. X;
/**
* The border is not filled, and the border is always on the right
**/
While (getpixel (curx, Cury )! = Bordercolor &&
Getpixel (curx, Cury )! = Fillcolor)
{
If (curx & gt; 600)
{
Printf ("curx = % d", curx );
Return;
}
Curx ++;
}
Xr = -- curx;
/**
* Start filling left
**/
While (getpixel (curx, Cury )! = Bordercolor &&
Getpixel (curx, Cury )! = Fillcolor)
{
Putpixel (curx, Cury, fillcolor );
If (curx <= 0)
{
/* Coordinates out of bounds */
Printf ("curx = % d/N", curx );
Return;
}
Curx --;
}
XL = ++ curx;
/**
// Check the Y-1 line from the left until you encounter a boundary or filled point, record, as a seed
// Continue the check until XR is found;
**/
Tag = 0;/* The initial value must be 0 */
Curx = XL;
While (curx <= xr)
{
/* Blank and marked (TAG = 1 )*/
If (getpixel (curx, cury-1 )! = Bordercolor &&
Getpixel (curx, cury-1 )! = Fillcolor)
{
Tag = 1;/* blank points */
}
Else
{
/**
* Seeds are available only when blank points exist.
* The white space ends. Save the seed and clear the flag.
**/
If (TAG = 1)
{
Curx --;
Point. x = curx;
Point. Y = cury-1;
Push (S, point );
/* A blank segment can have only one seed */
Tag = 0;
}
}
Curx ++;
}
If (TAG = 1)
{
Curx --;
Point. x = curx;
Point. Y = cury-1;
Push (S, point );
}
/**
// Check the line y + 1 from the left until it encounters a boundary or filled point. record as a seed
// Continue the check until XR is found;
**/
Curx = XL;
Tag = 0;/* The initial value must be 0 */
While (curx <= xr)
{
/* Blank and marked (TAG = 1 )*/
If (getpixel (curx, Cury + 1 )! = Bordercolor &&
Getpixel (curx, Cury + 1 )! = Fillcolor)
{
Tag = 1;/* blank points */
}
Else
{
/**
* Seeds are available only when blank points exist.
* The white space ends. Save the seed and clear the flag.
**/
If (TAG = 1)
{
Curx --;
Point. x = curx;
Point. Y = Cury + 1;
Push (S, point );
/* A blank segment can have only one seed */
Tag = 0;
}
}
Curx ++;
}
If (TAG = 1)
{
Curx --;
Point. x = curx;
Point. Y = Cury + 1;
Push (S, point );
}
}
/**
* Fill the specified region with the specified color (and specify the stack used)
**/
Void fill (pointstack * s, color fillcolor, color bordercolor)
{
Point seed;
While (S-> length> 0)
{
Seed = POP (s );
Fillthisline (S, seed, fillcolor, bordercolor );
}
}
/**
* Application instance
**/
Int main ()
{
Point P;
Pointstack S;
Initstack (& S );
Init ();
Printf ("start! ");
Getch ();
Circle (200,200,100 );
Circle (200,200, 30 );
Circle (200,150, 25 );
Circle (200,170, 20 );
Rectangle (150,210,250,250 );
Getch ();
Initscan (& S, 245,201 );
Fill (& S, Red, White );
Destroy (& S );
Initscan (& S, 201,199 );
Fill (& S, green, white );
Destroy (& S );
Initscan (& S, 201,151 );
Fill (& S, 3, white );
Destroy (& S );
Initscan (& S, 201,139 );
Fill (& S, 9, white );
Destroy (& S );
Initscan (& S, 249,249 );
Fill (& S, 13, white );
Destroy (& S );
Printf ("length of stackt: % d/N", S. Length );
Printf ("Capacity of stackt: % d/N", S. capacity );
Getch ();
Closegraph ();
}
/* Pstack. H */
/******************* Stack declaration ***************** *************/
/**
* Define a stack for storing piont structures (Dynamic Array Implementation)
**/
# Ifndef pointstack_h
# Define pointstack_h
# Include "stdio. H"
# Include "stdlib. H"
/**
* Define the point struct
**/
Typedef struct
{
Int X;
Int y;
} Point;
/**
* Stack
**/
Typedef struct
{
Point * head;
Int length;
Int capacity;
} Pointstack;
/* =================================== */
Bool initstack (pointstack * s );
Void destroy (pointstack * s );
Void push (pointstack * s, point P );
Point POP (pointstack * s );
# Endif
/* Pstack. C */
/**
* Define a stack for storing piont structures (Dynamic Array Implementation)
**/
# Include "myhead. H"
# Include "pstack. H"
/* Returned when the stack is empty */
Point npoint;
/* Initialize the stack */
Bool initstack (pointstack * s)
{
Npoint. x =-1;
Npoint. Y =-1;
S-> length = 0;
S-> capacity = 20;
/* Capacity */
S-> head = (point *) malloc (sizeof (point) * 20 );
If (S-> head = NULL)
{
Printf ("malloc error! ");
Exit (0 );
}
Return true;
}
Void destroy (pointstack * s)
{
Free (S-> head );
S-> length = 0;
S-> capacity = 0;
}
Void push (pointstack * s, point P)
{
Point * temp;
Int I;
If (S-> length> = s-> capacity)
{
Temp = (point *) malloc (sizeof (point) * (S-> capacity + 20 ));
If (temp! = NULL)
{
/* Success */
For (I = 0; I <s-> length; I ++)
{
Temp [I] = s-> head [I];
}
S-> capacity + = 20;
Free (S-> head );
S-> head = temp;
}
Else
{
Printf ("malloc error! = N ");
Exit (0 );
}
}
/* Push */
S-> head [S-> length] = P;
S-> length ++;
}
Point POP (pointstack * s)
{
Point temp;
If (S-> length = 0)
{
Return 0;
}
Else
{
S-> length --;
Temp = s-> head [S-> length];
Return temp;
}
}
/* Myhead. C */
/* Image Mode Initialization */
Int Init ()
{
Int driver = detect, mode = 0;
Initgraph (& driver, & mode, "E: // TC // TC2 ");
Return 1;
}