0% found this document useful (0 votes)
41 views27 pages

CGMT Practical - File

The programs implement various computer graphics algorithms and operations: 1) Programs 1-3 implement drawing basic shapes like lines, circles, and ellipses and performing transformations like scaling and rotation. 2) Programs 4-5 implement plotting pixels and translating objects. 3) Programs 6-7 implement polygon clipping and drawing lines using the DDA algorithm. 4) Programs 8-9 implement circle and ellipse generation using midpoint and elliptic algorithms. 5) Program 10 implements object rotation about an arbitrary point.

Uploaded by

Purva Sukhwal
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)
41 views27 pages

CGMT Practical - File

The programs implement various computer graphics algorithms and operations: 1) Programs 1-3 implement drawing basic shapes like lines, circles, and ellipses and performing transformations like scaling and rotation. 2) Programs 4-5 implement plotting pixels and translating objects. 3) Programs 6-7 implement polygon clipping and drawing lines using the DDA algorithm. 4) Programs 8-9 implement circle and ellipse generation using midpoint and elliptic algorithms. 5) Program 10 implements object rotation about an arbitrary point.

Uploaded by

Purva Sukhwal
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/ 27

List of Programs :

1) Implementation of Line, Circle and ellipse attributes.

2) Two Dimensional transformations – Scaling.

3) Drawing 3D objects and scenes.

4) To plot a point (pixel) on the screen.

5) To translate an object with translation parameters in X and Y


directions.

6) Sutherland – Hodgeman Polygon clipping Algorithm.

7) To draw a straight line using DDA Algorithm.

8) Implementation of mid-point circle generating Algorithm.

9) Implementation of ellipse generating Algorithm.

10) To perform the rotation of an object with certain angle about an


arbitrary point.
Program 1
Implementation of Line, Circle and ellipse attributes.

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

void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
int x1,y1,x2,y2,ls,lt,lc;
printf("Enter coordinates for 1st point of line:");
scanf("%d%d",&x1,&y1);
printf("Enter coordinates for 2nd point of line:");
scanf("%d%d",&x2,&y2);
printf("Enter a number for linestyle:\n");
scanf("%d",&ls);
printf("Enter for 1/2/3 for thickness:\n");
scanf("%d",&lt);
printf("Enter a number for color:\n");
scanf("%d",&lc);
clrscr();
cleardevice();
setcolor(lc);
setlinestyle(ls,1,lt);
line(x1,y1,x2,y2);

getch();
closegraph();

Output:
Program 2
Two Dimensional transformations – Scaling.

#include<stdio.h>
#include<graphics.h>

void findNewCoordinate(int s[][2], int p[][1])


{
int temp[2][1] = { 0 };

for (int i = 0; i < 2; i++)


for (int j = 0; j < 1; j++)
for (int k = 0; k < 2; k++)
temp[i][j] += (s[i][k] * p[k][j]);

p[0][0] = temp[0][0];
p[1][0] = temp[1][0];
}

// Scaling the Polygon


void scale(int x[], int y[], int sx, int sy)
{
// Triangle before Scaling
line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);

// Initializing the Scaling Matrix.


int s[2][2] = { sx, 0, 0, sy };
int p[2][1];

// Scaling the triangle


for (int i = 0; i < 3; i++)
{
p[0][0] = x[i];
p[1][0] = y[i];

findNewCoordinate(s, p);

x[i] = p[0][0];
y[i] = p[1][0];
}
// Triangle after Scaling
line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);
}

// Driven Program
int main()
{
int x[] = { 100, 200, 300 };
int y[] = { 200, 100, 200 };
int sx = 2, sy = 2;

int gd, gm;


detectgraph(&gd, &gm);
initgraph(&gd, &gm," ");

scale(x, y, sx,sy);
getch();

return 0;
}

Output:
Program 3
Drawing three dimensional objects and scenes.

#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT, gm, i, x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x=getmaxx()/3;
y=getmaxx()/3;
setbkcolor(WHITE);
setcolor(BLUE);
for(i=1;i<=8;i++)
{
setfillstyle(i,i);
delay(20);
circle(x, y, i*20);
floodfill(x-2+i*20,y,BLUE); }
getch();
closegraph();
}
Output:
Program 4
To plot a point (pixel) on the screen.

#include <graphics.h>
#include <stdio.h>
#include<conio.h>
void main()
{

int gd = DETECT, gm, color;


initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
putpixel(85, 35, GREEN);
putpixel(30, 40, RED);
putpixel(115, 50, YELLOW);
putpixel(135, 50, CYAN);
putpixel(45, 60, BLUE);
putpixel(20, 100, WHITE);
putpixel(200, 100, LIGHTBLUE);
putpixel(150, 100, LIGHTGREEN);
putpixel(200, 50, YELLOW);
putpixel(120, 70, RED);
getch();
closegraph();

}
Output:
Program 5
To translate an object with translation parameters in X and Y
directions.

#include<stdio.h>
#include<graphics.h>

void findNewCoordinate(int s[][2], int p[][1])


{
int temp[2][1] = { 0 };
for (int i = 0; i < 2; i++)
for (int j = 0; j < 1; j++)
for (int k = 0; k < 2; k++)
temp[i][j] += (s[i][k] * p[k][j]);
p[0][0] = temp[0][0];
p[1][0] = temp[1][0];
}

// Scaling the Polygon


void scale(int x[], int y[], int sx, int sy)
{
// Triangle before Scaling
line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);
// Initializing the Scaling Matrix.
int s[2][2] = { sx, 0, 0, sy };
int p[2][1];

// Scaling the triangle


for (int i = 0; i < 3; i++)
{
p[0][0] = x[i];
p[1][0] = y[i];
findNewCoordinate(s, p);
x[i] = p[0][0];
y[i] = p[1][0];
}
// Triangle after Scaling
line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);
}
// Driven Program
int main()
{ int x[] = { 100, 200, 300 };
int y[] = { 200, 100, 200 };
int sx = 2, sy = 2;
int gd, gm;
detectgraph(&gd, &gm);
initgraph(&gd, &gm," ");
scale(x, y, sx,sy);
getch();
return 0;
}

Output:
Program 6
Sutherland – Hodgeman Polygon clipping Algorithm.

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int gd,gm,n,*x,i,k=0;
int
wx1=220,wy1=140,wx2=420,wy2=140,wx3=420,wy3=340,wx4=220,w
y4=340;
int w[]={220,140,420,140,420,340,220,340,220,140};//array for
drawing window
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi"); //initializing graphics
printf("Window:-");
setcolor(RED); //red colored window
drawpoly(5,w); //window drawn
printf("Enter the no. of vertices of polygon: ");
scanf("%d",&n);
x = malloc(n*2+1);
printf("Enter the coordinates of points:\n");
k=0;
for(i=0;i<n*2;i+=2) //reading vertices of polygon
{
printf("(x%d,y%d): ",k,k);
scanf("%d,%d",&x[i],&x[i+1]);
k++;
}
x[n*2]=x[0];
x[n*2+1]=x[1];
setcolor(WHITE);
drawpoly(n+1,x);
printf("\nPress a button to clip a polygon..");
getch();
setcolor(RED);
drawpoly(5,w);
setfillstyle(SOLID_FILL,BLACK);
floodfill(2,2,RED);
gotoxy(1,1);
printf("\nThis is the clipped polygon..");
getch();

cleardevice();
closegraph();
return 0;
}

Output:
Program 7
To draw a straight line using DDA Algorithm.

#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");

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

closegraph();
}

Output:
Program 8
Implementation of mid-point circle generating Algorithm.

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

class bresen
{
float x, y,a, b, r, p;
public:
void get ();
void cal ();
};
void main ()
{
bresen b;
b.get ();
b.cal ();
getch ();
}
Void bresen :: get ()
{
cout<<"ENTER CENTER AND RADIUS";
cout<< "ENTER (a, b)";
cin>>a>>b;
cout<<"ENTER r";
cin>>r;
}
void bresen ::cal ()
{
/* request auto detection */
int gdriver = DETECT,gmode, errorcode;
int midx, midy, i;
/* initialize graphics and local variables */
initgraph (&gdriver, &gmode, " ");
/* read result of initialization */
errorcode = graphresult ();
if (errorcode ! = grOK) /*an error occurred */
{
printf("Graphics error: %s \n", grapherrormsg (errorcode);
printf ("Press any key to halt:");
getch ();
exit (1); /* terminate with an error code */
}
x=0;
y=r;
putpixel (a, b+r, RED);
putpixel (a, b-r, RED);
putpixel (a-r, b, RED);
putpixel (a+r, b, RED);
p=5/4)-r;
while (x<=y)
{
If (p<0)
p+= (4*x)+6;
else
{
p+=(2*(x-y))+5;
y--;
}
x++;
putpixel (a+x, b+y, RED);
putpixel (a-x, b+y, RED);
putpixel (a+x, b-y, RED);
putpixel (a+x, b-y, RED);
putpixel (a+x, b+y, RED);
putpixel (a+x, b-y, RED);
putpixel (a-x, b+y, RED);
putpixel (a-x, b-y, RED);
}
}

