Sie sind auf Seite 1von 63

1

Ex. No: 1.a) Implementation of Line- Bresenhams Algorithm


Date:

Aim:
To write a C++ program to implement Bresenhams Algorithm to draw a line.
Algorithm:
Step 1: Start the program.
Step 2: Include necessary header files for graphics programming.
Step 3: Define the variables gd & gm, and set the path C:\\tc\\bgi.
Step 4: Get the values for co-ordinates (x1,y1) and (x2,y2) from the user.
Step 5: Calculate the values of dx,dy and p.
Step 6: Check the if condition and assign values accordingly.
Step 7: The condition is
If(p<0) then p=p+2*dy
Else p=p+2*(dy-dx)
Step 8: Increment the x and y values accordingly.
Step 9: Display the output
Step 10: Stop the program.
Program:
Line.cpp
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi");
2

int x1,x2,y1,y2; int dx,dy,x,y,p,xend;
cout<<"enter first point"<<endl;
cin>>x1>>x2;
cout<<"enter second point<<endl;
cin>>y1>>y2;
dx=abs(x1-x2);
dy=abs(y1-y2);
p=2*dy-dx;
if(x1>x2)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel(x,y,100);
outtextxy(x1,y1,"(x1,y1)");
outtextxy(x2,y2,"(x2,y2)");
while(x<xend)
{
x=x+1;
if(p<0)
3

{
p=p+2*dy;
}
else
{
y=y+1;
p=p+2*(dy-dx);
}
putpixel(x,y,100);
}
getch();
closegraph();
}
Output:


Result:
Thus the Bresenhams algorithm to draw the line was implemented and the output was
verified successfully.
4

Ex. No: 1.b) Implementation of Circle
Date:

Aim:
To write a C program to implement midpoint Algorithm to draw a circle.
Algorithm:
Step 1: Start the program.
Step 2: Include necessary header files for graphics programming.
Step 3: Define the variables gd & gm, and set the path C:\\tc\\bgi.
Step 4: Get the values for co-ordinates of center (xc,yc) from the user.
Step 5: Check for the conditions of p value.
Step 6: The condition is
If(p<0) then p=p+4*x+6
Else p=p+4*x-4*y+10
Step 7: Display the output
Step 8: Stop the program.
Program
Circle.C
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int xc,yc,x,y,r,p;
int gd=DETECT,gm;
5

initgraph(&gd,&gm,"c:\\tc\\bgi ");
cleardevice();
outtextxy(100,10,"BRESENHAMS CIRCLE GENERATION ALGORITHM");
printf("\n\nENTER THE VALUE OF CENTER(XC,YC) AND RADIUS:)");
scanf("%d%d%d",&xc,&yc,&r);
p=3-2*r;
x=0;
y=r;
while(x<=y)
{
putpixel(xc+x,yc-y,40);
putpixel(xc+x,yc+y,40);
putpixel(xc-x,yc+y,40);
putpixel(xc-x,yc-y,40);
putpixel(xc-y,yc-x,40);
putpixel(xc-y,yc+x,40);
putpixel(xc+y,yc-x,40);
putpixel(xc+y,yc+x,40);
if(p<0)
p=p+4*x+6;
else
{
p=p+4*x-4*y+10;
y=y-1;
}
x=x+1;
}
6

setcolor(12);
outtextxy(300,275,"CIRCLE");
getch();
closegraph();
}

Output:
BRESENHAMS CIRCLE GENERATION ALGORITHM
ENTER THE VALUE OF CENTER(XC,YC) AND RADIUS:
120
120
10







Result:
Thus the midpoint algorithm to draw a circle was implemented and the output was
verified successfully.



7

Ex. No: 1.c) Implementation of Ellipse
Date:

Aim:
To write a C program to implement midpoint Algorithm to draw an ellipse.
Algorithm:
Step 1: Start the program.
Step 2: Include necessary header files for graphics programming.
Step 3: Define the variables gd & gm, and set the path C:\\tc\\bgi.
Step 4: Define two functions ellipsemidpoint() and drawellipse().
Step 5: Declare the required variables and get the co-ordinates of ellipse from the user.
Step 6: Calculate the values of p and check if conditions with different values of p.
Step 7: Using the radius and center co-ordinate values draw the ellipse.
Step 8: Display the output.
Step 9: Stop the program.
Program
Ellipse.C
#include<stdio.h>
#include<dos.h>
#include<graphics.h>
void ellipseMidpoint(float,float,float,float);
void drawEllipse(float,float,float,float);
void main()
{
float xc,yc,rx,ry;
int gd=DETECT,gm;
clrscr();
8

initgraph(&gd,&gm,"c:/tc/bgi");
printf("\nEnter the center coordinates of ellipse:");
scanf("%f%f",&xc,&yc);
printf("\n Enter the x_radius coordinates:");
scanf("%f",&rx);
printf("\n Enter the y-radius coordinates:");
scanf("%f",&ry);
ellipseMidpoint(xc,yc,rx,ry);
getch();
closegraph();
}
void ellipseMidpoint(float xc,float yc,float rx,float ry)
{
float rxsq=rx*rx;
float rysq=ry*ry;
float x=0,y=ry,p;
float px=0,py=2*rxsq*y;
drawEllipse(xc,yc,x,y);
p=rysq-(rxsq*ry)+(0.25*rxsq);
while(px<py)
{
x++;
px=px+2*rysq;
if(p<0)
p=p+rysq+px;
else
{
9

y--;
py=py-2*rxsq;
p=p+rysq+px-py;
}
drawEllipse(xc,yc,x,y);
delay(30);
}
p=rysq*(x+0.5)*(x+0.5)+rxsq*(y-1)*(y-1)-rxsq*rysq;
while(y>0)
{
y--;
py=py-2*rxsq;
if(p>0)
p=p+rxsq-py;
else
{
x++;
px=px+2*rysq;
p=p+rxsq-py+px;
}
drawEllipse(xc,yc,x,y);
delay(30);
}
}
void drawEllipse(float xc,float yc,float x,float y)
{
putpixel(xc+x,yc+y,RED);
10

putpixel(xc-x,yc+y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc-y,RED);
}

