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

Computer Graphics Practical

Uploaded by

aviraj112001
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)
22 views19 pages

Computer Graphics Practical

Uploaded by

aviraj112001
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

Computer Graphics Program

Program 1:

To write a C program to draw a line using DDA Algorithm.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<graphics.h>

void main()

int i,steps,x1,x2,y1,y2;

float x,y,xinc,yinc,dx,dy;

char msg[86];

int gdriver = DETECT,gmode,errorcode;

clrscr();

initgraph(&gdriver,&gmode,"f:\\tc");

printf("\n Enter the co ordinates ");

scanf("%d%d%d%d",&x1,&x2,&y1,&y2);

cleardevice();

outtextxy(200,4,"Draw Using DDA");

line(x1,x2,y1,y2);

dx = x2 - x1;

dy = y2 - y1;

if(abs(dx) > abs(dy))

steps = abs(dx);

else

steps = abs(dy);

xinc = (float)dx/steps ;
yinc = (float)dy/steps ;

y = y1;

x = x1;

putpixel(ceil(x),ceil(y),20);

for(i = 0;i <= steps ;i++)

x += xinc ;

y += yinc ;

putpixel(x,y,2);

delay(45);

getch();

Program 2:

To write a C program to draw a line using Bresenham’s Algorithm.


Program 3 :
To write a C program to draw a circle using Bresenham’s Algorithm.
Program 4 :

To write a C program to draw the various attributes of line, circle and


ellipse.
Program 4 :

To write a C program to draw the various attributes of line, circle and


ellipse.
Program 5 :

To write a C program to perform 2D Translation transformation

#include<graphics.h>

#include<stdlib.h>

#include<stdio.h>

#include<math.h>

void main()

int graphdriver=DETECT,graphmode,errorcode;

int i;

int x2,y2,x1,y1,x,y;

printf("Enter the 2 line end points:");

printf("x1,y1,x2,y2");

scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");

line(x1,y1,x2,y2);

printf("Enter translation co-ordinates ");

printf("x,y");

scanf("%d%d",&x,&y);

x1=x1+x;

y1=y1+y;

x2=x2+x;

y2=y2+y;

printf("Line after translation");

line(x1,y1,x2,y2);

getch();

closegraph();

Program 6 :

To write a C program to perform 2D Rotation transformation

#include<graphics.h>

#include<stdlib.h>

#include<stdio.h>

#include<math.h>

void main()

int graphdriver=DETECT,graphmode,errorcode;

int i;

int x2,y2,x1,y1,x,y,xn,yn;

double r11,r12,r21,r22,th;
clrscr();

printf("Enter the 2 line end points:");

printf("x1,y1,x2,y2");

scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");

line(x1,y1,x2,y2);

printf("\n\n\n[ Enter the angle");

scanf("%lf",&th);

r11=cos((th*3.1428)/180);

r12=sin((th*3.1428)/180);

r21=(-sin((th*3.1428)/180));

r22=cos((th*3.1428)/180);

//printf("%lf %lf %lf %lf",r11,r12,r21,r22);

xn=((x2*r11)-(y2*r12));

yn=((x2*r12)+(y2*r11));

line(x1,y1,xn,yn);

getch();

closegraph();

Program 6 :

To write a C program to perform 2D Scaling transformation

#include<graphics.h>

#include<stdlib.h>

#include<stdio.h>

#include<math.h>

void main()
{

int graphdriver=DETECT,graphmode,errorcode;

int i;

int x2,y2,x1,y1,x,y;

printf("Enter the 2 line end points:");

printf("x1,y1,x2,y2");

scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");

line(x1,y1,x2,y2);

printf("Enter scaling co-ordinates ");

printf("x,y");

scanf("%d%d",&x,&y);

x1=(x1*x);

y1=(y1*y);

x2=(x2*x);

y2=(y2*y);

printf("Line after scaling");

line(x1,y1,x2,y2);

getch();

closegraph();

Program 7 :

To write a C program Midpoint Ellipse Drawing Algorithm.

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<graphics.h>
void plot(int xc,int yc,int x,int y)

putpixel(x+xc,y+yc,1);

putpixel(x+xc,-y+yc,1);

putpixel(-x+xc,y+yc,1);

putpixel(-x+xc,-y+yc,1);

void myellipse(int xc,int yc,int rx,int ry)

long rx2,ry2,px,py,tworx2,twory2,p;

float x,y;

x=0;

y=ry;

rx2=rx*rx;

ry2=ry*ry;

tworx2=2*rx2;

twory2=2*ry2;

px=0;

py=tworx2*y;

//Printing the initial point

plot(xc,yc,x,y);

p=(int)(ry2-(rx2*ry)+(0.25*rx2));

while(px<py)

x++;
px+=twory2;

if(p<0)

p+=px+ry2;

else

y=y-1;

py-=tworx2;

p+=ry2+px-py;

plot(xc,yc,x,y);

p=(int)((ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2));

while(y>0)

y--;

py-=tworx2;

if(p>0)

p+=rx2-py;

else

x++;

px+=twory2;

p+=rx2-py+px;

plot(xc,yc,x,y);

}
void main()

int gd,gm;

int xc,yc,rx,ry;

clrscr();

gd=DETECT;

detectgraph(&gd,&gm);

printf("Enter values of centre of ellipse\n");

scanf("%d%d",&xc,&yc);

printf("Enter the x radius and y radius\n");

scanf("%d%d",&rx,&ry);

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

myellipse(xc,yc,rx,ry);

getch();

You might also like