0% found this document useful (0 votes)
44 views

Computer Graphics

The document contains code snippets for drawing various 2D shapes and performing 2D transformations using different algorithms in C graphics programming. It includes programs to draw circles using midpoint, trigonometric, Bresenham and polynomial algorithms. Similarly, it shows drawing lines using DDA, Bresenham and slope-intercept algorithms. Programs for drawing ellipses using trigonometric and polynomial methods are also included. Lastly, it demonstrates 2D transformations like translation, rotation and scaling of geometric objects.

Uploaded by

Dharm Mahal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Computer Graphics

The document contains code snippets for drawing various 2D shapes and performing 2D transformations using different algorithms in C graphics programming. It includes programs to draw circles using midpoint, trigonometric, Bresenham and polynomial algorithms. Similarly, it shows drawing lines using DDA, Bresenham and slope-intercept algorithms. Programs for drawing ellipses using trigonometric and polynomial methods are also included. Lastly, it demonstrates 2D transformations like translation, rotation and scaling of geometric objects.

Uploaded by

Dharm Mahal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

/*WAP to draw a circle*/

#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
circle(250,250,30);
getch();
closegraph();
}
/*WAP to draw a circle using by using mid point algorithm*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void pixel(int xc,int yc,int x,int y);
void main()
{
int gd=DETECT,gm,xcs,ycs,rd,x,y,k;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi ");
printf("Enter the value of Xc\t");
scanf("%d",&xcs);
printf("Enter the value of Yc \t");
scanf("%d",&ycs);
printf("Enter the Radius of circle\t");
scanf("%d",&rd);
x=0;
y=rd;
k=1-r;
pixel(xcs,ycs,x,y);
while(x<y)
{
if(k<0)
{
x=x+1;
k=k+(2*x)+1;
}
else
{
x=x+1;
y=y-1;
k=k+(2*x)-(2*y)+1;
}
pixel(xcs,ycs,x,y);
}
getch();
closegraph();
}
void pixel(int xcs,int ycs,int x,int y)
{
putpixel(xcs+x,ycs+y,7);
putpixel(xcs+y,ycs+x,7);
putpixel(xcs-y,ycs+x,7);
putpixel(xcs-x,ycs+y,7);
putpixel(xcs-x,ycs-y,7);
putpixel(xcs-y,ycs-x,7);
putpixel(xcs+y,ycs-x,7);
putpixel(xcs+x,ycs-y,7);
}
/WAP to draw a circle using trignometric method*/

#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://tc//bgi");
int rd,xcs,ycs,x,y,the,the_end;
the=0;
the_end=45;
printf("enter the radius for circle");
scanf("%d",&rd);
printf("Enter the center coordinates");
scanf("%d%d",&xcs,&ycs);
while(the<=the_end)
{
x=rd*cos(the);
y=rd*sin(the);
putpixel(xcs+x,ycs+y,1);
putpixel(xcs+x,ycs-y,2);
putpixel(xcs-x,ycs+y,3);
putpixel(xcs-x,ycs-y,4);
putpixel(xcs+y,ycs+x,1);
putpixel(xcs+y,ycs-x,2);
putpixel(xcs-y,ycs+x,3);
putpixel(xcs-y,ycs-x,4);
the=the+1;
}
getch();
}
/WAP to draw a circle using bresenham algorithm*/

