0% found this document useful (0 votes)
69 views26 pages

Sunil Kumar CG Lab

The document describes 7 labs completed for a computer graphics and multimedia course, where programs were written to draw various graphics primitives, lines, circles, and curves using different algorithms, and to demonstrate 2D and 3D transformations including translation, scaling, rotation, shearing, and reflection of graphics objects. Each lab section includes the problem statement, code written, and results obtained from running the program.

Uploaded by

Brijesh Nagar
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)
69 views26 pages

Sunil Kumar CG Lab

The document describes 7 labs completed for a computer graphics and multimedia course, where programs were written to draw various graphics primitives, lines, circles, and curves using different algorithms, and to demonstrate 2D and 3D transformations including translation, scaling, rotation, shearing, and reflection of graphics objects. Each lab section includes the problem statement, code written, and results obtained from running the program.

Uploaded by

Brijesh Nagar
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/ 26

Computer Graphics & Multimedia Lab

(ETCS 257)

Submitted To: Submitted By:


Ms. Medhavi Pandey Sunil Kumar
Assistant Professor Enroll No: 12018002720
CSE Department Roll No: C22
B.Tech CSE(3rd Sem)
INDEX
S. No. Name Of Experiment Remark
1 Write a program to draw computer
graphics primitives.
2 Write a program to draw a line using
DDA algorithm.
3 Write a program to draw a line using
Bresenham line drawing algorithm.
4 Write a program to draw a circle
using mid point algorithm.
5 Write a program to draw a circle
using bresenham circle algorithm.
6 Write a program showing 2-D
transformation.
7 Write a program showing 3-D
transformation.
8 Write a program to draw a bezier
curves.
LAB 1
Ques: Write a program to draw computer graphics primitives.

Code:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main()
{
    int gdriver= DETECT, gmode;
    initgraph(&gdriver,&gmode,"C:/TURBOC3/BGI");
    putpixel(200,200,BLUE);
    setcolor(WHITE);
    line(50,50,100,100);
    setcolor(RED);
    circle(200,200,100);  
    getch();
    closegraph();
}

RESULT:

.
LAB 2
Ques: Write a program to draw a line using DDA algorithm.

CODE:

#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<conio.h>

void main()
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"C:/TURBOC3/BGI");
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);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
step=dx;
else
step=dy;
dx=dx/step;
dy=dy/step;
x=x1;
y=y1;
i=1;
while(i<=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
}

RESULT:
LAB 3
Ques: Write a program to draw a line using Bresenham line drawing
algorithm.

CODE:
#include<stdio.h>
#include<graphics.h>
#include <conio.h>

void drawline(int x0,int y0,int x1,int y1)
{
int dx,dy,p,x,y;
clrscr();
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,BLUE);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,BLUE);
p=p+2*dy;
}
x=x+1;
}
}
void main()
{
int gdriver=DETECT,gmode,error,x0,y0,x1,y1;
initgraph(&gdriver,&gmode,"C:/TURBOC3/BGI");
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);
getch();
closegraph();
}

RESULT:
LAB 4
Ques: Write a program to draw a circle using mid point algorithm.

CODE:

#include<stdio.h>
#include<graphics.h>
 
void drawcircle(int x0, int y0, int radius)
{
    int x = radius;
    int y = 0;
    int err = 0;
 
    while (x >= y)
  {
    putpixel(x0 + x, y0 + y, 7);
    putpixel(x0 + y, y0 + x, 7);
    putpixel(x0 - y, y0 + x, 7);
    putpixel(x0 - x, y0 + y, 7);
    putpixel(x0 - x, y0 - y, 7);
    putpixel(x0 - y, y0 - x, 7);
    putpixel(x0 + y, y0 - x, 7);
    putpixel(x0 + x, y0 - y, 7);
 
    if (err <= 0)
  {    
        y += 1;
        err += 2*y + 1;                    
  }              
 
    if (err > 0)
  {
        x -= 1;
        err -= 2*x + 1;
  }
    delay(100);
  }
}

void main()
{
    int gdriver=DETECT, gmode, error, x, y, r;
    initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");

    printf("Enter radius of circle: ");


    scanf("%d", &r);

    printf("Enter co-ordinates of center(x and y): ");


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

    getch();
}

RESULT:
LAB 5
Ques: Write a program to draw a circle using bresenham circle
algorithm.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

void EightWaySymmetricPlot(int xc,int yc,int x,int y)


