0% found this document useful (0 votes)
47 views19 pages

CG Final

The flood fill algorithm recursively fills an area bounded by a boundary pixel value by replacing the color of the pixel being examined with the replacement color. It begins by examining the target pixel and filling it if its color matches the boundary pixel value, then examining and filling the neighbors of that pixel if their colors also match the boundary value, continuing recursively until all pixels connected to the target pixel that match the boundary value have been filled. The algorithm terminates when it can no longer find any neighboring pixels to fill that match the boundary value.

Uploaded by

babu roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views19 pages

CG Final

The flood fill algorithm recursively fills an area bounded by a boundary pixel value by replacing the color of the pixel being examined with the replacement color. It begins by examining the target pixel and filling it if its color matches the boundary pixel value, then examining and filling the neighbors of that pixel if their colors also match the boundary value, continuing recursively until all pixels connected to the target pixel that match the boundary value have been filled. The algorithm terminates when it can no longer find any neighboring pixels to fill that match the boundary value.

Uploaded by

babu roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Date:

Q1. Write a C program to implement DDA Line Drawing


Algorithm.
Source code:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main() {
int gd = DETECT ,gm, i;
float x, y,dx,dy,steps;
float x0, x1, y0, y1;
initgraph(&gd, &gm, "C:\\TC\\BGI");
printf("Enter the value of x0 and y0 : ");
scanf("%f%f",&x0,&y0);
printf("Enter the value of x1 and y1: ");
scanf("%f%f",&x1,&y1);
dx = (float)(x1 - x0);
dy = (float)(y1 - y0);
if(dx>=dy) {
steps = dx;
}
else
{
steps = dy;
}
dx = dx/steps;
dy = dy/steps;
x = x0;
y = y0;
i = 1;
while(i<= steps) {
putpixel(x, y, RED);
x += dx;
y += dy;
i=i+1;
}
getch();
closegraph();
}

1
Date:

Output:

2
Teacher’s Signature
Date:

Q2. Write a C program to implement Bresenham’s Line Drawing


Algorithm.
Source code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd = DETECT, gm;
int dx, dy, p, end;
float x1, x2, y1, y2, x, y;
initgraph(&gd, &gm, "c:\tc\bgi");
printf("Enter Value of X1: ");
scanf("%f", &x1);
printf("Enter Value of Y1: ");
scanf("%f", &y1);
printf("Enter Value of X2: ");
scanf("%f", &x2);
printf("Enter Value of Y2: ");
scanf("%f", &y2);
dx = abs(x1 - x2);
dy = abs(y1 - y2);
p = 2 * dy - dx;
if(x1 > x2)
{
x = x2;
y = y2;
end = x1;
}
else
{
x = x1;
y = y1;
end = x2;
}
putpixel(x, y, 10);
while(x < end)
{
x = x + 1;
if(p < 0)
{
p = p + 2 * dy;

3
Date:

}
else
{
y = y + 1;
p = p + 2 * (dy - dx);
}
putpixel(x, y, 10);
}
getch();
}

Output:

4
Teacher’s Signature
Date:

Q3. Write a C program to implement Mid- Point Circle Drawing


Algortihm.
Source code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int main()
{
int xc,yc,x,y,d,r,del,big;
int gd=DETECT,gm;
printf("\nEnter the center of the circle (xc,yc):");
scanf("%d%d",&xc,&yc);
printf("\nEnter the radius of the circle:");
scanf("%d",&r);
initgraph(&gd,&gm,"");
x=getmaxx()/2;
y=getmaxy()/2;
setbkcolor(WHITE);
setcolor(GREEN);
line(0,y,2*x,y);
line(x,0,x,2*y);
big=0;
xc+=x;
yc=y-yc;
d=(1-r);
putpixel((xc+big),(yc+r),5);
putpixel((xc+big),(yc-r),5);
putpixel((xc-big),(yc+r),5);
putpixel((xc-big),(yc-r),5);
putpixel((xc+r),(yc+big),5);
putpixel((xc+r),(yc-big),5);
putpixel((xc-r),(yc+big),5);
putpixel((xc-r),(yc-big),5);
while(r>=big)
{
big+=1;
if(d<0)
d+=(2*big)+1;
else
{
r-=1;

5
Date:

d+=2*(big-r)+1;
}
putpixel((xc+big),(yc+r),5);
putpixel((xc+big),(yc-r),5);
putpixel((xc-big),(yc+r),5);
putpixel((xc-big),(yc-r),5);
putpixel((xc+r),(yc+big),5);
putpixel((xc+r),(yc-big),5);
putpixel((xc-r),(yc+big),5);
putpixel((xc-r),(yc-big),5);
}
getch();
}

