CGM Exp

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

List of Experiments

1 To plot a point (pixel) on the screen


2 Divide the screen into four quadrants
3 Draw different objects on screen
4 Implementation of Line, Circle and ellipse attributes
5 Draw a Hut
6 To draw a straight line using DDA algorithm
7 Draw a cycle moving left to right across the screen
8 Implementation of Bresenhem’s line drawing algo
9 Implementation of mid-point circle generating Algorithm
10 Draw a bouncing ball using circle function
11 Implementation of ellipse generating Algorithm
12 TWO Dimensional transformations - Translation, Rotation, Scaling, Reflection,
Shear
13 Composite 2D Transformations
14 Draw two triangles and perform rotation of one triangle over another on
centroid
15 Cohen Sutherland 2D line clipping and Windowing
16 Sutherland Hodgeman Polygon clipping algorithm
17 Three dimensional transformations - Translation, Rotation, Scaling
18 Composite 3D transformations

19 Drawing three dimensional objects and Scenes


20 Generating Fractal images
MID POINT CIRCLE ALGO
DDA Line Algorithm
Bresenhem’s line drawing algo
Bouncing Ball
Drawing three dimensional objects
3d chart

#include <graphics.h>

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");

settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(275,0,"3D BAR GRAPH");

setlinestyle(SOLID_LINE,0,2);
/* Print X and Y Axis */
line(90,410,90,50);
line(90,410,590,410);
line(85,60,90,50);
line(95,60,90,50);
line(585,405,590,410);
line(585,415,590,410);

outtextxy(65,60,"Y");
outtextxy(570,420,"X");
outtextxy(70,415,"O");

/* Print 3D bars */
setfillstyle(XHATCH_FILL, RED);
bar3d(150,80,200,410, 15, 1);
bar3d(225,100,275,410, 15, 1);
bar3d(300,120,350,410, 15, 1);
bar3d(375,170,425,410, 15, 1);
bar3d(450,135,500,410, 15, 1);

closegraph();
return 0;
}
Generating Fractal images

#include <graphics.h>

#include<conio.h>

#include <stdio.h>

#define MAXCOUNT 30

void fractal(float left, float top, float xside, float


yside)

float xscale, yscale, zx, zy, cx, tempx, cy;

int x, y, i, j;

int maxx, maxy, count;

// getting maximum value of x-axis and y-axis of


screen

maxx = getmaxx();

maxy = getmaxy();

xscale = xside / maxx;

yscale = yside / maxy;

rectangle(0, 0, maxx, maxy);

for (y = 1; y <= maxy - 1; y++) {

for (x = 1; x <= maxx - 1; x++)

cx = x * xscale + left;

cy = y * yscale + top;

zx = 0;
zy = 0;

count = 0;

while ((zx * zx + zy * zy < 4) && (count <


MAXCOUNT))

tempx = zx * zx - zy * zy + cx;

zy = 2 * zx * zy + cy;

zx = tempx;

count = count + 1;

putpixel(x, y, count);

} }}

void main()

int gd = DETECT, gm, errorcode;

float left, top, xside, yside;

// setting the left, top, xside and yside for the


screen and image to be displayed

left = -1.75;

top = -0.25;

xside = 0.25;

yside = 0.45;

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


fractal(left, top, xside, yside);

getch();

closegraph();

}
MOVING CYCLE
Eclipse algo

#include<stdio.h>
#include<graphics.h>
void main(){
long x,y,x_center,y_center;
long a_sqr,b_sqr, fx,fy, d,a,b,tmp1,tmp2;
int g_driver=DETECT,g_mode;
clrscr();

initgraph(&g_driver,&g_mode,"C:\\TURBOC3\
\BGI");
printf("********* MID POINT ELLIPSE
ALGORITHM *********");
printf("\n\n Enter coordinate x and y = ");
scanf("%ld%ld",&x_center,&y_center);
printf("\n Now enter constants a and b = ");
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();
}
All 2D Transformations

#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
int x1,y1,x2,y2;
void translation()
{
int tx,ty,xn1,xn2,yn1,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;
line(x1,y1,x2,y2);
line(xn1,yn1,xn2,yn2);
getch();
}

void scaling()
{
int xn1,xn2,yn1,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;
line(x1,y1,x2,y2);
line(xn1,yn1,xn2,yn2);
getch();
}

void rotation()
{
int r;
float rx,xn1,xn2,yn1,yn2;
printf("\n enter the angle for rotation");
scanf("%d",&r);
cleardevice();
outtextxy(500,200,"ROTATION");
rx=(r*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);
line(x1,y1,x2,y2);
line(xn1,yn1,xn2,yn2);
getch();
}

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

void reflection()
{
int xn1,xn2,yn1,yn2;
cleardevice();
outtextxy(300,100,"REFLECTION");
if((x1<y1)^(x2<y2))
{
xn1=x1+50;
xn2=x2+50;
yn1=y1;
yn2=y2;
}
else
{
xn1=x1;
xn2=x2;
yn1=y1+50;
yn2=y2+50;
}
line(x1,y1,x2,y2);
line(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");
line(x1,y1,x2,y2);
getch();
}
void main()
{
int ch,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\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);
}

Rotating Triangle
#include<graphics.h>
#include<conio.h>
Cohen Sutherland 2D line
#include<stdio.h>
#include<math.h>
clipping and Windowing
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 x,y,x1,y1,i, xc,yc;
int gr=DETECT,gm;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\n****** Cohen Sutherlsnd Line Clipping
algorithm ***********");
printf("\n Now, enter XMin, YMin =");

scanf("%d %d",&W_xmin,&W_ymin);
printf("\n First enter XMax, YMax =");
scanf("%d %d",&W_xmax,&W_ymax);
printf("\n Please enter intial point x and y=
");
scanf("%d %d",&x,&y);
printf("\n Now, enter final point x1 and y1= ");
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;
}

//end point of Line


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)
{
printf("No need of clipping as it is already in
window");
}
flag=1;
for(i=0;i<4;i++){
region_code[i]= rcode_begin[i] && rcode_end[i] ;
if(region_code[i]==1)
flag=0;
}
if(flag==0)
{
printf("\n Line is completely outside the
window");
}
else{
slope=(float)(y1-y)/(x1-x);
if(rcode_begin[2]==0 && rcode_begin[3]==1) //
left
{
y=y+(float) (W_xmin-x)*slope ;
x=W_xmin;

}
if(rcode_begin[2]==1 && rcode_begin[3]==0)
// right
{
y=y+(float) (W_xmax-x)*slope ;
x=W_xmax;

}
if(rcode_begin[0]==1 &&
rcode_begin[1]==0) // top
{
x=x+(float) (W_ymax-y)/slope ;
y=W_ymax;

}
if(rcode_begin[0]==0 &&
rcode_begin[1]==1) // bottom
{
x=x+(float) (W_ymin-y)/slope ;
y=W_ymin;

}
// end points
if(rcode_end[2]==0 && rcode_end[3]==1) //left
{
y1=y1+(float) (W_xmin-x1)*slope ;
x1=W_xmin;

}
if(rcode_end[2]==1 && rcode_end[3]==0) //
right
{
y1=y1+(float) (W_xmax-x1)*slope ;
x1=W_xmax;

}
if(rcode_end[0]==1 && rcode_end[1]==0) //
top
{
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();
}
#include<stdio.h> Sutherland Hodgeman Polygon
#include<conio.h> clipping algorithm
#include<graphics.h>
#include<math.h>
void clip(float,float,float);
int i,j=0,n;
int rx1,rx2,ry1,ry2;
float x1[8],y1[8];
void main()
{
int gd=DETECT,gm;
int i,n;
float x[8],y[8],m;
clrscr();
initgraph(&gd,&gm,"");
printf("coordinates for rectangle : ");
scanf("%d%d%d%d",&rx1,&ry1,&rx2,&ry2);
printf("no. of sides for polygon : ");
scanf("%d",&n);
printf("coordinates : ");
for(i=0;i<n;i++)
{
scanf("%f%f",&x[i],&y[i]);
}
cleardevice();
outtextxy(10,10,"Before clipping");
outtextxy(10,470,"Press any key....");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<n-1;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[i],y[i],x[0],y[0]);
getch();
cleardevice();
for(i=0;i<n-1;i++)
{
m=(y[i+1]-y[i])/(x[i+1]-x[i]);
clip(x[i],y[i],m);
clip(x[i+1],y[i+1],m);
}
m=(y[i]-y[0])/(x[i]-x[0]);
clip(x[i],y[i],m);
clip(x[0],y[0],m);
outtextxy(10,10,"After clipping");
outtextxy(10,470,"Press any key....");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<j-1;i++)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
getch();
}

void clip(float e,float f,float m)


{
while(e<rx1 e>rx2 f<ry1 f>ry2)
{
if(e<rx1)
{
f+=m*(rx1-e);
e=rx1;
}
else if(e>rx2)
{
f+=m*(rx2-e);
e=rx2;
}
if(f<ry1)
{
e+=(ry1-f)/m;
f=ry1;
}
else if(f>ry2)
{
e+=(ry2-f)/m;
f=ry2;
}
}
x1[j]=e;
y1[j]=f;
j++;
}
3D Transformation

3D Translation:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int x,y,z,o,x1,x2,y1,y2;
int gd=DETECT,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
//setfillstyle(0,getmaxcolor());
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);
//axis();
printf("After translation:");
bar3d(midx+x+50,midy-(y+100),midx+x+60,midy-(y+90),10,1);
getch();
closegraph();
}

