0% found this document useful (0 votes)
74 views30 pages

Assignment No: 01 Assignment Name: DDA Line Drawing: Date

The document contains 6 assignments related to computer graphics and algorithms in C language. Assignment 1 demonstrates DDA line drawing, Assignment 2 uses Bresenham's algorithm for line drawing, Assignment 3 implements Bresenham's circle drawing algorithm, Assignment 4 applies midpoint circle drawing, Assignment 5 reflects a triangle about the X-axis, and Assignment 6 reflects a triangle about the Y-axis. Each assignment contains the C code to implement the given algorithm along with sample input/output.

Uploaded by

Erik Winkel
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)
74 views30 pages

Assignment No: 01 Assignment Name: DDA Line Drawing: Date

The document contains 6 assignments related to computer graphics and algorithms in C language. Assignment 1 demonstrates DDA line drawing, Assignment 2 uses Bresenham's algorithm for line drawing, Assignment 3 implements Bresenham's circle drawing algorithm, Assignment 4 applies midpoint circle drawing, Assignment 5 reflects a triangle about the X-axis, and Assignment 6 reflects a triangle about the Y-axis. Each assignment contains the C code to implement the given algorithm along with sample input/output.

Uploaded by

Erik Winkel
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/ 30

Date:

Assignment no: 01
Assignment name: DDA Line Drawing

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
float xs,ys,xe,ye,dx,dy,span,x,y,i;
clrscr();
initgraph(&gd,&gm,"");
printf("\nEnter the value of (xs,ys):");
scanf("%f%f",&xs,&ys);
printf("\nEnter the value of (xe,ye):");
scanf("%f%f",&xe,&ye);
x=getmaxx()/2;
y=getmaxy()/2;
setbkcolor(WHITE);
setcolor(GREEN);
line(0,y,2*x,y);
line(x,0,x,2*y);
if(fabs(xe-xs)>fabs(ye-ys))
span=fabs(xe-xs);
else
span=fabs(ye-ys);
dx=(xe-xs)/span;
dy=(ye-ys)/span;
for(i=0;i<=span;i++)
{
putpixel(x+xs,y-ys,4);
xs+=dx;
ys+=dy;
}
getch();

Teacher’s Signature
1
Date:

Output:
Enter the value of (xs,ys): 10 10
Enter the value of (xe,ye): 40 40

Teacher’s Signature
2
Date:

Assignment no: 02
Assignment name: Bresenham Line Drawing

#include<stdio.h>

#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int xs,ys,xe,ye,s1,s2,i,x,y,dx,dy,swap,temp,p;
int gd=DETECT,gm;
clrscr();
printf("\nEnter the value of (xs,ys):");
scanf("%d%d",&xs,&ys);
printf("\nEnter the value of (xe,ye):");
scanf("%d%d",&xe,&ye);
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);
dx=abs(xe-xs);
dy=abs(ye-ys);
if((xe-xs)>=0)
s1=1;
else
s1=-1;
if((ye-ys)>=0)
s2=1;

Teacher’s Signature
3
Date:

else
s2=-1;
if(dy>dx)
{
temp=dx;
dx=dy;
dy=temp;
swap=1;
}
else
swap=0;
p=2*dy-dx;
//putpixel(x+xs,y-ys,3);
for(i=0;i<=dx;i++)
{
if(p>=0)
{
xs+=s1;
ys+=s2;
p+=2*(dy-dx);
}
else
{
if(swap==1)
ys+=s2;
else
xs+=s1;
p+=2*dy;
}
putpixel(x+xs,y-ys,3);
}
getch();
}

Teacher’s Signature
4
Date:

Output:
Enter the value of (xs,ys): 10 10
Enter the value of (xe,ye): 40 40

Teacher’s Signature
5
Date:

Assignment no: 03
Assignment name: Bresenham Circle Drawing

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int xc,yc,r,x,y,d,del,big;
int gd=DETECT,gm;
clrscr();
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=2*(1-r);
while(r>big)
{
\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);

Teacher’s Signature
6
Date:

putpixel((xc+r),(yc-big),5);
putpixel((xc-r),(yc+big),5);
putpixel((xc-r),(yc-big),5);
if(d<0)
{
del=(2*d)+(2*r)-1;
if(del<=0)
{
big+=1;
d+=(2*big)+1;
}
else
{
big+=1;
r-=1;
d+=(2*big)-(2*r)+2;
}
}
else if(d>0)
{;’
del=(2*big)-(2*d)+1;
if(del<0)
{
r-=1;
d-=(2*r)+1;
}
else
{
big+=1;
r-=1;
d+=(2*big)-(2*r)+2;
}
}
else
{
big+=1;
r-=1;

Teacher’s Signature
7
Date:

d+=(2*big)-(2*r)+2;
}
}
getch();
}
Output:
Enter the center of the circle (xc,yc):0 0
Enter the radius of the circle:100

Teacher’s Signature
8
Date:

Assignment no: 04
Assignment name: Midpoint Circle Drawing

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int xc,yc,x,y,d,r,del,big;
int gd=DETECT,gm;
clrscr();
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)

Teacher’s Signature
9
Date:

{
big+=1;
if(d<0)
d+=(2*big)+1;
else
{
r-=1;
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:
Enter the center of the circle (xc,yc):40 40
Enter the radius of the circle:100

Teacher’s Signature
10
Date:

Assignment no: 05
Assignment name: Reflection of a triangle about X-axis

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x1,y1,x2,y2,x3,y3,x,y;
int gd=DETECT,gm;
clrscr();
printf("\nEnter the 1st co-ordinate of the triangle:");
scanf("%d%d",&x1,&y1);
printf("\nEnter the 2nd co-ordinate of the triangle:");
scanf("%d%d",&x2,&y2);
printf("\nEnter the 3rd co-ordinate of the triangle:");
scanf("%d%d",&x3,&y3);
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);
setcolor(RED);
line(x+x1,y-y1,x+x2,y-y2);
line(x+x2,y-y2,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-y2,x+x3,y-y3);
line(x+x3,y-y3,x+x1,y-y1);
getch();
closegraph();

Teacher’s Signature
11
Date:

Output:
Enter the first co-ordinate of the triangle: 100 100
Enter the second co-ordinate of the triangle: 50 150
Enter the third co-ordinate of the triangle: 150 150

Teacher’s Signature
12
Date:

Assignment no: 06
Assignment name: Reflection of a triangle about Y-axis

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x1,y1,x2,y2,x3,y3,x,y;
int gd=DETECT,gm;
clrscr();
printf("\nEnter the 1st co-ordinate of the triangle:");
scanf("%d%d",&x1,&y1);
printf("\nEnter the 2nd co-ordinate of the triangle:");
scanf("%d%d",&x2,&y2);
printf("\nEnter the 3rd co-ordinate of the triangle:");
scanf("%d%d",&x3,&y3);
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);
setcolor(RED);
line(x+x1,y-y1,x+x2,y-y2);
line(x+x2,y-y2,x+x3,y-y3);
line(x+x3,y-y3,x+x1,y-y1);
x1*=-1;
x2*=-1;
x3*=-1;
setcolor(BLUE);
line(x+x1,y-y1,x+x2,y-y2);
line(x+x2,y-y2,x+x3,y-y3);
line(x+x3,y-y3,x+x1,y-y1);
getch();
closegraph();

Teacher’s Signature
13
Date:

Output:
Enter the first co-ordinate of the triangle: 100 100
Enter the second co-ordinate of the triangle: 50 150
Enter the third co-ordinate of the triangle: 150 150

Teacher’s Signature
14
Date:

Assignment no: 07
Assignment name: Reflection of a triangle about Y=X axis

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x1,y1,x2,y2,x3,y3,x,y;
int gd=DETECT,gm;
clrscr();
printf("\nEnter the 1st co-ordinate of the triangle:");
scanf("%d%d",&x1,&y1);
printf("\nEnter the 2nd co-ordinate of the triangle:");
scanf("%d%d",&x2,&y2);
printf("\nEnter the 3rd co-ordinate of the triangle:");
scanf("%d%d",&x3,&y3);
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);
line(x,y,2*x,0);
setcolor(RED);
line(x+x1,y-y1,x+x2,y-y2);
line(x+x2,y-y2,x+x3,y-y3);
line(x+x3,y-y3,x+x1,y-y1);
setcolor(BLUE);
line(x+y1,y-x1,x+y2,y-x2);
line(x+y2,y-x2,x+y3,y-x3);
line(x+y3,y-x3,x+y1,y-x1);
getch();
closegraph();
}

Teacher’s Signature
15
Date:

Output:
Enter the first co-ordinate of the triangle: 9 18
Enter the second co-ordinate of the triangle: 4 12
Enter the third co-ordinate of the triangle: 9 12

Teacher’s Signature
16
Date:

Assignment no: 08
Assignment name: Scaling of a rectangle

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int xl,yl,xh,yh,sx,sy,x,y;
printf("\nEnter lower left corner of the rectangle:");
scanf("%d%d",&xl,&yl);
printf("\nEnter upper right corner of the rectangle:");
scanf("%d%d",&xh,&yh);
printf("\nEnter the scaling factor(sx,sy):");
scanf("%d%d",&sx,&sy);
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);
setcolor(RED);
line(x+xl,y-yl,x+xh,y-yl);
line(x+xh,y-yl,x+xh,y-yh);
line(x+xh,y-yh,x+xl,y-yh);
line(x+xl,y-yh,x+xl,y-yl);

xh*=sx;
yh*=sy;

setcolor(BLUE);

line(x+xl,y-yl,x+xh,y-yl);
line(x+xh,y-yl,x+xh,y-yh);

Teacher’s Signature
17
Date:

line(x+xh,y-yh,x+xl,y-yh);
line(x+xl,y-yh,x+xl,y-yl);

getch();
closegraph();
}

Output:
Enter the lower left corner of the rectangle:10 10
Enter the upper left corner of the rectangle:20 20
Enter the scaling factor(sx,sy):2 2

Teacher’s Signature
18
Date:

Assignment no: 09
Assignment name: Translation of a line

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int xs,ys,xe,ye,tx,ty,x,y;
int gd=DETECT,gm;
clrscr();
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();
}

Teacher’s Signature
19
Date:

Output:
Enter the initial line position(xs,ys):10 10
Enter the final line position(xe,ye):20 20
Enter the translation vector(tx,ty):20 20

Teacher’s Signature
20
Date:

Assignment no: 10
Assignment name: Rotation of a line about origin

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

Teacher’s Signature
21
Date:

Output:
ENTER THE FINAL CO-ORDINATES: 10 10
ENTER THE ANGLE FOR ROTATION: 50

Teacher’s Signature
22
Date:

Assignment no: 11
Assignment name: Rotation about arbitrary point of a line

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int xs,ys,xe,ye,xn,yn,x,y,a;
int gd=DETECT,gm;
clrscr();
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,"");
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);
line(x+xs,y-ys,x+xn,y-yn);
getch();
closegraph();
}

Teacher’s Signature
23
Date:

Output:
Enter the initial line position(xs,ys):10 10
Enter the final line position(xe,ye):50 50
Enter the rotation angle:50

Teacher’s Signature
24
Date:

Teacher’s Signature
25
Date:

Assignment no: 12
Assignment name: Boundary fill
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void bound(int,int,int,int);
void main()
{
int gd=DETECT,gm,x,y,fill,boun;
initgraph(&gd,&gm," ");
printf("Enter the boundary color and fill color:");
scanf("%d%d",&boun,&fill);
setbkcolor(WHITE);
setcolor(boun);
x=getmaxx()/2;
y=getmaxy()/2;
rectangle(100,300,150,270);
bound(130,280,fill,boun);
getch();
//closegraph();
}
void bound(int x,int y,int fill,int boun)
{
if((getpixel(x,y)!=fill) && (getpixel(x,y)!=boun))
{
putpixel(x,y,fill);
bound(x+1,y,fill,boun);
bound(x-1,y,fill,boun);
bound(x,y+1,fill,boun);
bound(x,y-1,fill,boun);
bound(x+1,y+1,fill,boun);
bound(x+1,y-1,fill,boun);
bound(x-1,y+1,fill,boun);
bound(x-1,y-1,fill,boun);
}
}

Teacher’s Signature
26
Date:

Output:
Enter the boundary color and fill color:2 4

Teacher’s Signature
27
Date:

Assignment no: 13
Assignment name: Ellipse Drawing

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void 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,"");
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)
{

Teacher’s Signature
28
Date:

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

Teacher’s Signature
29
Date:

Output:
Enter the semi major axis(a,b):50 20
Enter the center(xc,yc):0 0

Teacher’s Signature
30

You might also like