{
    putpixel(x+xc,y+yc,RED);
    putpixel(x+xc,-y+yc,YELLOW);
    putpixel(-x+xc,-y+yc,GREEN);
    putpixel(-x+xc,y+yc,YELLOW);
    putpixel(y+xc,x+yc,12);
    putpixel(y+xc,-x+yc,14);
    putpixel(-y+xc,-x+yc,15);
    putpixel(-y+xc,x+yc,6);
}

void BresenhamCircle(int xc,int yc,int r)


{
    int x=0,y=r,d=3-(2*r);
    EightWaySymmetricPlot(xc,yc,x,y);
    while(x<=y)
  {
        if(d<=0)
    {
            d=d+(4*x)+6;
    }
        else
    {
            d=d+(4*x)-(4*y)+10;
            y=y-1;
    }
        x=x+1;
        EightWaySymmetricPlot(xc,yc,x,y);
  }
}

int main(void)
{
    int xc,yc,r,gdriver = DETECT, gmode, errorcode;
    initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
    errorcode = graphresult();
    if (errorcode != grOk)
  {
        printf("Graphics error: %s\n", grapherrormsg(errorcode));
        printf("Press any key to halt:");
        getch();
        exit(1);
  }
    printf("Enter the values of xc and yc :");
    scanf("%d%d",&xc,&yc);
    printf("Enter the value of radius :");
    scanf("%d",&r);

    BresenhamCircle(xc,yc,r);
    getch();
    closegraph();
    return 0;
}

RESULT:
LAB 6
Ques: Write a program showing 2-D transformation.

CODE:

#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>

int x1,y1,x2,y2,midx,midy;
void axis();
void translation()
{
int tx,ty,xn1,yn1,xn2,yn2;
printf("\n Enter the translation:\n");
scanf("%d%d",&tx,&ty);
cleardevice();
outtextxy(400,100,"TRANSLATION");
xn1=x1+tx;
yn1=y1+ty;
xn2=x2+tx;
yn2=y2+ty;
axis();
rectangle(xn1,yn1,xn2,yn2);
getch();
}

void scaling()
{
float xn1,yn1,xn2,yn2;
float sx,sy;
printf("Enter the scaling factor");
scanf("%f%f",&sx,&sy);
cleardevice();
outtextxy(300,200,"SCALING");
xn1=x1*sx;
yn1=y1*sy;
xn2=x2*sx;
yn2=y2*sy;
axis();
rectangle(xn1,yn1,xn2,yn2);
getch();
}

void rotation()
{
int ang;
float rx,xn1,yn1,xn2,yn2,x1n1,y1n1,x2n2,y2n2;
printf("\n Enter the angle for rotation:\n");
scanf("%d",&ang);
cleardevice();
outtextxy(500,200,"ROTATION");
rx=(ang*3.14)/180;
xn1=x1*cos(rx)-y1*sin(rx);
yn1=y1*cos(rx)+x1*sin(rx);
xn2=x2*cos(rx)-y2*sin(rx);
yn2=y2*cos(rx)+x2*sin(rx);
x1n1=x2*cos(rx)-y1*sin(rx);
y1n1=y1*cos(rx)+x2*sin(rx);
x2n2=x1*cos(rx)-y2*sin(rx);
y2n2=y2*cos(rx)+x1*sin(rx);
axis();
line(xn1,yn1,x1n1,y1n1);
line(x1n1,y1n1,xn2,yn2);
line(xn2,yn2,x2n2,y2n2);
line(x2n2,y2n2,xn1,yn1);
getch();
}

void shearing()
{
float sh;
float xn1,yn1,xn2,yn2,x1n1,y1n1,x2n2,y2n2;
printf("\n Enter the value for shearing:\n");
scanf("%f",&sh);
cleardevice();
outtextxy(500,100,"SHEARING");
xn1=x1+sh*y1;
yn1=y1;
xn2=x2+sh*y2;
yn2=y2;
x1n1=x2+sh*y1;
y1n1=y1;
x2n2=x1+sh*y2;
y2n2=y2;
axis();
line(xn1,yn1,x1n1,y1n1);
line(x1n1,y1n1,xn2,yn2);
line(xn2,yn2,x2n2,y2n2);
line(x2n2,y2n2,xn1,yn1);
getch();
}

