Showing posts with label GRAPHICS LAB. Show all posts
Showing posts with label GRAPHICS LAB. Show all posts

Saturday, 8 March 2014

FLOOD FILL ALGORITHM


/*CSEMATTER.BLOGSPOT.IN
PROGRAM-TO IMPLEMENT FLOOD FILL ALGORITHM*/

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void fill_right(int x,int y)
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_right(++x,y);
x = x - 1 ;
fill_right(x,y-1);
fill_right(x,y+1);
}
}
void fill_left(int x,int y)
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_left(--x,y);
x = x + 1 ;
fill_left(x,y-1);
fill_left(x,y+1);
}
}
int main()
{
int x , y ,a[10][10];
int gd=DETECT, gm ,n,i;
initgraph(&gd,&gm,"c:\tc\bgi");
printf("Enter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\nEnter the cordinates of polygon :\n\n ");
for(i=0;i<n;i++)
{
printf("\tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
printf("\n\nEnter the seed pt. : ");
scanf("%d %d",&x,&y);
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
fill_right(x,y);
fill_left(x-1,y);
getch();
}

BOUNDARY FILL ALGORITHM


/*CSEMATTER.BLOGSPOT.IN
PROGRAM-TO IMPLELMENT BOUNDARY FILL ALGORITHM*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void colouring(int,int,int,int,int);
int main()
{
    int gd=DETECT,gm,x,y,a=0,b=0,c,tempx,tempy,left,right,top,bottom;
    initgraph(&gd,&gm,"C:\TC\BGI");
    printf("Enter the left, top, right & bottom coordinates of rectangle: ");
    scanf("%d %d %d %d",&left,&top,&right,&bottom);
    rectangle(left,top,right,bottom);
    printf("Enter the corresponding code of color to be filled");
    scanf("%d",&c);
    tempx=(right-left)/2;
    tempy=(bottom-top)/2;
    x=tempx+left;
    y=tempy+top;
    printf("%d %d",x,y);
    while(a!=tempx | b!=tempy)
    {printf("%d %d\n",a,b);
        colouring(x,y,a,b,c);
        if(a<=tempx)
        a++;
        if(b<=tempy)
        b++;
    }
    getch();
    closegraph();
}

void colouring(int x,int y,int a,int b,int c)
{
    int i,j;
    for(i=0; i<=a; i++)
    {
        for(j=0; j<=b; j++)
        {
            putpixel(x,y,c);
            putpixel(x-i,y,c);
            putpixel(x+i,y,c);
            putpixel(x,y-j,c);
            putpixel(x,y+j,c);
            putpixel(x-i,y-j,c);
            putpixel(x-i,y+j,c);
            putpixel(x+i,y-j,c);
            putpixel(x+i,y+j,c);
        }
    }
}

3D TRANSFORMATION AND ROTATION

/*CSEMATTER.BLOGSPOT.IN
PROGRAM-3D TRANSFORMATION AND ROTATION*/



#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<process.h>

#include<graphics.h>




void draw();

void rotate();

int x12,x22,y12,y22,mx,my,depth;

int main()

{


    int gd=DETECT,gm,c;

    initgraph(&gd,&gm,"C:\tc\bgi");

    printf("\n3D Transformation Rotating\n\n");

    printf("\nEnter 1st top value(x12,y12):");

    scanf("%d%d",&x12,&y12);

    printf("Enter right bottom value(x2,y2):");

    scanf("%d%d",&x22,&y22);

    depth=(x22-x12)/4;

    mx=(x12+x22)/2;

    my=(y12+y22)/2;

    draw();

    getch();

    cleardevice();

    rotate();

    getch();

}



void draw()

{

    bar3d(x12,y12,x22,y22,depth,1);

}



void rotate()

{

    float t;

    int a1,b1,a2,b2,dep;

    printf("Enter the angle to rotate=");

    scanf("%f",&t);

    t=t*(3.14/180);

    a1=mx+(x12-mx)*cos(t)-(y12-my)*sin(t);

    a2=mx+(x22-mx)*cos(t)-(y22-my)*sin(t);

    b1=my+(x12-mx)*sin(t)-(y12-my)*cos(t);

    b2=my+(x22-mx)*sin(t)-(y22-my)*cos(t);

    if(a2>a1)

       dep=(a2-a1)/4;

    else

      dep=(a1-a2)/4;

    bar3d(a1,b1,a2,b2,dep,1);

    setcolor(5);

    //draw();

}