Output:
Enter the center coordinates of ellipse:
310
155
Enter the x_radius coordinates:10
Enter the y-radius coordinates:30







Result:
Thus the midpoint algorithm to draw an ellipse was implemented and the output was
verified successfully.



11

Ex. No: 2 Implementation of Line, Circle and Ellipse Attributes
Date:

Aim:
To write a C program to implement attributes of line, circle and ellipse.
Algorithm:
Step 1: Start the program.
Step 2: Include necessary header files for graphics programming.
Step 3: Define the variables gd & gm, and set the path C:\\tc\\bgi.
Step 4: For line attribute, the options are solid line, dotted line, center line, dashed line.
Step 5: Using switch case, choose whether line, circle or ellipse.
Step 6: In case of line, choose from the options either color or style.
Step 7: In case of circle, draw the circles of different colors.
Step 8: In case of ellipse, get the choice for color or style of ellipse.
Step 9: Display the output.
Step 10: Stop the program.
Program:
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
int main(void)
{
int gdriver=EGA,DETECT,gmode=EGAHI,errorcode;
int color,maxcolor,x,y,s,ch,ch1,ch2,i;
int midx,midy;
int radius=100;
12

int xradius=100,yradius=50;
char msg[80];
char *lname[]={"Solid Line", "Dotted Line", "Center Line", "Dashed Line", "Usebit
Line"};
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
errorcode=graphresult();
if (errorcode!=grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
do{
printf("\n1.Line\n2.Circle\n3.Ellipse\n");
printf("\nEnter Your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Attribute: 1.Color 2.Style:\n");
scanf("%d",&ch1);
switch(ch1)
{
case 1:maxcolor=getmaxcolor();
x=getmaxx()/2;
y=getmaxy()/2;
for(color=1;color<=maxcolor;color++)
13

{
cleardevice();
setcolor(color);
line(100,100,100,300);
sprintf(msg,"Color:%d",color);
outtextxy(x,y,msg);
getch();
}
closegraph();
break;
case 2:
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
for(s=0;s<5;s++)
{
setlinestyle(s,1,1);
line(20,20+s*50,120,120+s*50);
outtextxy(125,120+s*50,lname[s]);
}
getch();
closegraph();
break;
}
break;
case 2:
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
midx=getmaxx()/2;
midy=getmaxy()/2;
14

maxcolor=getmaxcolor();
for(color=1;color<=maxcolor;color++)
{
cleardevice();
setcolor(color);
circle(midx,midy,radius);
getch();
}
closegraph();
break;
case 3:
printf("\n1.pattern 2.colour\n");
printf("\nEnter your choice:\n");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
midx=getmaxx()/2;
midy=getmaxy()/2;
for(i=EMPTY_FILL;i<USER_FILL;i++)
{
setfillstyle(i,getmaxcolor());
fillellipse(midx, midy, xradius, yradius);
getch();
}
closegraph();
15

break;
case 2:
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
maxcolor=getmaxcolor();
for(color=1;color<=maxcolor;color++)
{
cleardevice();
setcolor(color);
ellipse(100,200,0,360,xradius,yradius);
getch();
}
closegraph();
break;
}
default://exit(0);
break;
}
}while(ch==3);
return 0;
}
Output:
1.Line
2.Circle
3.Ellipse
Enter Your choice: 1
Attribute: 1.Color 2.Style:
1
16



1.Line
2.Circle
3.Ellipse
Enter Your choice: 1
Attribute: 1.Color 2.Style:
2

1.Line
2.Circle
3.Ellipse
Enter Your choice: 2
17



1.Line
2.Circle
3.Ellipse
Enter Your choice: 3
1.pattern 2.colour
Enter your choice:
1

1.Line
2.Circle
3.Ellipse
Enter Your choice:
3
1.pattern 2.colour
18

Enter your choice:
2

















Result:
Thus the program was written and executed to implement the various attributes of line,
circle and ellipse and the output was verified successfully.

19

Ex. No: 3 Implementation of 2D Transformation
Date:

Aim:
To write a C program to implement two dimensional transformations.
Algorithm:
Step 1: Start the program.
Step 2: Include necessary header files for graphics programming.
Step 3: Declare the required variables.
Step 4: Use do while statement and get the choice from the user using switch case statement.
Step 5: Incase of translation, get the value of translation factor and draw the image.
Step 6: Incase of reflection, get the co-ordinates & calculate the reflection factor.
Step 7: Incase of rotation, get the rotation angle and draw the image.
Step 8: Incase of scaling, get the value of scaling factor and draw the image.
Step 9: Incase of shearing, choose either x-shear or y-shear and draw the corresponding image.
Step 10: Display the output accordingly.
Step 11: Stop the program.
Program:
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
int x1,x2,x3,y1,y2,y3,t,tx,sx,sy,shx,shy,ch;
float rx1,rx2,rx3,ry1,ry2,ry3;
float ang,theta;
int main(void)
20

{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode,"C:\\TC\\BGI");
errorcode = graphresult();
if(errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
else
{
do{
printf("\n1.Translation\n2.Reflection\n3.Rotation\n4.Scaling\n5.Shearing
x-axis 6.Shearing y-axis\n");
printf("\nEnter Your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter all coordinates values :");
scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
printf("\n Before Translation ");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
printf("\n Enter the value tranlsation factor :");
21

scanf("%d",&tx);
printf("\n After Translation\n ");
line(x1+tx,y1,x2+tx,y2);
line(x2+tx,y2,x3+tx,y3);
line(x3+tx,y3,x1+tx,y1);
break;
case 2:
printf("\n Enter all coordinates values :");
scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
printf("\n Before Reflection ");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
t=abs(y1-y3);
printf("\n After Reflection ");
line(x1,y1+10+(2*t),x2,y2+10);
line(x2,y2+10,x3,y3+10);
line(x3,y3+10,x1,y1+10+(2*t));
break;
case 3:
printf("\n Enter all coordinates values :");
scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
printf("\n Before Rotation ");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
printf("\n Enter the rotation angle :");
22

scanf("%f",&ang);
theta=((ang*3.14)/180);
rx1=x1*cos(theta)-y1*sin(theta);
rx2=x2*cos(theta)-y2*sin(theta);
rx3=x3*cos(theta)-y3*sin(theta);
ry1=x1*sin(theta)+y1*cos(theta);
ry2=x2*sin(theta)+y2*cos(theta);
ry3=x3*sin(theta)+y3*cos(theta);
printf("\n After Rotation ");
line(rx1,ry1,rx2,ry2);
line(rx2,ry2,rx3,ry3);
line(rx3,ry3,rx1,ry1);
break;
case 4:
printf("\n Enter all coordinates values :");
scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
printf("\n Before Scaling ");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
printf("\n Enter the Scale factor :");
scanf("%d %d",&sx,&sy);
printf("\n After Scaling ");
line(x1+sx,y1+sy,x2+sx,y2+sy);
line(x2+sx,y2+sy,x3+sx,y3+sy);
line(x3+sx,y3+sy,x1+sx,y1+sy);
break;
23

case 5:
printf("\n Enter all coordinates values :");
scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
printf("\n Before Shearing");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
printf("\n Enter the x-SHEAR (^.^) Value: ");
scanf("%d",&shx);
x1=x1+shx*y1;
x2=x2+shx*y2;
x3=x3+shx*y3;
printf("\n After Shearing ");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
break;
case 6:
printf("\n Enter all coordinates values :");
scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
printf("\n Before Shearing");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
printf("\n Enter the y-SHEAR (^.^) Value: ");
scanf("%d",&shy);
y1=y1+shy*x1;
24

y2=y2+shy*x2;
y3=y3+shy*x3;
printf("\n After Shearing ");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
break;
case 7:
default:
exit(0);
break;
}
}while(ch!=0);
}
getch();
closegraph();
return 0;
}
Output:
1. Translation
2. Reflection
3. Rotation
4. Scaling
5. Shearing
Enter Your choice
1
Enter all coordinates values 213 236 253 321 256 214



25

Before Translation

Enter the value translation vector 32
After Translation


1. Translation
2. Reflection
3. Rotation
4. Scaling
5. Shearing
Enter Your choice 2
Enter all coordinates values 213 236 253 321 256 214

After Reflection

26

1. Translation
2. Reflection
3. Rotation
4. Scaling
5. Shearing
Enter Your choice 3
Enter all coordinates values 213 236 253 321 256 214
Before Rotation

Enter the rotation angle 20
After Rotation


1. Translation
2. Reflection
3. Rotation
4. Scaling
5. Shearing
Enter Your choice 4
Enter all coordinates values 213 236 253 321 256 214
Before Scaling

Enter the scale factor 10 5

27

After Scaling


1. Translation
2. Reflection
3. Rotation
4. Scaling
5. Shearing
Enter Your choice 5
Enter all coordinates values 213 236 253 321 256 214
Before Shearing
Enter 0 for x axis and 1 for y axis 0

Enter the x-shear value 1
After Shearing







28

Before Shearing
Enter 0 for x axis and 1 for y axis 1

Enter the y-shear value 1















Result:
Thus the program was written and executed to implement the various 2D transformations
and the output was verified successfully.

29

Ex. No: 4 Implementation of Composite 2D Transformation
Date:

Aim:
To write a C program to implement composite two dimensional transformations.
Algorithm:
Step 1: Start the program.
Step 2: Include necessary header files for graphics programming.
Step 3: Declare the required variables.
Step 4: Get the co-ordinates values from the user.
Step 5: Incase of translation, get the value of translation factor and draw the image.
Step 6: Incase of reflection, get the co-ordinates & calculate the reflection factor.
Step 7: Incase of rotation, get the rotation angle and draw the image.
Step 8: Incase of scaling, get the value of scaling factor and draw the image.
Step 9: Incase of shearing, choose either x-shear or y-shear and draw the corresponding image.
Step 10: Display the output accordingly.
Step 11: Stop the program.
Program:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
int xa,xb,xc,ya,yb,yc,y1a,y1b,y1c,x1a,x1b,x1c,x2a,x2b,x2c,y2a,y2b,y2c;
int x3a,x3b,x3c,y3a,y3b,y3c,x4a,x4b,x4c,y4a,y4b,y4c,x5a,x5b,x5c,y5a,y5b,y5c;
int tx,shx,t,ch,shy;
float ang,theta,sx,sy;
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode,"C:\\TC\\BGI");
printf("\n\t\t\t 2D Composite Transformations");
printf("\n\n Enter all coordinates values :");
30

scanf("%d %d %d %d %d %d",&xa,&ya,&xb,&yb,&xc,&yc);
printf("\n\n The original Image");
line(xa,ya,xb,yb);
line(xb,yb,xc,yc);
line(xc,yc,xa,ya);
printf("\n\n Enter the value tranlsation factor :");
scanf("%d",&tx);
printf("\n\n After Translation ");
x1a=xa+tx;
x1b=xb+tx;
x1c=xc+tx;
y1a=ya;
y1b=yb;
y1c=yc;
line(x1a,y1a,x1b,y1b);
line(x1b,y1b,x1c,y1c);
line(x1c,y1c,x1a,y1a);
delay(1);
printf("\n\n Next Operation is Rotation");
printf("\n\n Enter the rotation angle :");
scanf("%f",&ang);
theta=((ang*3.14)/180);
x2a=x1a*cos(theta)-y1a*sin(theta);
y2a=x1a*sin(theta)+y1a*cos(theta);
x2b=x1b*cos(theta)-y1b*sin(theta);
y2b=x1b*sin(theta)+y1b*cos(theta);
x2c=x1c*cos(theta)-y1c*sin(theta);
y2c=x1c*sin(theta)+y1c*cos(theta);
printf("\n\n After Rotation ");
line(x2a,y2a,x2b,y2b);
line(x2b,y2b,x2c,y2c);
line(x2c,y2c,x2a,y2a);
delay(1);
printf("\n\n Next Operation is Scaling");
printf("\n\n Enter the Scale factor :");
scanf("%f %f",&sx,&sy);
x3a=x2a+sx;
y3a=y2a+sy;
x3b=x2b+sx;
y3b=y2b+sy;
x3c=x2c+sx;
y3c=y2c+sy;
printf("\n\n After Scaling ");
line(x3a,y3a,x3b,y3b);
line(x3b,y3b,x3c,y3c);
line(x3c,y3c,x3a,y3a);
31

delay(1);
printf("\n\n Next Operation is Shearing");
printf("\n\n Enter 1 for x-axis \n 2 for y-axis: ");
scanf("%d",&ch);
if(ch==1)
{
printf("\n\n Enter the x-SHEAR (^.^) Value: ");
scanf("%d",&shx);
}
else
{
printf("\n\n Enter the y-SHEAR (^.^) Value: ");
scanf("%d",&shy);
}
if(ch==1)
{
x3a=x3a+shx*y3a;
y4a=y3a;
x3b=x3a+shx*y3a;
y4b=y3b;
x3c=x3a+shx*y3a;
y4c=y3c;
}
else
{
x4a=x3a;
y3a=y3a+shy*x3a;
x4b=x3b;
y3b=y3b+shy*x3b;
x4c=x3c;
y3c=y3c+shy*x3c;
}
printf("\n\n After Shearing ");
line(x3a,y3a,x3b,y3b);
line(x3b,y3b,x3c,y3c);
line(x3c,y3c,x3a,y3a);
delay(1);
printf("\n\n Next Operation is Reflection");
t=abs(y3a-y3c);
x5a=x3a;
x5b=x3b;
x5c=x3c;
y5a=y3a+10+(2*t);
y5b=y3b+10;
y5c=y3c+10;
printf("\n\n After Reflection ");
32

line(x5a,y5a,x5b,y5b);
line(x5b,y5b,x5c,y5c);
line(x5c,y5c,x5a,y5a);
getch();
closegraph();
return 0;
}

Output:
2D Composite Transformations
Enter all coordinates values 213 236 253 321 256 214
The original Image

Enter the value translation vector 32
After Translation

Next Operation is Rotation
Enter the rotation angle 20
After Rotation

Next Operation is Scaling
Enter the scale factor 10 5

33

After Scaling

Next Operation is Shearing
Enter 0 for x axis and 1 for y axis 0
Enter the x-shear value 1
After Shearing

Enter 0 for x axis and 1 for y axis 1
Enter the y-shear value 1

Next Operation is Reflection
After Reflection


Result:
Thus the program was written and executed to implement the various composite 2D
transformations and the output was verified successfully.
34

Ex. No: 5 Implementation of Cohen-Sutherland 2D Line Clipping and windowing
Date:

Aim:
To write a C program to implement Cohen-Sutherland 2D line clipping Algorithm and
perform clipping.
Algorithm:
Step 1: Start the program.
Step 2: Include necessary header files for graphics programming.
Step 3: Declare the required variables.
Step 4: Initialize the variables to draw a window.
Step 5: Decide the position of the object on the Clipping window.
Step 6: Get the values of co-ordinates of both clipping window and the line.
Step 7: Invoke the functions required for clipping algorithm.
Step 8: Display the output.
Step 9: Stop the program.
Program:

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
typedef unsigned int outcode;
enum {TOP=0x1,BOTTOM=0x2,RIGHT=0x4,LEFT=0x8};
void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax)
float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
{
int gd,gm;
outcode code0,code1,codeout;
int accept=0,done=0;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1=calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
do
{
if(!(code0|code1))
{
accept=1;done=1;
35

}
Else if(code0&code1)
done=1;
else
{
float x,y;
codeout=code0?code0:code1;
if(codeout&TOP)
{
x=x0+(x1-x0)*(ywmax-y0)/(y1-y0);
y=ywmax;
}
Else if(codeout&BOTTOM)
{
x=x0+(x1-x0)*(ywmin-y0)/(y1-y0);
y=ywmin;
}
Else if (codeout&RIGHT)
{
y=y0+(y1-y0)*(xwmax-x0)/(x1-x0);
x=xwmax;
}
else
{
y=y0+(y1-y0)*(xwmin-x0)/(x1-x0);
x=xwmin;
}
if(codeout==code0)
{
x0=x;y0=y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
x1=x;y1=y;
code1=calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
}
}while(done==0);
if(accept)
line(x0,y0,x1,y1);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
}
int calcode(x,y,xwmin,ywmin,xwmax,ywmax)
float x,y,xwmin,ywmin,xwmax,ywmax;
36

{
int code=0;
if(y>ywmax)
code|=TOP;
else if(y<ywmin)
code|=BOTTOM;
else if(x>xwmax)
code|=RIGHT;
else if (x<xwmin)
code|=LEFT;
return(code);
}
void main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\tc\\bgi");
printf("\n\n\tEnter the co-ordinates of Line :");
printf("\n\n\tX1, Y1 : ");
scanf("%f %f",&x1,&y1);
printf("\n\n\tX2 ,Y2 : ");
scanf("%f %f",&x2,&y2);
printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);
clrscr();
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
clrscr();
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
closegraph();
}

Output:
Enter the co-ordinates of Line :
X1, Y1 =120 240
X2, Y2 =350 500
Enter the co_ordinates of window :

Xwmin, Ywmin= 200 200
37

Xwmax, Ywmax= 350 350
Before Clipping

After Clipping:






Result:
Thus the program was written and executed to implement the Cohen Sutherland 2D line
clipping and the output was verified successfully.



38

Ex. No: 6 Implementation of Sutherland-Hodgeman Polygon Clipping Algorithm
Date:

Aim:
To write a C program to implement Sutherland-Hodgeman Polygon Clipping Algorithm
and perform polygon clipping.
Algorithm:
Step 1: Start the program.
Step 2: Include necessary header files for graphics programming.
Step 3: Declare the required variables.
Step 4: Initialize the variables to draw a window.
Step 5: Decide the position of the object on the Clipping window.
Step 6: Get the values of co-ordinates of both clipping window and the polygon.
Step 7: Invoke the functions required for clipping algorithm.
Step 8: Display the output.
Step 9: Stop the program.
Program:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);
enum
{
TOP=0x1,BOTTOM=0x2,RIGHT=0x4,LEFT=0x8
};
float xmin,xmax,ymin,ymax;
void clip(float x0,float y0,float x1,float y1)
{
39

outcode outcode0,outcode1,outcodeOut;
int accept=FALSE,done=FALSE;
outcode0=CompOutCode(x0,y0);
outcode1=CompOutCode(x1,y1);
do
{
if(!(outcode0|outcode1))
{
accept=TRUE;
done=TRUE;
}
Else if(outcode0&outcode1)
done=TRUE;
else
{
float x,y;
outcodeOut=outcode0?outcode0:outcode1;
if(outcodeOut&TOP)
{
x=x0+(x1-x0)*(ymax-y0)/(y1-y0);
y=ymax;
}
Else if(outcodeOut&BOTTOM)
{
x=x0+(x1-x0)*(ymin-y0)/(y1-y0);
y=ymin;
}
Else if(outcodeOut&RIGHT)
{
y=y0+(y1-y0)*(xmax-x0)/(x1-x0);
x=xmax;
}
else
{
y=y0+(y1-y0)*(xmin-x0)/(x1-x0);
x=xmin;
}
if(outcodeOut==outcode0)
{
x0=x;
y0=y;
outcode0=CompOutCode(x0,y0);
}
else
{
x1=x;
40

y1=y;
outcode1=CompOutCode(x1,y1);
}
}
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(150,20,"POLYGON AFTER CLIPPING");
rectangle(xmin,ymin,xmax,ymax);
}
outcode CompOutCode(float x,float y)
{
outcode code=0;
if(y>ymax)
code|=TOP;
else if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else if(x<xmin)
code|=LEFT;
return code;
}
void main()
{
float x1,y1,x2,y2;
int gdriver=DETECT,gmode,n,poly[14],i;
clrscr( );
printf("Enter the no of sides of polygon:"); scanf("%d",&n);
printf("\nEnter the coordinates of polygon\n");
for(i=0;i<2*n;i++)
{
scanf("%d",&poly[i]);
}
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
outtextxy(150,20,"POLYGON BEFORE CLIPPING");
drawpoly(n+1,poly);
rectangle(xmin,ymin,xmax,ymax);
getch();
cleardevice();
for(i=0;i<n;i++)
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
41

getch();
restorecrtmode();
}

Output:
Enter the Sides of the Polygon: 5
Enter the co-ordinates of the Polygon 80 50 200 100 350 350 80
200 40 80
Enter the rectangular co-ordinates 150 150 300 300




Result:
Thus the program was written and executed to implement the Sutherland-Hodgeman
Polygon Clipping Algorithm and the output was verified successfully.
42

Ex. No: 7 Implementation of 3D Transformations
Date:

Aim:
To write a C program to implement three dimensional transformations.
Algorithm:
Step 1: Start the program.
Step 2: Include necessary header files for graphics programming.
Step 3: Declare the required variables.
Step 4: Incase of translation, get the values of translation factor x and y from the user.
Step 5: Set the attributes such as color, font and size of the actual figure
Step 6: Incase of rotation, get the value of rotation angle from the user.
Step 7: Perform rotation in all the three axis x, y, z.
Step 8: Incase of scaling, get the value of scaling factor from the user.
Step 9: Set the attributes of style as CLOSE-DOT-FILL and text as GOTHIC_FONT.
Step 10: Display the output accordingly.
Step 11: Stop the program.
Program:

Transformation.C

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main(void)
{
int gd=DETECT,gm,errorcode;
int l,t,r,b,tf,x,y,z,dp;
initgraph(&gd,&gm,"c:\\tc\\bgi");
clrscr();
setcolor(9);
setfillstyle(CLOSE_DOT_FILL,3);
settextstyle(GOTHIC_FONT,HORIZ_DIR,3);
43

outtextxy(200,10,"3D TRANSFORMATION");
bar3d(l=10,t=50,r=100,b=150,dp=15,tf=1);
getch();
cleardevice();
printf("\nEnter the x and y values : ");
scanf("%d%d",&x,&y);
setcolor(14);
outtextxy(150,150,"After Transformation");
setcolor(9);
bar3d(l=x+10,t=y+50,r+x+100,b+y+150,15,1);
getch();
setcolor(9);
setfillstyle(CLOSE_DOT_FILL,3);
bar3d(l=10,t=50,r=100,b=150,dp=15,tf=1);
getch();
}

Rotation.C

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int mx,my,mix,miy;
void axis()
{
getch();
cleardevice();
line(mix,0,mix,my);
line(0,miy,mx,miy);
}
void main()
{
int gd=DETECT,gm,x,y,z,ang,x1,x2,y1,y2,o;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setfillstyle(0,getmaxcolor());
mx=getmaxx();
my=getmaxy();
mix=mx/2;
miy=my/2;
axis();
bar3d(mix+50,miy-100,mix+60,miy-90,5,1);
printf("\nEnter the rotation angle : ");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
44

x2=60*sin(o*3.14/180)-90*cos(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
axis();
printf("\nAfter rotation about z-axis");
bar3d(mix+x1,miy-y1,mix+x2,miy-y2,5,1);
axis();
printf("\nAfter rotation about y-axis");
bar3d(mix+50,miy-x1,mix+60,miy-x2,5,1);
axis();
printf("\nAftet rotation about x-axis");
bar3d(mix+x1,miy-100,mix+x2,miy-90,5,1);
getch();
closegraph();
}

Scaling.C

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
void main(void)
{
int gd=DETECT,gm,errorcode, l,t,r,b,tf,x,y,z,dp;
initgraph(&gd,&gm,"c:\\tc\\bgi");
clrscr();
setcolor(9);
setfillstyle(CLOSE_DOT_FILL,3);
settextstyle(GOTHIC_FONT,HORIZ_DIR,3);
outtextxy(200,10,"3D TRANSFORMATION");
bar3d(l=10,t=50,r=100,b=150,dp=15,tf=1);
getch();
cleardevice();
printf("\n Enter the x ,y and z values : ");
scanf("%d%d%d",&x,&y,&z);
cleardevice();
setcolor(14);
outtextxy(200,10,"AFTER SCALING");
setcolor(9);
setfillstyle(CLOSE_DOT_FILL,3);
bar3d(l=10,t=50,r=x+100,b=y+150,dp=z+15,tf=1);
getch();
}



45

Output:
Translation
Enter Translation Factor :50 60 70
Before Translation

After Translation

Scaling
Enter Scaling Factor : 80 90 95

46

After Scaling:

Rotation
Enter Rotating Angle : 60

After Rotation about Z-Axis

47

After Rotation about X-Axis

After Rotation about Y-Axis :







Result:
Thus the program was written and executed to implement the various 3D transformations
and the output was verified successfully.

48

Ex. No: 8 Implementation of Composite 3D Transformation
Date:

Aim:
To write a C program to implement composite three dimensional transformations.
Algorithm:
Step 1: Start the program.
Step 2: Include necessary header files for graphics programming.
Step 3: Declare the required variables.
Step 4: Get the co-ordinates values from the user.
Step 5: Incase of translation, get the value of translation factor and draw the image.
Step 6: Incase of reflection, get the co-ordinates & calculate the reflection factor.
Step 7: Incase of rotation, get the rotation angle and draw the image.
Step 8: Incase of scaling, get the value of scaling factor and draw the image.
Step 9: Incase of shearing, choose either x-shear or y-shear and draw the corresponding image.
Step 10: Display the output accordingly.
Step 11: Stop the program.

Program:

#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
class cube
{
public:
void drawcube(int x1[],int y1[])
{
int i;
for(i=0;i<4;i++)
49

{
if(i<3)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
line(x1[0],y1[0],x1[3],y1[3]);
}
for(i=4;i<8;i++)
{
if(i<7)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
line(x1[4],y1[4],x1[7],y1[7]);
}
for(i=0;i<4;i++)
{
line(x1[i],y1[i],x1[i+4],y1[i+4]);
}
}
};
void main()
{
int i,x1[8],y1[8],x2[8],y2[8],z1[8],x3[8], y3[8],z3[8],x4[8],y4[8], theta,op,ch,tx,ty, tz,sx,
sy,sz,xf,yf,zf,x,y,z,size;
int driver=DETECT;
int mode;
initgraph(&driver,&mode,"");
cout<<"enter the points on the cube:";
cin>>x>>y>>z;
cout<<"enter the size of the edge:";
cin>>size;
x1[0]=x1[3]=x;
x1[1]=x1[2]=x+size;
x1[4]=x1[7]=x;
x1[5]=x1[6]=x+size;
y1[0]=y1[1]=y;
y1[2]=y1[3]=y+size;
y1[4]=y1[5]=y;
y1[6]=y1[7]=y+size;
z1[1]=z1[2]=z1[3]=z1[0]=z ;
z1[4]=z1[5]=z1[6]=z1[7]=z-size;
for(i=0;i<8;i++)
{
50

x2[i]=x1[i]+z1[i]/2;
y2[i]=y1[i]+z1[i]/2;
}
cube c;
getch();
cleardevice();
do
{
cout<<"MENU"<<endl;
cout<< "\n1.translation\n2.rotation\n3.scaling\n4.exit\n";
cout<<"enter the choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the translation vector:";
cin>>tx>>ty>>tz;
for(i=0;i<8;i++)
{
x3[i]=x1[i]+tx;
y3[i]=y1[i]+ty;
z3[i]=z1[i]+tz;
}
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before translation";
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<"after translation";
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 2:
cout<<"enter the rotation angle:";
51

cin>>theta;
theta=(theta*3.14)/180;
cout<<"enter the direction"<<endl;
cout<<"1.rotation about x axis"<<endl<<"2.rotation about y axis"
<<endl<<"3.rotation about z axis";
cin>>op;
if(op==1)
{
for(i=0;i<8;i++)
{
x3[i]=x1[i];
y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta);
z3[i]=y1[i]*sin(theta)+z1[i]*cos(theta);
}
}
else
if(op==2)
{
for(i=0;i<8;i++)
{
y3[i]=y1[i];
x3[i]=z1[i]*cos(theta)-x1[i]*sin(theta);
x3[i]=z1[i]*sin(theta)+x1[i]*cos(theta);
}
}
Else if(op==3)
{
for(i=0;i<8;i++)
{
z3[i]=z1[i];
x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta);
y3[i]=x1[i]*sin(theta)+y1[i]*cos(theta);
}
}
else
cout<<"enter correct option";
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
52

}
cleardevice();
cout<<"before rotation";
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<"after rotation";
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 3:
cout<<"enter the scaling factor:";
cin>>sx>>sy>>sz;
cout<<"enter the reference point:";
cin>>xf>>yf>>zf;
for(i=0;i<8;i++)
{
x3[i]=xf+(x1[i]*sx)+xf*(1-sx);
y3[i]=yf+(y1[i]*sy)+yf*(1-sy);
z3[i]=zf+(z1[i]*sz)+zf*(1-sz);
}
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before scaling";
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<"after scaling";
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 4:
exit(0);
break;
53

}
}while(op!=4);
getch();
}

Output:
Enter the points in the cube : 100 100 100
Enter the Size of Edge : 50
MENU
1.translation
2.rotation
3.scaling
4.exit
Enter your choice : 1
Enter the Translation Vector 5 10 15



Enter your Choice : 2
Rotation
Enter the Rotation Angle : 60
Enter the Direction
1.Rotation about x-axis
2.Rotation about y-axis
3.Rotation about z-axis

54



55



MENU
1.Translation
2.Rotation
3.Scaling
4.exit
Enter your choice : 3
Enter the Scaling Factor : 30 40 50
Enter the Reference point : 20 35 45
Before scaling


56
















Result:
Thus the program was written and executed to implement the various composite 2D
transformations and the output was verified successfully.
57

Ex. No: 9.a) Drawing three dimensional objects
Date:

Aim:
To write a C program to draw three dimensional objects.
Algorithm:
Step 1: Start the program.
Step 2: Include necessary header files for graphics programming.
Step 3: Declare the required variables.
Step 4: Get the co-ordinates values from the user for a three dimensional object.
Step 5: Display the front view of the image separately.
Step 6: Display the top view of the three dimensional image.
Step 7: Display the side view of the 3D object.
Step 8: Clear the screen each time after viewing the image.
Step 9: Stop the program.

Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
int gd,gm;
int tlx,tly,brx,bry,d;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\tc\\bgi");
printf("Enter the input for 3D object");
scanf("%d %d %d %d %d",&tlx,&tly,&brx,&bry,&d);
bar3d(tlx,tly,brx,bry,d,1);
sleep(3);
getch();
cleardevice();
printf("Front View");
58