# include<stdio.h>
# include<conio.h>
# include<graphics.h>
void main()
{
int gd=DETECT,gm,xcs,ycs,rd,x,y,k;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");

printf("enter the value of Xcs\t");


scanf("%d",&xcs);
printf("enter the value of ycs\t");
scanf("%d",&ycs);
printf("enter the radius\t");
scanf("%d",&rd);
x=0;
y=rd;
putpixel(xcs+x,ycs-y,1);
Pk=3-(2*rd);
for(x=0;x<=y;x++)
{
if (k<0)
{
y=y;
k=(k+(4*x)+6);
}
else
{
y=y-1;
k=k+((4*(x-y)+10));
}
putpixel(xcs+x,ycs-y,WHITE);
putpixel(xcs-x,ycs-y,WHITE);
putpixel(xcs+x,ycs+y,WHITE);
putpixel(xcs-x,ycs+y,WHITE);
putpixel(xcs+y,ycs-x,WHITE);
putpixel(xcs-y,ycs-x,WHITE);
putpixel(xcs+y,ycs+x,WHITE);
putpixel(xcs-y,ycs+x,WHITE);
}
getch();

}
/WAP to draw a circle using polynomial method*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int main()
{
int gm,gd=DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
int h, k, rd, xc, yc, xe;
printf("Enter co-ordinate of center and radius ");
scanf("%d%d%d",&h,&k,&rd);
xc= 0;
xe = (int)rd/sqrt(2)+0.5;
yc = rd;
while(xc <= xe){
putpixel(xc+h,yc+k,WHITE);
putpixel(xc+h,-yc+k,WHITE);
putpixel(yc+h,-xc+k,WHITE);
putpixel(-yc+h,-xc+k,WHITE);
putpixel(-xc+h,-yc+k,WHITE);
putpixel(-xc+h,yc+k,WHITE);
putpixel(-yc+h,xc+k,WHITE);
putpixel(yc+h,xc+k,WHITE);
xc++;
yc= (int)sqrt(rd*rd-xc*xc)+0.5;
}
getch();
closegraph();
return 0;
}
/WAP to draw a line using DDA*/

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x,y,x1,x2,y1,y2,dx,dy,pixel;
int i,gm,gd=DETECT;
printf("enter the value of x1");
scanf("%f",&x1);
printf("enter the value of y1");
scanf("%f",&y1);
printf("enter the value of x2");
scanf("%f",&x2);
printf("enter the value of y2");
scanf("%f",&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
pixel=dx;
else
pixel=dy;
dx=dx/pixel;
dy=dy/pixel;
x=x1;
y=y1;
i=1;
while(i<=pixel)
{
putpixel(x,y,1);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
}
/*WAP to draw a line using bresenham algorithm*/

# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;
clrscr();
printf("\nEnter the co-ordinates of x1 and y1");
scanf("%d %d",&x1,&y1);
printf("\n\n\tEnter the co-ordinates of x2 and y2");
scanf("%d %d",&x2,&y2);
dx=(x2-x1);
dy=(y2-y1);
p=2*(dy)-(dx);
x=x1;
y=y1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
putpixel(x,y,WHITE);
while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}
/* WAP to draw a line by using slope intercept method*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
float m,x1,y1,x2,y2;
int xc,yc;
int gd=DETECT,gm;
clrscr();
printf("enter the value of x1 and y1 ");
scanf("%f%f",&x1,&y1);
printf("enter the value of x2 and y2 ");
scanf("%f%f",&x2,&y2);
initgraph(&gd,&gm,"c:\\tc\\bgi");
m=(y2-y1)/(x2-x1);
for(xc=1;xc<=xc2;xc++)
{
yc=m*(xc-xc1)+yc1;
putpixel(xc,yc,WHITE);
}
getch();
closegraph();
}
/*WAP to drwa an ellipse using trignometric method*/

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void putpixel(int,int,int,int);
void main()
{
int xc,yc,x1,y1,a,b,h,k,theta;
float p=3.14159/180;
clrscr();
printf("enter the x and y coordinates ");
scanf("%d%d",&h,&k);
printf(""enter the major and minor axis");
scanf("%d%d",&a,&b);
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
for(theta=0;theta<=90;theta++)
{
x1=a*cos(theta*p);
y1=b*sin(theta*p);
xc=int(x1+0.5);
yc=int(y1+0.5);
putpixel(xc,yc,h,k);
}
getch();
closegraph();
}
void putpixel(int xc,int yc,int h,int k)
{
putpixel(xc+h,yc+k,WHITE);
putpixel(xc+h,-yc+k,WHITE);
putpixel(-xc+h,yc+k,WHITE);
putpixel(-xc+h,-yc+k,WHITE);
}
/*WAP to draw an ellipse using polynomial method*/

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void plot(int,int,int,int);
void main()
{
int xc,yc,rd,i,h,k,a,b;
printf("enter the x and y coordinates: ");
scanf("%d%d",&h,&k);
printf("enter the major and minor axis ");
scanf("%d%d",&a,&b);
xc=0;
yc=b;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
while(xc<a)
{
plot(xc,yc,h,k);
xc++;
yc=b*sqrt(((a*a)-(xc*xc*1.0))/(a*a));
}
getch();
}
void plot(int xc,int yc,int h,int k)
{
putpixel(xc+h,yc+k,WHITE);
putpixel(xc+h,-yc+k,WHITE);
putpixel(-xc+h,yc+k,WHITE);
putpixel(-xc+h,-yc+k,WHITE);
}
W.A.P. for 2D translation

#include<graphics.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,ec,i;
int x2,y2,x1,y1,tx,ty;
printf("Enter the coordinates points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&gd,&gm,"c:\\tc\\bin");
rectangle(x1,y1,x2,y2);
printf("Enter translation co-ordinates tx and ty ");
scanf("%d%d",&tx,&ty);
x1=x1+tx;
y1=y1+ty;
x2=x2+tx;
y2=y2+ty;
printf("rectangle after translation");
rectangle(x1+tx,y1+ty,x2+tx,y2+ty);
getch();
closegraph();
}
W.A.P. to rotate a point about a point

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
clrscr();
int gm,gd=DETECT;
initgraph(&gd,&gm,"c:\\tc\\bin");
int h,k,x1,y1,x2,y2,x3,y3;
float t;
printf("enter the coordinates of point \n");
scanf("%d%d",&x2,&y2);
putpixel(x2,y2,2);
printf("enter the coordinates of point around which rotation done");
scanf("%d%d",&h,&k);
putpixel(h,k,2);
printf("enter the angle for rotation");
scanf("%d",&t);
cleardevice();
x1=(h*cos(t))-(k*sin(t));
y1=(h*sin(t))+(k*cos(t));
x3=x1+x2-h;
y3=y1+y2-k;
printf("points after rotation is:");
putpixel(x3,y3,2);
getch();
closegraph();
}
W.A.P. to rotate a point about origin

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
clrscr();
int gm,gd=DETECT;
initgraph(&gd,&gm,"");
int h,k,x1,y1,x2,y2,x3,y3;
float t;
printf("enter the coordinates of point");
scanf("%d%d",&x2,&y2);
putpixel(x2,y2,2);
printf("enter the angle for rotation");
scanf("%d",&t);
cleardevice();
x1=int(x2*cos(t*3.14/180))-(y2*sin(t*3.14/180));
y1=int(x2*sin(t*3.14/180))+(y2*cos(t*3.14/180));
printf("points after rotation is:");
putpixel(x1,y1,2);
getch();
closegraph();
}
W.A.P. for 2D Scaling

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x2,y2,x1,y1,x,y,i,ec;
printf("Enter the points x1,y1,x2,y2 :");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&gd,&gm,"c:\\tc\\bgi");
rectangle(x1,y1,x2,y2);
printf("Enter scaling co-ordinates values x and y ");
scanf("%d%d",&x,&y);
x1=(x1*x);
y1=(y1*y);
x2=(x2*x);
y2=(y2*y);
printf("rectangle after scaling");
rectangle(x1,y1,x2,y2);
getch();
closegraph();
}

You might also like