3D Scaling:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int x,y,z,o,x1,x2,y1,y2;
int gd=DETECT,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
//setfillstyle(0,getmaxcolor());
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;

axis();

bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter scaling factors");
scanf("%d%d%d", &x,&y,&z);
//axis();
printf("After scaling");
bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1);
//axis();
getch();
closegraph();
}

3D Rotation:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int x,y,z,o,x1,x2,y1,y2;
int gd=DETECT,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
//setfillstyle(0,getmaxcolor());
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+x1,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 yaxis");
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
getch();
closegraph();
}
bresenham circle

1. #include <graphics.h>  
2. #include <stdlib.h>  
3. #include <stdio.h>  
4. #include <conio.h>  
5. #include <math.h>  
6.   
7.     void    EightWaySymmetricPlot(int xc,int yc,int x,int y)  
8.    {  
9.     putpixel(x+xc,y+yc,RED);  
10.     putpixel(x+xc,-y+yc,YELLOW);  
11.     putpixel(-x+xc,-y+yc,GREEN);  
12.     putpixel(-x+xc,y+yc,YELLOW);  
13.     putpixel(y+xc,x+yc,12);  
14.     putpixel(y+xc,-x+yc,14);  
15.     putpixel(-y+xc,-x+yc,15);  
16.     putpixel(-y+xc,x+yc,6);  
17.    }  
18.   
19.     void BresenhamCircle(int xc,int yc,int r)  
20.    {  
21.     int x=0,y=r,d=3-(2*r);  
22.     EightWaySymmetricPlot(xc,yc,x,y);  
23.   
24.     while(x<=y)  
25.      {  
26.       if(d<=0)  
27.              {  
28.         d=d+(4*x)+6;  
29.       }  
30.      else  
31.       {  
32.         d=d+(4*x)-(4*y)+10;  
33.         y=y-1;  
34.       }  
35.        x=x+1;  
36.        EightWaySymmetricPlot(xc,yc,x,y);  
37.       }  
38.     }  
39.   
40.     int  main(void)  
41.    {  
42.     /* request auto detection */  
43.     int xc,yc,r,gdriver = DETECT, gmode, errorcode;  
44.     /* initialize graphics and local variables */  
45.      initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");  
46.   
47.      /* read result of initialization */  
48.      errorcode = graphresult();  
49.   
50.       if (errorcode != grOk)  /* an error occurred */  
51.      {  
52.         printf("Graphics error: %s\n", grapherrormsg(errorcode)
);  
53.         printf("Press any key to halt:");  
54.         getch();  
55.         exit(1); /* terminate with an error code */  
56.      }  
57.        printf("Enter the values of xc and yc :");  
58.        scanf("%d%d",&xc,&yc);  
59.        printf("Enter the value of radius  :");  
60.        scanf("%d",&r);  
61.        BresenhamCircle(xc,yc,r);  
62.   
63.      getch();  
64.      closegraph();  
65.      return 0;  
66.     }  

You might also like