CGMT Practical - File
CGMT Practical - File
#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",<);
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>
p[0][0] = temp[0][0];
p[1][0] = temp[1][0];
}
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;
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()
{
}
Output:
Program 5
To translate an object with translation parameters in X and Y
directions.
#include<stdio.h>
#include<graphics.h>
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");
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;
getch();
return 0;
}
}
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]);
float theta,xp,yp,a;
init(vertex,n);
mul(rot,vertex,n);
Output: