CGMA LAB FILE
CGMA LAB FILE
Program :
#include <graphics.h>
#include <conio.h>
main(){
//init graphics
/*
*/
//draw a line
/*
x1: 100
y1: 100
x2: 200
y2: 100
*/
getch();
closegraph();
return 0;
}
Q2. Write the program to draw a line using DDA Algorithm.
Program :-
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>
void main( )
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
scanf("%f%f",&x1,&y1);
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);
closegraph();
}
Q3. Write the program to draw a line using Bresenham’s Algorithm.
Program :-
#include<stdio.h>
#include<graphics.h>
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;
}
int main()
return 0;
Program :-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int dx = X2 - X1;
int dy = Y2 - Y1;
if(dy<=dx){
int d = dy - (dx/2);
printf("%d%dn", x, y);
x++;
if (d < 0)
d = d + dy;
else
d += (dy - dx);
y++;
printf("%d%dn", x, y);
else if(dx<dy)
int d = dx - (dy/2);
printf("%d%dn", x, y);
y++;
if (d < 0)
d = d + dx;
else
d += (dx - dy);
x++;
printf("%d%dn", x, y);
putpixel(x,y,15);
}int main()
getch();
return 0;
}
Q5. Write the program to draw a circle using Bresenham’s Algorithm.
Program:
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
int main()
x = 0, y = r;
d = 3 - 2 * r;
while (y >= x)
x++;
if (d > 0)
y--;
d = d + 4 * (x - y) + 10;
else
d = d + 4 * x + 6;
delay(70);
getch();
return 0;
}
Q6. Write the program to draw the circle using Mid-point Algorithm.
Program:
#include<stdio.h>
#include<graphics.h>
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;
int main()
scanf("%d", &r);
printf("Enter co-ordinates of center(x and y): ");
drawcircle(x, y, r);
return 0;
Program :
#include<stdio.h>
#include<graphics.h>
void main(){
long x,y,x_center,y_center;
int g_driver=DETECT,g_mode;
clrscr();
initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\BGI");
scanf("%ld%ld",&x_center,&y_center);
scanf("%ld%ld",&a,&b);
x=0;
y=b;
a_sqr=a*a;
b_sqr=b*b;
fx=2*b_sqr*x;
fy=2*a_sqr*y;
d=b_sqr-(a_sqr*b)+(a_sqr*0.25);
do
putpixel(x_center+x,y_center+y,1);
putpixel(x_center-x,y_center-y,1);
putpixel(x_center+x,y_center-y,1);
putpixel(x_center-x,y_center+y,1);
if(d<0)
d=d+fx+b_sqr;
else
y=y-1;
d=d+fx+-fy+b_sqr;
fy=fy-(2*a_sqr);
x=x+1;
fx=fx+(2*b_sqr);
delay(10);
while(fx<fy);
tmp1=(x+0.5)*(x+0.5);
tmp2=(y-1)*(y-1);
d=b_sqr*tmp1+a_sqr*tmp2-(a_sqr*b_sqr);
do
putpixel(x_center+x,y_center+y,1);
putpixel(x_center-x,y_center-y,1);
putpixel(x_center+x,y_center-y,1);
putpixel(x_center-x,y_center+y,1);
if(d>=0)
d=d-fy+a_sqr;
else
x=x+1;
d=d+fx-fy+a_sqr;
fx=fx+(2*b_sqr);
}
y=y-1;
fy=fy-(2*a_sqr);
while(y>0);
getch();
closegraph();
Program:
#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;
int main()
{
int gd=DETECT,gm,s;
initgraph(&gd,&gm,(char*)"");
cout<<"1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Shearing "<<endl;
cout<<"Selection:";
cin>>s;
switch(s)
{
case 1:
{ int x1=200,y1=150,x2=300,y2=250;
int tx=50,ty=50;
cout<<"Rectangle before translation"<<endl;
setcolor(3);
rectangle(x1,y1,x2,y2);
setcolor(4);
cout<<"Rectangle after translation"<<endl;
rectangle(x1+tx,y1+ty,x2+tx,y2+ty);
getch();
break;
}
case 2:
{ long x1=200,y1=200,x2=300,y2=300;
double a;
cout<<"Rectangle with rotation"<<endl;
setcolor(3);
rectangle(x1,y1,x2,y2);
cout<<"Angle of rotation:";
cin>>a;
a=(a*3.14)/180;
long xr=x1+((x2-x1)*cos(a)-(y2-y1)*sin(a));
long yr=y1+((x2-x1)*sin(a)+(y2-y1)*cos(a));
setcolor(2);
rectangle(x1,y1,xr,yr);
getch();
break;}
case 3:
{
int x1=30,y1=30,x2=70,y2=70,y=2,x=2;
cout<<"Before scaling"<<endl;
setcolor(3);
rectangle(x1,y1,x2,y2);
cout<<"After scaling"<<endl;
setcolor(10);
rectangle(x1*x,y1*y,x2*x,y2*y);
getch();
break;}
case 4:
{
int x1=200,y1=300,x2=500,y2=300,x3=350,y3=400;
cout<<"triangle before reflection"<<endl;
setcolor(3);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x2,y2,x3,y3);cout<<"triangle after reflection"<<endl;
setcolor(5);
line(x1,-y1+500,x2,-y2+500);
line(x1,-y1+500,x3,-y3+500);
line(x2,-y2+500,x3,-y3+500);
getch();
break;}
case 5:
{
int x1=400,y1=100,x2=600,y2=100,x3=400,y3=200,x4=600,y4=200,shx=2;
cout<<"Before shearing of rectangle"<<endl;
setcolor(3);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x4,y4);
line(x2,y2,x4,y4);
cout<<"After shearing of rectangle"<<endl;
x1=x1+shx*y1;
x2=x2+shx*y2;
x3=x3+shx*y3;
x4=x4+shx*y4;
setcolor(13);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x4,y4);
line(x2,y2,x4,y4);getch();}default:
{
cout<<"Invalid Selection"<<endl;
break;
}
}closegraph();
return 0;
}
9. Write a program for 2D Scaling of an object.
Program :-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main(){
int x,y,x1,y1,x2,y2;
int scl_fctr_x,scl_fctr_y;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
scanf("%d %d",&x,&y);
scanf("%d %d",&x1,&y1);
scanf("%d %d",&x2,&y2);
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,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);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
Output:
Q10: Write a program to window to viewport mapping.
Program:-
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main ()
printf ("Please enter Window coordinates \n First enter XMax, YMax =");
cleardevice ();
delay (50);
//Window
rectangle (W_xmin, W_ymin, W_xmax, W_ymax);
//drawing a triangle
// viewport
V_xmin = 300;
V_ymin = 30;
V_xmax = 550;
V_ymax = 350;
// calculatng Sx and Sy
// drawing triangle
getch ();
closegraph ();}
Output:-
Program:-
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
int rcode_begin[4]={0,0,0,0},rcode_end[4]={0,0,0,0},region_code[4];
int W_xmax,W_ymax,W_xmin,W_ymin,flag=0;
float slope;
int gr=DETECT,gm;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
scanf("%d %d",&W_xmin,&W_ymin);
scanf("%d %d",&W_xmax,&W_ymax);
scanf("%d %d",&x,&y);
scanf("%d %d",&x1,&y1);
cleardevice();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(x,y,x1,y1);
line(0,0,600,0);
line(0,0,0,600);
if(y>W_ymax) {
rcode_begin[0]=1; // Top
flag=1 ;
if(y<W_ymin) {
rcode_begin[1]=1; // Bottom
flag=1;
if(x>W_xmax) {
rcode_begin[2]=1; // Right
flag=1;
if(x<W_xmin) {
rcode_begin[3]=1; //Left
flag=1;
if(y1>W_ymax){
rcode_end[0]=1; // Top
flag=1;
if(y1<W_ymin) {
rcode_end[1]=1; // Bottom
flag=1;
if(x1>W_xmax){
rcode_end[2]=1; // Right
flag=1;
if(x1<W_xmin){
rcode_end[3]=1; //Left
flag=1;
if(flag==0)
flag=1;
for(i=0;i<4;i++){
if(region_code[i]==1)
flag=0;
if(flag==0)
else{
slope=(float)(y1-y)/(x1-x);
y=y+(float) (W_xmin-x)*slope ;
x=W_xmin;
y=y+(float) (W_xmax-x)*slope ;
x=W_xmax;
x=x+(float) (W_ymax-y)/slope ;
y=W_ymax;
x=x+(float) (W_ymin-y)/slope ;
y=W_ymin;
// end points
y1=y1+(float) (W_xmin-x1)*slope ;
x1=W_xmin;
y1=y1+(float) (W_xmax-x1)*slope ;
x1=W_xmax;
x1=x1+(float) (W_ymax-y1)/slope ;
y1=W_ymax;
}
if(rcode_end[0]==0 && rcode_end[1]==1) // bottom
x1=x1+(float) (W_ymin-y1)/slope ;
y1=W_ymin;
delay(1000);
clearviewport();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(0,0,600,0);
line(0,0,0,600);
setcolor(RED);
line(x,y,x1,y1);
getch();
closegraph();
Output:-
Q12. Write a program for creating various types of texts and fonts.
Program :-
#include <graphics.h>
#include <conio.h>
int main()
{
//initilizing graphic driver and
int graphicdriver=DETECT,graphicmode;
initgraph(&graphicdriver,&graphicmode,"c:\\turboc3\\bgi");
//initilizing variables
y = y + 20;
getch();
return 0;
}
Q13. Write a program for creating 2D objects.
Program:
#include <graphics.h>
#include <conio.h>
int main() {
getch();
closegraph();
return 0;
}
Q14. Write a program for creating three dimensional object.
Program:-
#include <graphics.h>
#include <conio.h>
// Connect corners
int main() {
getch();
closegraph();
return 0;
}
Output:
Program :
/*
Bezier Curve
*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int x,y,z;
void main()
{
float u;
int gd,gm,ymax,i,n,c[4][3];
for(i=0;i<4;i++)
{
c[i][0]=0;
c[i][1]=0;
}
c[4][0]=c[0][0];
c[4][1]=c[0][1];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\Turboc3\\bgi");
ymax = 480;
setcolor(13);
for(i=0;i<3;i++)
{
line(c[i][0],ymax-c[i][1],c[i+1][0],ymax-c[i+1][1]);
}
setcolor(3);
n=3;
for(i=0;i<=40;i++)
{
u=(float)i/40.0;
bezier(u,n,c);
if(i==0)
{
moveto(x,ymax-y);
}
else
{
lineto(x,ymax-y);
}
getch();
}
getch();
}
x=0;y=0;z=0;
for(j=0;j<=n;j++)
{
b=blend(j,n,u);
x=x+(p[j][0]*b);
y=y+(p[j][1]*b);
z=z+(p[j][2]*b);
}
}
Program :
#include <graphics.h>
#include <conio.h>
int main() {
int x = 0;
while (!kbhit()) {
cleardevice(); // Clear previous frame
setcolor(WHITE);
x += 5; // Translate right
if (x > getmaxx())
x = 0; // Reset to left
closegraph();
return 0;
}
Q 17. Write a program to visualize the projection of 3D images.
Program :
#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
void draw3d(int fs,int x[20],int y[20],int tx,int ty,int d);
void draw3d(int fs,int x[20],int y[20],int tx,int ty,int d)
{
int i,j,k=0;
for(j=0;j<2;j++)
{
for(i=0;i<fs;i++)
{
if(i!=fs-1)
line(x[i]+tx+k,y[i]+ty-k,x[i+1]+tx+k,y[i+1]+ty-k);
else
line(x[i]+tx+k,y[i]+ty-k,x[0]+tx+k,y[0]+ty-k);
}
k=d;
}
for(i=0;i<fs;i++)
{
line(x[i]+tx,y[i]+ty,x[i]+tx+d,y[i]+ty-d);
}
}
void main()
{
int gd=DETECT,gm;
int x[20],y[20],tx=0,ty=0,i,fs,d;
initgraph(&gd,&gm,"");
printf("No of sides (front view only) : ");
scanf("%d",&fs);
printf("Co-ordinates : ");
for(i=0;i<fs;i++)
{
printf("(x%d,y%d)",i,i);
scanf("%d%d",&x[i],&y[i]);
}
printf("Depth :");
scanf("%d",&d);
draw3d(fs,x,y,tx,ty,d);
getch();//front view
setcolor(14);
for(i=0;i<fs;i++)
{
if(i!=fs-1)
line(x[i]+200,y[i],x[i+1]+200,y[i+1]);
else
line(x[i]+200,y[i],x[0]+200,y[0]);
}
getch();//top view
for(i=0;i<fs-1;i++)
{
line(x[i],300,x[i+1],300);
line(x[i],300+d*2,x[i+1],300+d*2);
line(x[i],300,x[i],300+d*2);
line(x[i+1],300,x[i+1],300+d*2);
}
getch();//side view
for(i=0;i<fs-1;i++)
{
line(10,y[i],10,y[i+1]);
line(10+d*2,y[i],10+d*2,y[i+1]);
line(10,y[i],10+d*2,y[i]);
line(10,y[i+1],10+d*2,y[i+1]);
}
getch();
closegraph();
}
Output:
Q18. Write a program to produce animations effect of triangle transform int square and then
into circle.
Program :
#include <graphics.h>
#include <conio.h>
int main() {
int step, i;
cleardevice();
delay(100);
delay(500);
// Shrink triangle
// Grow square
delay(100);
delay(500);
cleardevice();
// Shrink square
// Grow circle
delay(100);
getch();
closegraph();
return 0;