void reflection()
{
int xn1,yn1,xn2,yn2;
cleardevice();
outtextxy(300,100,"REFLECTION");
if((x1<y1)^(x1<y1))
{
xn1=x1+100;
xn2=x2+100;
yn1=y1;
yn2=y2;
}
else
{
xn1=x1;
xn2=x2;
yn1=y1+100;
yn2=y2+100;
}
axis();
rectangle(xn1,yn1,xn2,yn2);
getch();
}

void get()
{
printf("\n Enter the coordinates x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
outtextxy(200,100,"ORIGINAL OBJECT");
x1= getmaxx() / 2-x1;
y1= getmaxy() / 2-y1;
x2 = getmaxx() / 2+x2;
y2 = getmaxy() / 2+y2;
axis();
getch();
}

void axis()
{
midx=getmaxx() / 2;
midy=getmaxy() / 2;
line(0,midy,midx*2,midy);
line(midx,0,midx,midy*2);
rectangle(x1,y1,x2,y2);
}

void main()
{
int ch,gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
get();
do
{
cleardevice();
outtextxy(10,10,"1)TRANSLATION");
outtextxy(10,20,"2)SCALING");
outtextxy(10,30,"3)ROTATION");
outtextxy(10,40,"4)SHEARING");
outtextxy(10,50,"5)REFLECTION");
outtextxy(10,60,"6)EXIT");
outtextxy(10,70,"ENTER UR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
translation();
break;
case 2:
scaling();
break;
case 3:
rotation();
break;
case 4:
shearing();
break;
case 5:
reflection();
break;
case 6:
exit(0);
}
}while(ch<6);
}

RESULTS:
LAB 7
Ques: Write a program showing 3-D transformation.

Code:

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void trans();
void axis();
void scale();
void rotate();
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int ch;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:/TURBOC3/BGI");
printf("\n 1. Translation \n2.Scaling\n 3.Rotation\n 4.Exit");
printf("Enter your choice");
scanf("%d",&ch);
do
{
switch(ch)
{
case 1: trans();
getch();
closegraph();
break;
case 2: scale();
getch();
closegraph();
break;
case 3: rotate();
getch();
closegraph();
break;
case 4: break;
}
printf("Enter your choice");
scanf("%d",&ch);
} while(ch<4);
}
void trans()
{
int x,y,z,o,x1,x2,y1,y2;
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,10,1);
printf("Enter translation factor");
scanf("%d%d",&x,&y);
printf("After translation:");
bar3d(midx+x+50,midy-(y+100),midx+x+60,midy-(y+90),10,1);
}
void scale()
{
int x,y,z,o,x1,x2,y1,y2;
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("before translation\n");
printf("Enter scaling factors\n");
scanf("%d%d%d",&x,&y,&z);
printf("After scaling\n");
bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1);
}
void rotate()
{
int x,y,z,o,x1,x2,y1,y2;
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter rotating angle");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*sin(o*3.14/180)+100*cos(o*3.14/180);
x2=60*cos(o*3.14/180)-90*sin(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
axis();
printf("After rotation about z axis");
bar3d(midx+1,midy-y1,midx+x2,midy-y2,5,1);
axis();
printf("After rotation about x axis");
bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);
axis();
printf("After rotation about y axis");
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
}

RESULT:
LAB 8
Ques: Write a program to draw a bezier curves.

Code:

#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>
void bezier(int x[4],int y[4])
{
int gd=DETECT,gm;
int i;
double t;
initgraph(&gd,&gm,"C:/TURBOC3/BGI");
for(t=0.0; t<1.0; t+=0.0005)      // t values lies between 0 to
1.
{
// formula to draw curve.
double xt=pow(1-t,3)*x[0]+3*t*pow(1-t,2)*x[1]+3*pow(t,2)*(1-
t)*x[2]+pow(t,3)*x[3];
double yt=pow(1-t,3)*y[0]+3*t*pow(1-t,2)*y[1]+3*pow(t,2)*(1-
t)*y[2]+pow(t,3)*y[3];
putpixel(xt,yt,WHITE);        
}
for(i=0;i<4;i++)
putpixel(x[i],y[i],YELLOW);// control points n=3
getch();
closegraph();
return;
}
void main()
{
int x[4],y[4];
int i;
printf("Enter the x- and y-coordinates of four control points.\
n");
for(i=0;i<4;i++)
scanf("%d%d",&x[i],&y[i]);
bezier(x,y);
}

RESULT:

You might also like