Graph fill algorithm (scan line seed fill algorithm)

Source: Internet
Author: User
Tags printf

A lot of days did not go to the graphics class, today I heard to pay a number of graphic learning algorithm implementation program, it took nearly a day to finally pass the program debugging, but to the laboratory, just know that the teacher did not have time to check. Hey, White wrote the ^_^. How can you really understand the true meaning of the algorithm without actually writing it? Now put the program out for the future to learn younger brother to use. (In fact, if only copy of the program, it is no use to yourself, in addition to sometimes to deceive teachers, may be helpful to the results.) That's all. )

/**
* Author: Lao Xie
* Last Modified Date: 2006.4.14
* email:do_while@sohu.com
* Function Description:
* The graph filling algorithm
* Scanning line seed filling algorithm;
**/
/**
* The program also requires a stack that stores a point (DOT) structure
* and graphical mode initialization program
* These two programs are implemented in the back of the program
**/

/*linescan.c*/

#include "Graphics.h"
#include "Dos.h"
#include "stdio.h"
#include "PStack.h"

/**
* Initialize Scan 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 seed, and find the next two lines of seed, into 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; /* is the left and right coordinate values of the segment */
Point Point;

CurX = seed. X

/**
* It's not a border, it's not filled, go right
**/
while (GetPixel (curx,cury)! = bordercolor &&
GetPixel (curx,cury)! = FillColor)
{
if (CurX > 600)
{
printf ("CurX =%d", CurX);
Return
}

curx++;
}

XR =--curx;

/**
* Start padding to the left
**/
while (GetPixel (curx,cury)! = bordercolor &&
GetPixel (curx,cury)! = FillColor)
{
Putpixel (Curx,cury,fillcolor);

if (CurX <= 0)
{
/* Coordinate 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 a filled point, record, as a seed
Continue checking until the XR is checked;
**/
Tag = 0;/* Initial value must be 0*/
CurX = XL;
while (CurX <= XR)
{
/* Find blank and mark (tag = 1) */
if (GetPixel (curx,cury-1)! = bordercolor &&
GetPixel (curx,cury-1)! = FillColor)
{
tag = 1; /* There is a blank * *
}
Else
{
/**
* There are no gaps to seed
* Blank end, save seed, and empty mark
**/
if (tag = = 1)
{
curx--;
Point. X = CurX;
Point. Y = cury-1;
Push (S,point);

/* A blank can only have one seed */
tag = 0;
}
}
curx++;
}
if (tag = = 1)
{
curx--;
Point. X = CurX;
Point. Y = cury-1;
Push (S,point);
}
/**
Check the y+1 line from the left until you encounter a boundary or a filled point, record, as a seed
Continue checking until the XR is checked;
**/
CurX = XL;
Tag = 0;/* Initial value must be 0*/
while (CurX <= XR)
{
/* Find blank and mark (tag = 1) */
if (GetPixel (curx,cury+1)! = bordercolor &&
GetPixel (curx,cury+1)! = FillColor)
{
tag = 1; /* There is a blank * *
}
Else
{
/**
* There are no gaps to seed
* Blank end, save seed, and empty mark
**/
if (tag = = 1)
{
curx--;
Point. X = CurX;
Point. Y = cury+1;
Push (S,point);

/* A blank can only have one seed */
tag = 0;
}
}
curx++;
}
if (tag = = 1)
{

curx--;
Point. X = CurX;
Point. Y = cury+1;
Push (S,point);
}

}

/**
* Fills the specified area with the specified color (and specifies 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 Examples
**/
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*/
/******************* the declaration of the Stack ******************************/
/**
* Define a stack that holds the Piont structure (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 that holds the Piont structure (dynamic array implementation)
**/
#include "Myhead.h"
#include "PStack.h"

/* Return when stack is empty */
Point Npoint;

/* Initialize 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*/
/* Graphical mode initialization */
int init ()
{
int driver = Detect,mode = 0;
Initgraph (&driver,&mode, "e://tc//tc2");
return 1;
}

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.