0

我对代码进行了一些更改。我得到的代码和输出如下所示。我想用边界颜色以外的颜色填充两个相交圆的公共区域。绘制了两个圆圈,但公共区域保持不变,我正在使用洪水填充来填充公共区域。

在此处输入图像描述

#include<stdio.h>
#include<GL/glut.h>

struct Color{
  GLfloat red, green, blue;
};

struct Color getPixel(GLint x, GLint y){        // gets the color of the pixel at (x,y) 
    struct Color c;
    GLfloat color[4];
    glReadPixels(x,y,1,1,GL_RGBA, GL_FLOAT, color);
    c.red = color[0]; 
    c.green = color[1];     
    c.blue = color[2];  
    return c;
}

void setPixel(GLint x, GLint y,struct Color c){
     printf("\n i am at the cordinate %d and %d",x,y);
    glColor3f(c.red, c.green, c.blue);
    glBegin(GL_POINTS);
    glVertex2i(x,y);
    glEnd();
}

void init()
     {
        glClearColor(1.0,1.0,1.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
        gluOrtho2D(0.0,300.0,0.0,300.0);
     }

void drawPixel(int x,int y)
{
    glBegin(GL_POINTS);
    glVertex2i(x,y);
    glEnd();
    glFlush();
}

void Boundary_fill(GLint x,GLint y,struct Color thisColor) {       
    struct Color boundary_color;
    boundary_color.red=0.0;
    boundary_color.green=0.0;
    boundary_color.blue=1.0;
    struct Color nextpixel=getPixel(x,y);
    printf("\n colour of pixel pixel to be filled=%f,%f,%f",nextpixel.red,nextpixel.green,nextpixel.blue);
    if((nextpixel.red!=boundary_color.red)&&(nextpixel.blue!=boundary_color.blue)&&(nextpixel.green!=boundary_color.green) || (nextpixel.red!=thisColor.red) && (nextpixel.blue!=thisColor.blue)&&(nextpixel.green!=thisColor.green)){

     setPixel(x,y,thisColor);
     Boundary_fill((x+1),y,thisColor);  
     Boundary_fill((x-1),y,thisColor);
     Boundary_fill(x,(y+1),thisColor);
     Boundary_fill(x,(y-1),thisColor);
    }

}

void draw(int x1,int y1, int x, int y){
    drawPixel(x1+x,y1+y);//quadrant1
    drawPixel(x1+x,y1-y);//quadrant2
    drawPixel(x1-x,y1+y);//quadrant3
    drawPixel(x1-x,y1-y);//quadrant4
    drawPixel(x1+y,y1+x);//quadrant5
    drawPixel(x1+y,y1-x);//quadrant6
    drawPixel(x1-y,y1+x);//quadrant7
    drawPixel(x1-y,y1-x);//quadrant8
}

void circle(int px,int py,int r){
printf("\n xcenter=%d,ycenter=%d,radius=%d",px,py,r);
    int a,b;
    float p;
    a=0;
    b=r;
    p=(5/4)-r;

    while(a<=b){
        draw(px,py,a,b);
        if(p<0){
            p=p+(2*a)+1;
        }
        else{
            b=b-1;
            p=p+(2*a)+1-(2*b);
        }
        a=a+1;
    }
}


void Circle(void)
{
    struct Color thisColor;
    thisColor.red=0.0;
    thisColor.blue=0.0;
    thisColor.green=0.0;
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0,0.0,1.0); //blue colour
    glPointSize(2.0);
    GLint x0 = 100;
    GLint y0 = 150;
    circle(x0,y0,50);
    glColor3f(0.0,0.0,1.0);
    circle(x0+50,y0,25);
     Boundary_fill(x0+25,y0,thisColor);
    glutSwapBuffers();
}

void main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(400,400);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Boundary fill in a circle");
    init();
    glutDisplayFunc(Circle);
    glutMainLoop();
}
4

0 回答 0