Output:

6
Teacher’s Signature
Date:

Q4. Write a C program to implement Ellipse Drawing Algorithm.


Source code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int main()
{
int gd=DETECT,gm;
float a,b,xc,yc,x,y,fx,fy,p,xm,ym;
printf("\nEnter the semi major axis(a,b):");
scanf("%f%f",&a,&b);
printf("\nEnter the center(xc,yc):");
scanf("%f%f",&xc,&yc);
initgraph(&gd,&gm,"NULL");
xm=getmaxx()/2;
ym=getmaxy()/2;
setbkcolor(WHITE);
setcolor(GREEN);
line(0,ym,2*xm,ym);
line(xm,0,xm,2*ym);
x=0,y=b;
fx=0,fy=2*a*a*b;
p=(b*b)-(a*a*b)+(a*a)/4.0;
xc+=xm;
yc=ym-yc;
putpixel((xc+x),(yc+y),1);
putpixel((xc-x),(yc+y),1);
putpixel((xc+x),(yc-y),1);
putpixel((xc-x),(yc-y),1);
while(fx<fy)
{
x+=1;
fx=fx+2*b*b;
if(p>=0)
{
y-=1;
fy=fy-2*a*a;
}
if(p<0)
p=p+b*b+fx;
else

7
Date:

p=p+b*b+fx-fy;
putpixel((xc+x),(yc+y),1);
putpixel((xc-x),(yc+y),1);
putpixel((xc+x),(yc-y),1);
putpixel((xc-x),(yc-y),1);
}
p=(b*b)*pow((x+0.5),2)+(a*a)*pow((y-1),2)-(a*a)*(b*b);
while(y>0)
{
y-=1;
fy=fy-2*a*a;
if(p<0)
{
x+=1;
fx=fx+2*b*b;
}
if(p>=0)
p=p+a*a-fy;
else
p=p+a*a-fy+fx;
putpixel((xc+x),(yc+y),1);
putpixel((xc-x),(yc+y),1);
putpixel((xc+x),(yc-y),1);
putpixel((xc-x),(yc-y),1);
}
getch();
closegraph();
}

Output:

8
Teacher’s Signature
Date:

Q5. Write a C program to implement Scaling of a Triangle.


Source code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main(){
int x,y,x1,y1,x2,y2;
int scl_fctr_x,scl_fctr_y;
int gd=DETECT,gm;
initgraph(&gd,&gm,"NULL");
printf("\t\t\t********** Scaling ***********\n");
printf("\n\t\t\t Please enter first coordinate of Triangle = ");
scanf("%d %d",&x,&y);
printf("\n\t\t\t Please enter second coordinate of Triangle = ");
scanf("%d %d",&x1,&y1);
printf("\n\t\t\t Please enter third coordinate of Triangle = ");
scanf("%d %d",&x2,&y2);
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
printf("\n\t\t\t Now Enter Scaling factor x and y = ");
scanf("%d %d",&scl_fctr_x,&scl_fctr_y);
x = x* scl_fctr_x;
x1 = x1* scl_fctr_x;
x2 = x2* scl_fctr_x;
y = y* scl_fctr_y;
y1 = y1* scl_fctr_y;
y2= y2 * scl_fctr_y ;
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
}

9
Date:

Output:

10
Teacher’s Signature
Date:

Q6. Write a C program to implement Translation of a line.


Source code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int main()
{
int xs,ys,xe,ye,tx,ty,x,y;
int gd=DETECT,gm;
printf("\nEnter the initial line position(xs,ys):");
scanf("%d%d",&xs,&ys);
printf("\nEnter the final line position(xe,ye):");
scanf("%d%d",&xe,&ye);
printf("\nEnter the translation vector(tx,ty):");
scanf("%d%d",&tx,&ty);
initgraph(&gd,&gm,"");
setbkcolor(WHITE);
setcolor(GREEN);
x=getmaxx()/2;
y=getmaxy()/2;
line(0,y,2*x,y);
line(x,0,x,2*y);
setcolor(RED);
line(x+xs,y-ys,x+xe,y-ye);
xs+=tx;
xe+=tx;
ys+=ty;
ye+=ty;
setcolor(5);
line(x+xs,y-ys,x+xe,y-ye);
getch();
closegraph();
}