bar3d(tlx,tly,brx,bry,0,0);
sleep(3);
//getch();
cleardevice();
printf("Top view");
bar3d(tlx,tly-d,brx,tly,0,0);
sleep(3);
//getch();
cleardevice();
printf("Side View");
bar3d(brx,tly,brx+d,bry,0,0);
getch();
closegraph();
}

Output:

Enter the input for 3D object: 45 50 70 60 65

Front View: Top View:





Side View:






Result:
Thus the program was written and executed to draw the 3D objects and the output was
verified successfully.
59

Ex. No: 9.b) Drawing three dimensional scenes
Date:

Aim:
To write a C program to draw three dimensional scenes.
Algorithm:
Step 1: Start the program.
Step 2: Include necessary header files for graphics programming.
Step 3: Declare the required variables.
Step 4: Get the co-ordinates values from the user for a three dimensional object.
Step 5: Display the front view of the image separately.
Step 6: Display the top view of the three dimensional image.
Step 7: Display the side view of the 3D object.
Step 8: Clear the screen each time after viewing the image.
Step 9: Stop the program.

Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int x1=200,y1=200,x2=400,y2=200,x3=400,y3=400,x4=200,y4=400;
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
printf("\n 3D Scenes \n");
rectangle(x1,y1,x3,y3);
rectangle(x1+50,y1-50,x3+50,y3-50);
line(x1,y1,x1+50,y1-50);
line(x2,y2,x2+50,y2-50);
line(x3,y3,x3+50,y3-50);
line(x4,y4,x4+50,y4-50);
printf("\n press any key to see top view");
getch();
60

cleardevice();
printf("\n top view\n");
rectangle(x1,y1,x2+50,y2-50);
printf("\n press any key to see side view");
getch();
cleardevice();
printf("\n side view\n");
rectangle(x2,y2,x3+50,y3-50);
printf("\n press any key to see front view");
getch();
cleardevice();
printf("\n front view \n");
rectangle(x1,y1,x3,y3);
getch();
}

Output:

3D Scenes


press any key to see top view
top view






press any key to see side view
side view





61


press any key to see front view
front view



































Result:
Thus the program was written and executed to draw the 3D scenes and the output was
verified successfully.



62

Ex. No: 10 Generating Fractal Images
Date:

Aim:
To write a C program to generate the fractal images such as sierpinski .
Algorithm:
Step 1: Start the program.
Step 2: Include necessary header files for graphics programming.
Step 3: Declare the required variables.
Step 4: The sierpinski Triangle is created by infinite removals

Step 5: Each triangle is divided into 4 smaller upside down triangles.

Step 6: The center of the 4 triangles is removed

Step 7: As the process is iterated infinite number of times, the total area of the set goes to
infinity as the size of the each new triangle goes to zero

Step 8: After closer examination magnification factor is 2.

Step 9: With each magnification there are 3 divisions of a triangle
Dimension D=ln(3)/ln(2)
D=1.5850

Step 10: Display the corresponding results.
Step 11: Stop the program.
Program:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <graphics.h>
void DrawSierpinski(void);
void main(void)
{
int gd=VGA;
int gm=VGAHI;
initgraph(&gd, &gm, "\\tc\\bgi");
63

DrawSierpinski();
getch();
}
void DrawSierpinski(void)
{
char Direct;
int iterate;
unsigned int x1, y1, x2, y2;
x1 = x2 = 320; y1 = y2 = 0;
for(iterate = 0; iterate < 10000; iterate++)
{
Direct = random(3);
if(Direct == 0)
{
x1 = (x2 + 320) / 2;
y1 = (y2 + 0) / 2;
}
else if(Direct == 1)
{
x1 = (x2 + 0) / 2;
y1 = (y2 + 480) / 2;
}
else if(Direct == 2)
{
x1 = (x2 + 640) / 2;
y1 = (y2 + 480) / 2;
}
putpixel(x1, y1, WHITE);
x2 = x1;y2 = y1;
}
}

Output:


Result:
Thus the program was written and executed to generate the fractal images and the output
was verified successfully.

Das könnte Ihnen auch gefallen