0% found this document useful (0 votes)
26 views21 pages

Q1. Write A C Program To Implement DDA Line Drawing Algorithm

The document contains 10 questions about computer graphics algorithms. Each question provides source code for implementing an algorithm such as DDA line drawing, Bresenham's line algorithm, circle drawing, ellipse drawing, scaling, translation, rotation, reflection, shearing, and flood fill. The source code is in C language.

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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views21 pages

Q1. Write A C Program To Implement DDA Line Drawing Algorithm

The document contains 10 questions about computer graphics algorithms. Each question provides source code for implementing an algorithm such as DDA line drawing, Bresenham's line algorithm, circle drawing, ellipse drawing, scaling, translation, rotation, reflection, shearing, and flood fill. The source code is in C language.

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 DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Q1. Write a C program to implement DDA Line Drawing Algorithm.

Source code:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
int main(){
int gd=DETECT,gm,x1,y1,x2,y2,dx,dy,xinc,yinc,step,i=0;
initgraph(&gd,&gm,”NULL”);
printf("Enter the first co-ordinates - ");
scanf("%d%d",&x1,&y1);
printf("Enter the second co-ordinates - ");
scanf("%d%d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy)){
step=abs(dx);
}
else{
step=abs(dy);
}
xinc=dx/step;
yinc=dy/step;
do{
x1=x1+xinc;
y1=y1+yinc;
putpixel(x1,y1,RED);
i++;
}

1
while(i<step);
getch();
}

Output:

TEACHER’S SIGNATURE

2
Q2. Write a C program to implement Bresenham’s Line Drawing
Algorithm.
Source code :
#include<stdio.h>
#include<graphics.h>
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;}
x=x+1;
}
getch();
}

3
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "NULL");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
return 0;
getch();
}

Output:

TEACHER’S SIGNATURE

4
Q3. Write a C program to implement Mid- Point Circle Drawing
Algorithm.
Source code :
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
int x,y,x_mid,y_mid,radius,dp;
int g_mode,g_driver=DETECT;
initgraph(&g_driver,&g_mode,”NULL”);
printf("*********** MID POINT Circle drawing algorithm ********\n\n");
printf("\nenter the coordinates= ");
scanf("%d %d",&x_mid,&y_mid);
printf("\n now enter the radius =");
scanf("%d",&radius);
x=0;
y=radius;
dp=1-radius;
do
{
putpixel(x_mid+x,y_mid+y,YELLOW);
putpixel(x_mid+y,y_mid+x,YELLOW);
putpixel(x_mid-y,y_mid+x,YELLOW);
putpixel(x_mid-x,y_mid+y,YELLOW);
putpixel(x_mid-x,y_mid-y,YELLOW);
putpixel(x_mid-y,y_mid-x,YELLOW);
putpixel(x_mid+y,y_mid-x,YELLOW);
putpixel(x_mid+x,y_mid-y,YELLOW);

5
if(dp<0) {
dp+=(2*x)+1;
}
else{
y=y-1;
dp+=(2*x)-(2*y)+1;
}
x=x+1;
}while(y>x);
getch();
}

Output:

TEACHER’S SIGNATURE

6
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);

7
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
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;
}

8
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:

9
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);

10
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();

closegraph();
}

Output:

TEACHER’S SIGNATURE

11
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,"NULL");
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;

12
ye+=ty;
setcolor(5);
line(x+xs,y-ys,x+xe,y-ye);
getch();
closegraph();
}

Output:

TEACHER’S SIGNATURE

13
Q7. Write a C program to implement Rotation of a Line(Arbitrary
Point).
Source code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int main()
{
int xs,ys,xe,ye,xn,yn,x,y,a;
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 rotation angle:");
scanf("%d",&a);
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+xs,y-ys,x+xe,y-ye);
xn=(xe*cos(a))-(ye*sin(a));
yn=(xe*sin(a))+(ye*cos(a));
setcolor(5);

14
line(x+xs,y-ys,x+xn,y-yn);
getch();
closegraph();
}

Output:

TEACHER’S SIGNATURE

15
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;

16
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();
}

Output:

17
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,"NULL");
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;

18
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();
}

Output:

19
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);

20
Output:

TEACHER’S SIGNATURE

21

You might also like