11
Date:

Output:

12
Teacher’s Signature
Date:

Q7. Write a C program to implement Rotation of a


Line(Arbitrary Point).
Source code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int main()
{
int gd=DETECT,gm;
int x1,y1,x,y,radian,xv,yv;
printf("ENTER THE FINAL CO-ORDINATES:");
scanf("%d%d",&x1,&y1);
printf("ENTER THE ANGLE FOR ROTATION:");
scanf("%d",&radian);
initgraph(&gd,&gm,"");
xv=getmaxx()/2;
yv=getmaxy()/2;
setbkcolor(BLUE);
setcolor(RED);
line(0,yv,2*xv,yv);
line(xv,0,xv,2*yv);
setcolor(WHITE);
line(xv,yv,x1+xv,yv-y1);
x=(x1*cos(radian))-(y1*sin(radian));
y=(x1*sin(radian))+(y1*cos(radian));
line(xv,yv,x+xv,yv-y);
getch();
closegraph();
}

13
Date:

Output:

14
Teacher’s Signature
Date:

Q8. Write a C program to implement Reflection of a Triangle.


Source code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int x1,y1,x2,y2,x3,y3,x,y;
int gd=DETECT,gm;
printf("\nEnter the first co-ordinate of the triangle:");
scanf("%d%d",&x1,&y1);
printf("\nEnter the second co-ordinate of the triangle:");
scanf("%d%d",&x2,&y2);
printf("\nEnter the third co-ordinate of the triangle:");
scanf("%d%d",&x3,&y3);
initgraph(&gd,&gm,"NULL");
x=getmaxx()/2;
y=getmaxy()/2;
setbkcolor(WHITE);
setcolor(GREEN);
line(0,y,2*x,y);
line(x,0,x,2*y);
setcolor(RED);
line(x+x1,y-y1,x+x2,y-y2);
line(x+x2,y-y3,x+x3,y-y3);
line(x+x3,y-y3,x+x1,y-y1);
y1*=-1;
y2*=-1;
y3*=-1;
setcolor(BLUE);
line(x+x1,y-y1,x+x2,y-y2);
line(x+x2,y-y3,x+x3,y-y3);
line(x+x3,y-y3,x+x1,y-y1);
getch();
closegraph();
}

15
Date:

Output:

16
Teacher’s Signature
Date:

Q9. Write a C program to implement Shearing of a Rectangle.


Source code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3,x4,y4,sh,x,y;
printf("\nEnter the 1st corner of the rectangle:");
scanf("%d%d",&x1,&y1);
printf("\nEnter the 2nd corner of the rectangle:");
scanf("%d%d",&x2,&y2);
printf("\nEnter the 3rd corner of the rectangle:");
scanf("%d%d",&x3,&y3);
printf("\nEnter the 4th corner of the rectangle:");
scanf("%d%d",&x4,&y4);
printf("\nEnter the shearing factor:");
scanf("%d",&sh);
initgraph(&gd,&gm,"");
x=getmaxx()/2;
y=getmaxy()/2;
line(0,y,2*x,y);
line(x,0,x,2*y);
line(x+x1,y-y1,x+x2,y-y2);
line(x+x2,y-y2,x+x3,y-y3);
line(x+x3,y-y3,x+x4,y-y4);
line(x+x4,y-y4,x+x1,y-y1);
x1=x1+sh*y1;
x2=x2+sh*y2;
x3=x3+sh*y3;
x4=x4+sh*y4;
line(x+x1,y-y1,x+x2,y-y2);
line(x+x2,y-y2,x+x3,y-y3);
line(x+x3,y-y3,x+x4,y-y4);
line(x+x4,y-y4,x+x1,y-y1);
getch();
closegraph();
}

17
Date:

Output:

18
Teacher’s Signature
Date:

Q10. Write a C program to implement Flood Fill Algorithm.


Source code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void flood(int,int,int,int);
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"NULL ");
rectangle(50,50,250,250);
flood(55,55,10,0);
getch();
}
void flood(int x,int y,int fillColor, int defaultColor)
{
if(getpixel(x,y)==defaultColor)
{
delay(1);
putpixel(x,y,fillColor);
flood(x+1,y,fillColor,defaultColor);
flood(x-1,y,fillColor,defaultColor);
flood(x,y+1,fillColor,defaultColor);
flood(x,y-1,fillColor,defaultColor);
}
}

Output:

19
Teacher’s Signature

You might also like