Output:
Program 9
Implementation of ellipse generating Algorithm.

#include<bits/stdc++.h>
#include<graphics.h>
using namespace std;
int main()
{
int rx,ry,gm,gd;
float x,y;
float d1,d2,dx,dy;
//take ellipse radius as input we will not take center as it is default (0,0)
cout<<"enter radii rx,ry:"<>rx>>ry;
//initialize starting point
x=0;y=ry;
//initialize graph
detectgraph(&gd,&gm);
initgraph(&gd,&gm," ");
//initial decision variable for region 1
d1=ry*ry-rx*rx*ry+(rx*rx)/4;
do{
//draw all four symmetric points
putpixel(x+300,y+300,1);
putpixel(-x+300,y+300,2);
putpixel(-x+300,-y+300,3);
putpixel(x+300,-y+300,4);
//change in coordinates based on decision variable
if(d1<0 .0="" 2="" all="" based="" change="" coordinates="" d1="d1+dx;"
d2="" decision="" delay="" do="" draw="" dx="rx*rx*(2-
2*y)+ry*ry*(3+2*x);" else="" for="" four="" if="" in="" initial="" on=""
points="" putpixel="" region="" rx="" ry="" symmetric="" variable=""
while="" x="" y="">0.0)
{
y=y-1;
dy=rx*rx*(3-2*y);
d2=d2+dy;
}
else
{
y=y-1;x=x+1;
dy=ry*ry*(2+2*x)+rx*rx*(3-2*y);
d2=d2+dy;
}
delay(10);
}while(y>0);

getch();
return 0;
}

Output:
Program 10
To perform the rotation of an object with certain angle
about an arbitrary point.

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

//function declarations
void mul(float mat[3][3],float vertex[10][3],int n);
void rotate(float vertex[10][3],int n);
void init(float vertex[10][3],int n);

int main()
{
int i,x,y;

float vertex[10][3];
int n;

//clear the screen


clrscr();

//taking inputs for the polygon object


cout<<"Enter the no. of vertex : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the points (x,y): ";
cin>>x>>y;
vertex[i][0]=x;
vertex[i][1]=y;
vertex[i][2]=1;
}

//calling the rotate() function to rotate the given object


rotate(vertex,n);

getch();

return 0;
}

void int(float vertex[10][3],int n)


{
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");

//drawing the co-ordinate axes


setcolor(10);
line(0,240,640,240); //drawing X axis
line(320,0,320,480); //drawing Y axis

//drawing graph legends


setcolor(3);
line(450,20,490,20);
setcolor(15);
line(450,50,490,50);
setcolor(6);
outtextxy(350,20,"Original");
outtextxy(350,50,"Transformed");

//drawing the object


setcolor(3);
for(i=0;i<n-1;i++)
{
line(320+vertex[i][0],240-vertex[i][1],320+vertex[i+1][0],240-
vertex[i+1][1]);
}
line(320+vertex[n-1][0],240-vertex[n-1][1],320+vertex[0][0],240-
vertex[0][1]);

}
void mul(float mat[3][3],float vertex[10][3],int n)
{
int i,j,k;
float res[10][3];
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{ res[i][j]=0;
for(k=0;k<3;k++)
{
res[i][j] = res[i][j] + vertex[i][k]*mat[k][j];
}
}
}
//drawing the transformed image (rotated object)
setcolor(15);
for(i=0;i<n-1;i++)
{
line(320+res[i][0],240-res[i][1],320+res[i+1][0],240-res[i+1][1]);
}
line(320+res[n-1][0],240-res[n-1][1],320+res[0][0],240-res[0][1]);

void rotate(float vertex[][3],int n)


{
//rotation transformation matrix
float rot[3][3];

float theta,xp,yp,a;

//taking reference point


cout<<"\nEnter the point of rotation (x,y): ";
cin>>xp>>yp;

//taking abgle of rotation


cout<<"Enter the angle of rotation: ";
cin>>theta;

//converting angle in degrees to radians


a=(theta*3.142)/180;
rot[0][0]=cos(a);
rot[1][0]=-1*sin(a);
rot[2][0]=-xp*cos(a)+yp*sin(a)+xp;
rot[0][1]=sin(a);
rot[1][1]=cos(a);
rot[2][1]=-xp*sin(a)-yp*cos(a)+yp;
rot[0][2]=0;
rot[1][2]=0;
rot[2][2]=1;

init(vertex,n);
mul(rot,vertex,n);

Output:

You might also like