⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tianchong .c

📁 可以实现画线
💻 C
字号:
  #include   <graphics.h>
  #include   <stdio.h>
  #include   <conio.h>
  #include   <stdlib.h>
  #include   <malloc.h>  
#define closegr closegraph 

void initgr(void) 
{
  int driver = DETECT, mode = 0; 
  initgraph(&driver, &mode, "");
} 

enum BOOL{FALSE = 0, TRUE = 1};
typedef struct{
    int y;
    int xLeft;
    int xRight;
}Span;
typedef struct stacknode 
{ 
Span span;
struct stacknode *next; 
}stacknode;
typedef struct 
{ 
stacknode *top; 
}linkstack; 

void PushStack(linkstack *s, Span *span) 
{ 
stacknode *p=(stacknode*)malloc(sizeof(stacknode)); 
p->span.y = span->y;
p->span.xLeft = span->xLeft;
p->span.xRight = span->xRight; 

p->next=s->top;
s->top=p; 

} 
void PopStack(linkstack *s,Span *span) 
{ 
int x; 
stacknode *p=s->top; 
span->y = p->span.y;
span->xLeft = p->span.xLeft;
span->xRight = p->span.xRight; 
s->top=p->next; 
free(p);  
}
void SetStackEmpty(linkstack *s)
{
    stacknode *p=s->top;
    while( s->top != NULL)
    {
        free(p); 
        s->top=p->next;  
    }
}
int IsStackEmpty(linkstack *s)
{
    if(s->top == NULL) 
        return 1;
    else
        return 0;
}
void ScanLineFill4(int x,int y,int oldColor,int newColor)
{
    int xLeft,xRight;
    int i; 

    enum BOOL isLeftEndSet, spanNeedFill;
    Span span; 

    linkstack *s=(linkstack*)malloc(sizeof(linkstack));
    s->top = NULL; 
    i = x;
    while(getpixel(i,y) == oldColor)
    {
        putpixel(i,y,newColor);
        i++;
    } 

    span.xRight = i - 1;  
    i = x - 1;
    while(getpixel(i,y) == oldColor)
    {
        putpixel(i,y,newColor);
        i--;
    } 

    span.xLeft = i + 1;   
    SetStackEmpty(s);
    span.y = y;
    PushStack(s,&span);

    while( ! IsStackEmpty(s) )
    {
        
        PopStack(s, &span);
        
        y = span.y + 1;
        xRight = span.xRight;
        i = span.xLeft - 1;
        isLeftEndSet = FALSE; 

        while(getpixel(i,y) == oldColor)
        {
            putpixel(i, y, newColor);
            i--;
        } 

        if( i != span.xLeft - 1)
        {
            isLeftEndSet = TRUE;
            xLeft = i + 1;
        } 

        i = span.xLeft;
        while( i < xRight)
        {
            spanNeedFill = FALSE;
            while(getpixel(i,y) == oldColor) 
            {
                if( ! spanNeedFill)
                {
                    spanNeedFill = TRUE;
                    if( ! isLeftEndSet)
                    {
                        isLeftEndSet = TRUE;
                        xLeft = i;
                    }
                } 

                putpixel(i,y,newColor);
                i++;
            } 

            if( spanNeedFill )
            {
                span.y = y;
                span.xLeft = xLeft;
                span.xRight = i - 1;
                PushStack(s, &span); 
                isLeftEndSet = FALSE;
                spanNeedFill = FALSE;
            } 

             while(getpixel(i,y) != oldColor)
                i++;
        }/*end of while( i < xRight)    */ 

  

       
        y = y - 2; 


        xRight = span.xRight;
        i = span.xLeft - 1;
        isLeftEndSet = FALSE; 

        while(getpixel(i,y) == oldColor)
        {
            putpixel(i, y, newColor);
            i--;
        } 

        if( i != span.xLeft - 1)
        {
            isLeftEndSet = TRUE;
            xLeft = i + 1;
        } 

        i = span.xLeft;
        while( i < xRight)
        {
            spanNeedFill = FALSE;
            while(getpixel(i,y) == oldColor) 
            {
                if( ! spanNeedFill)
                {
                    spanNeedFill = TRUE;
                    if( ! isLeftEndSet)
                    {
                        isLeftEndSet = TRUE;
                        xLeft = i;
                    }
                } 

                putpixel(i,y,newColor);
                i++;
            } 

            if( spanNeedFill )
            {
                span.y = y;
                span.xLeft = xLeft;
                span.xRight = i - 1;
                PushStack(s, &span); 
                isLeftEndSet = FALSE;
                spanNeedFill = FALSE;
            } 

             while(getpixel(i,y) != oldColor) 
                i++;
        }/*end of while( i < xRight)    */ 

    }/*end of while( ! isStackEmpty() ) */ 

}/*end of ScanLineFill4()               */ 

int main()
{   int   poly[10]={100,100,150,100,150,200,100,200,100,100};
    initgr(); 
    /*setbkcolor(3);*/
    setcolor(5);
    cleardevice();
    drawpoly(5,poly);
    ScanLineFill4(150,150,0,14); 
    getch(); 
    closegr(); 
    return 0;
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -