Sei sulla pagina 1di 68

CS2405

COMPUTER GRAPHICS LABORATORY

LTPC
0032

1. Implementation of Bresenhams Algorithm Line, Circle, Ellipse.


2. Implementation of Line, Circle and ellipse Attributes
3. Two Dimensional transformations - Translation, Rotation, Scaling, Reflection,
Shear.
4. Composite 2D Transformations
5. Cohen Sutherland 2D line clipping and Windowing
6. Sutherland Hodgeman Polygon clipping Algorithm
7. Three dimensional transformations - Translation, Rotation, Scaling
8. Composite 3D transformations
9. Drawing three dimensional objects and Scenes
10. Generating Fractal images

TOTAL : 60 PERIODS
BASIC GRAPHICS FUNCTION

1) Initgraph ()
initgraph() function initializes the graphics mode and clears the screen.
Declaration:
void far initgraph(int far *driver, int far *mode, char far *path)
2) Detectgraph ()
Detectgraph function determines the graphics hardware in the system, if the function finds a
graphics adapter then it returns the highest graphics mode that the adapter supports.
Declaration:
void far detectgraph(int far *driver, int far *mode)
Integer that specifies the graphics driver to be used. You can give graphdriver a value using a constant
of the graphics_drivers enumeration type.
3) Closegraph ()

closegraph() function switches back the screen from graphcs mode to text mode. It clears the
screen also.
A graphics program should have a closegraph function at the end of graphics. Otherwise DOS
screen will not go to text mode after running the program.
4) Getpixel ()
getpixel function returns the color of pixel present at location(x, y).
Declaration:int getpixel(int x, int y);
5) Putpixel ()
putpixel function plots a pixel at location (x, y) of specified color.
Declaration:void putpixel(int x, int y, int color);
For example if we want to draw a GREEN color pixel at (35, 45) then we will write
putpixel(35, 35, GREEN); in our c program, putpixel function can be used to draw circles, lines
and ellipses using various algorithms.
6) line()
line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2)
are end points of the line.
Declaration :void line(int x1, int y1, int x2, int y2);
7) lineto()
lineto function draws a line from current position(CP) to the point(x,y), you can get current
position using getx and gety function.
8) circle()
circle function is used to draw a circle with center (x,y) and third parameter specifies the radius
of the circle.
Declaration :void circle(int x, int y, int radius);
9)ellipse()
Ellipse is used to draw an ellipse (x,y) are coordinates of center of the ellipse, stangle is the
starting angle, end angle is the ending angle, and fifth and sixth parameters specifies the X and Y radius
of the ellipse. To draw a complete ellipse strangles and end angle should be 0 and 360 respectively.

Declaration :void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);
10) drawpoly()
drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc.
Declaration :void drawpoly( int num, int *polypoints );
num indicates (n+1) number of points where n is the number of vertices in a polygon,
polypoints points to a sequence of (n*2) integers . Each pair of integers gives x and y coordinates of a
point on the polygon. We specify (n+1) points as first point coordinates should be equal to (n+1) th to
draw a complete figure.
To understand more clearly we will draw a triangle using drawpoly, consider for example the
array :int points[] = { 320, 150, 420, 300, 250, 300, 320, 150};
points array contains coordinates of triangle which are (320, 150), (420, 300) and (250, 300).
Note that last point(320, 150) in array is same as first.
11) outtext ()
outtext function displays text at current position.
Declaration :void outtext(char *string);
12) outtextxy ()
outtextxy function display text or string at a specified point(x,y) on the screen.
Declaration :void outtextxy(int x, int y, char *string);
x, y are coordinates of the point and third argument contains the address of string to be
displayed.
13)rectangle()
Rectangle function is used to draw a rectangle. Coordinates of left top and right bottom corner
are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Ycoordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies
the Y-coordinate of right bottom corner.
Declaration :void rectangle(int left, int top, int right, int bottom);
14) floodfill()

floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill
the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled otherwise
outside will be filled,border specifies the color of boundary of area.
Declaration :void floodfill(int x, int y, int border);
15)fillpoly()
f illpoly function draws and fills a polygon. It require same arguments as drawpoly.
Declaration :void drawpoly( int num, int *polypoints );
16)fillellipse()
f illellipse function draws and fills a polygon.
Declaration:void fillellipse(int x, int y, int xradius, int yradius);
x and y are coordinates of center of the ellipse, xradius and yradius are x and y radius of ellipse
respectively.

EX: NO: 1a

BRESENHMANS LINE DRAWING ALGORITHM

AIM:
To draw a line using Bresenhmans line drawing algorithm.
ALGORITHM:
1. Read the starting and ending co-ordinates.
2 .Calculate the distance between end points.
3. Find the starting and ending locations.
4. While starting location is less than the ending location, do the following
a. Calculate x & y co ordinates
b.Plot the pixels.
5. Display the line.
PROGRAM:
#include<stdio.h>
#include<conio.h>

#include<graphics.h>
#include<stdlib.h>
void main()
{
int xa,xb,ya,yb,dx,dy,xend,x,y,p,gd=DETECT,gmode;
clrscr();
initgraph(&gd,&gmode," ");
printf("\n\t Bresenhams line drawing algorithm");
printf("\n\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\n\tEnter the starting points:");
scanf("%d%d",&xa,&ya);
printf("\n\n\tEnter the end points:");
scanf("%d%d",&xb,&yb);
dx=abs(xa-xb);
dy=abs(ya-yb);
if(xa>xb)
x=xb,y=yb,xend=xa;
else
x=xa,y=yb,xend=xb;
putpixel(x,y,7);
while(x<xend)
{
x+=1;
if(p<0)
p=p+2*dy;
else
y+=1,p=p+2*(dy-dx);
putpixel(x,y,7);

}
getch();
closegraph();
}
OUTPUT:
Enter the starting point
200 300
Enter the End point

250 350

RESULT:
Thus the program to display a line using Bresenhams algorithm was executed.

EX.NO:1b

BRESENHAMS CIRCLE ALGORITHM

AIM:
To draw a circle using Bresenhams circle algorithm.
ALGORITHM:
1. Read the center and radius of the circle.
2. Shift the center to origin; consider the points on y-axis at a distance of radius.
3. Plot the pixel at the center of circle.
4. While x-co ordinate is less than the radius, do the following
a. Increment x by one and calculate y-co ordinate

b. Shift the co ordinate to original position.


c. Plot the pixel.
5. Display the circle.

PROGRAM:
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int x,y;
void plotpoints(int,int);
void main()
{
int p,x1,y1,r;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode," ");
clearviewport();
printf("\n\n\tCIRCLE ALGORITHM");
printf("\n\t\t*****************\n");
printf("\n\tEnter x co-ordinates:");
scanf("%d",&x1);
printf("\n\tEnter y co-ordinates:");
scanf("%d",&y1);
printf("\n\tEnter R-radius:");
scanf("%d",&r);
x=0;
y=r;
plotpoints(x1,y1);
p=1-r;

while(x<y)
{
if(p<0)
x++;
else
{
x++;
y--;
}
if(p<0)
p+=2*x+1;
else
p+=2*(x-y)+1;
plotpoints(x1,y1);
}
getch();

}
void plotpoints(int x1,int y1)
{
putpixel(x1+x,y1+y,2);
putpixel(x1-x,y1+y,2);
putpixel(x1+x,y1-y,2);
putpixel(x1-x,y1-y,2);
putpixel(x1+y,y1+x,2);
putpixel(x1-y,y1+x,2);
putpixel(x1+y,y1-x,2);
putpixel(x1-y,y1-x,2);
delay(30);
}

OUTPUT:
Enter the x co-ordinates 200
Enter the y co-ordinates 300
Enter R-Radius 50

RESULT:

Thus the program to display a circle using Bresenhams algorithm was executed.

EX.NO:1c

BRESENHAMS ELLIPSE ALGORITHMS

AIM:
To draw an ellipse using Bresenhams ellipse algorithm.
ALGORITHM:
1. Read the center and radius of x &y axis.
2. Obtain the first position on an ellipse centered on the origin.
3. Calculate the initial value of decision parameter.
4. At each position calculate the next point along ellipse.
5. Determine the symmetric points in other three quadrants and plot them.
6. Display the ellipse.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int i,gd=DETECT,gm,x1=0,y1=0,r1=0,r2=0;
float x=0,y=0,t,d;
initgraph(&gd,&gm,"f:\\");
setbkcolor(BLACK);
setcolor(YELLOW);

fflush(stdin);
printf("enter the center:");
scanf("%d%d",&x1,&y1);
printf("enter the radius 1 and 2:");
scanf("%d%d",&r1,&r2);
for(i=0;i<360;i++)
{
t=3.141/180;
d=i*t;
x=x1+ceil(r1*sin(d));
y=y1+ceil(r2*cos(d));
delay(20);
putpixel(x,y,15);
}
getch();
closegraph();
}

OUTPUT:

RESULT:
Thus the program to draw an ellipse using Bresenhams algorithm was executed.

Ex. No. 2 : IMPLEMENTATION OF LINE, CIRCLE AND ELLIPSE ATTRIBUTES

Aim:
To write a C Program to display the line, circle and ellipse attributes.

Algorithm:
1. Start the program .
2. Initialize the variables.
3. Call the initgraph() function
4. Set color for the output primitives.
5. Using Outtextxy() display the choosen particular primitives.
6. Using switch case mention the various primitives and their attributes.
7. The various primitives are arc, line ,circle, rectangle and ellipse.
8. close the graph and run the program.
9. stop the program.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
void main()
{
char ch='y';
int gd=DETECT,gm,x1,y1,x2,y2,rad,sa,ea,xrad,yrad,i;
initgraph(&gd,&gm,"");

while(ch=='y')
{
cleardevice();
setbkcolor(9);
outtextxy(100,150,"Enter 1 to get line");
outtextxy(100,170,"2.Circle");
outtextxy(100,190,"3.Box");
outtextxy(100,210,"4.Arc");
outtextxy(100,230,"5.Ellipse");
outtextxy(100,250,"6.Rectangle");
outtextxy(100,270,"7.Exit");
ch=getch();
cleardevice();
switch(ch)
{
case '1':
line(100,200,300,400);
break;
case '2':
circle(200,200,100);
break;
case '3':
setfillstyle(5,4);
bar(100,300,200,100);
break;
case '4':
setfillstyle(5,4);
arc(200,200,100,300,100);

break;
case '5':
setfillstyle(5,4);
fillellipse(100,100,50,100);
break;
case '6':
settextstyle(DEFAULT_FONT,0,2);
outtextxy(120,140,"VEL TECH");
line(100,100,100,300);
line(300,300,100,300);
line(100,100,300,100);
line(300,100,300,300);
break;
case '7':
closegraph();
return;
}
ch='y';
getch();
}
}
Output:

Result:
Thus the program to display the line, circle and ellipse attributes was executed successfully.

EX:NO:3a

TWO DIMENSIONAL TRANSFORMATIONS

AIM:
To implement the various 2D transformations like translation, scaling and rotation.
ALGORITHM:

To draw polygon
1. Read the number of vertices.
2. Read the x & y co ordinate of the vertices and store it in an array.
3. Draw the polygon using drawpoly()
Translation

It is applied to an object by repositioning it along a straight line path from one co ordinate to
another.
1. Read the translation distance tx & ty.
2. Add tx & ty to the co ordinates to move to the new position.
3. Display the polygon.

Scaling
It alters the size of an object, by multiplying the co ordinate values of each vertex by scaling factors.

1. Read the scaling factor sx.


2. Multiply the scaling sx with each co ordinate vertex to alter the size.
3. Display the polygon.
Rotation
It is applied to an object by repositioning it along a circular path in the xy plane
1. Read the rotation factor or pivot point (a) and distance(xr,yr) from origin.
2. Polygon is rotated by displacing each vertex through the specified rotation angle (a).
3. Display the polygon.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#define pi 3.148
int gd,gm;
void box(int x1,int y1,int x2,int y2)
{
char start[20],end[20];
line(x1,y1,x2,y1);
line(x2,y1,x2,y2);
line(x2,y2,x1,y2);
line(x1,y2,x1,y1);
}
void main()
{
int x1,x2,y1,y2,x3,y3,tx,ty,i,j,theta,ch,cho;

int a,b,c,d,e,f,x,y;
float sx,sy,m,n;
clrscr();
do
{
printf("\n\n\t\t sample i/p and o/p");
printf("\n\t Enter the value of x1,y1");
scanf("%d%d",&x1,&y1);
printf("\n\t Enter the value of x2,y2");
scanf("%d%d",&x2,&y2 );printf("\n\t1.Translation");

printf("\n\t2.Rotation");
printf("\n\t3.scaling");
printf("\n\t4.Exit");
printf("\n\t Enter ur choice");
scanf("%d",&ch);
detectgraph(&gd,&gm);
initgraph(&gd,&gm," ");
switch(ch)
{
case 1:
setcolor(5);
box(x1,y1,x2,y2);
printf("Enter the hori and verti translation");
scanf("%d%d",&tx,&ty);
setcolor(35);
box(x1+tx,y1+ty,x2+tx,y2+ty);
getch();
break;
case 2:
box(x1,y1,x2,y2);
printf("Enter the rotating angle");
scanf("%d",&theta);
m=(pi/180)*theta;

n=0.01;
do
{
cleardevice();
setcolor(5);
box(x1,y1,x2,y2);
x=x1,y=y2,a=x2,b=y2,c=x2,d=y1,e=x1,f=y1;
a=x+floor(x2-x1)*cos(n);
b=y+floor(y2-y1)*sin(n);
c=x+floor(x2-x1)*sqrt(2)*cos(n-(pi/4));
d=y+floor(y2-y1)*sqrt(2)*sin(n-(pi/4));
e=x+floor(x2-x1)*cos(n-(pi/2));
f=y+floor(y2-y1)*sin(n-(pi/2));
setcolor(35);
line(x1,y2,a,b);
line(a,b,c,d);
line(c,d,e,f);
line(e,f,x1,y2);
n+=0.01;
delay(50);
}
while(n<m);
getch();
break;

case 3:
cleardevice();
setcolor(5);
box(x1,y1,x2,y2);
printf("Enter the scaling factor");
scanf("%f%f",&sx,&sy);
cleardevice();
setcolor(5);
box(x1,y1,x2,y2);
x3=(x2-x1)*(sx)+x1;
y3=(y2-y1)*(sy)+y1;
setcolor(35);
box(x1,y1,x3,y3);
getch();
break;
default:
break;
}
printf("Do u want to continue");
scanf("%s",&cho);
}
while(cho=='y' || cho=='y');
closegraph();
}
OUTPUT:
Enter the value of x1,y1: 120 200
Enter the value of x2,y2: 300 250

1.Translation
2.Rotation
3.Scaling
4.Exit
Enter your choice:1
Enter the hori. and veri translation

1.Translation
2.Rotation
3.Scaling
4.Exit
Enter your choice:2
Enter the rotating angle 180

1.Translation
2.Rotation

50

60

3.Scaling
4.Exit
Enter your choice:3
Enter the scaling factor 2 4

1.Translation
2.Rotation
3.Scaling
4.Exit
Enter your choice:4

RESULT:
Thus the program to implement various 2D transformation like translation, scaling and rotation
was executed.

EX:NO: 3b

TWO DIMENSIONAL REFLECTION

AIM:
To implement Two Dimensional reflection.
ALGORITHM:
To draw polygon
1. Read the number of vertices.
2. Read the x & y co ordinate of the vertices and store it in an array.
3. Draw the polygon using drawpoly()
Reflection

A reflection is a transformation that produces a mirror image of an object.

1. Reflection of an object is produces by displacing the object along x axis & y axis being the
name.
2. Display the polygon.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void triangle(int x1,int y1,int x2,int y2,int x3,int y3)
{
char start[30],mid[50],end[30];
sprintf(start,"(%d%d)",x2,y2);
outtextxy(x1-5,y1-5,start);
sprintf(mid,"(%d%d)",x2,y2);
outtextxy(x2-5,y2-5,mid);
sprintf(end,"(%d%d)",x3,y3);
outtextxy(x3-5,y3-5,end);
setcolor(15);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);

}
void main()
{
int x1,y1,x2,y2,x3,y3,gd=DETECT,gm,k;
initgraph(&gd,&gm," ");
printf("\n\t Enter the co-ordinates");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
loop:
cleardevice();
printf("\n Main menu");
printf("\n 1.Creation of triangle");
printf("\n 2.X-axis reflection");
printf("\n 3.Y-axis reflection");
printf("\n 4.Exit");
printf("\n Enter ur choice");
scanf("%d",&k);
switch(k)
{
case 1:
triangle(x1,y1,x2,y2,x3,y3);
getch();
goto loop;
case 2:
line(0,200,630,200);

triangle(x1,y1,x2,y2,x3,y3);
getch();
triangle(x1,(200-y1)*2+y1,x2,(200-y2)*2+y2,x3,(200-y3)*2+y3);
getch();
goto loop;
case 3:
line(300,0,300,475);
triangle(x1,y1,x2,y2,x3,y3);
getch();
triangle((300-x1)*2+x1,y1,(300-x2)*2+x2,y2,(300-x3)*2+x3,y3);
getch();
goto loop;
case 4:
break;
}
}

OUTPUT OF PROGRAM FOR REFLECTION

Enter the co-ordinates: 200 200 300 100 300 200


Main menu
1.Creation of triangle

2.x-axis reflection
3.y-axis reflection
4.Exit
Enter your choice 1

(300,100)

(200,200)

(300,200)

Main menu
1.Creation of triangle
2.x-axis reflection
3.y-axis reflection
4.Exit
Enter your choice 2

(300100)

(200,200)

(300,200)

(300,300)

Main menu
1.Creation of triangle
2.x-axis reflection
3.y-axis reflection
4.Exit
Enter your choice 3

(300,100)

(200,200)

(400,200)

RESULT:
Thus the program to implement Two Dimensional reflection was executed.

EX:NO:3c

TWO DIMENSIONAL SHEARING

AIM:
To implement Two Dimensional shearing.
ALGORITHM:
To draw polygon
1. Read the number of vertices.
2. Read the x & y co ordinate of the vertices and store it in an array.
3. Draw the polygon using drawpoly()
Shearing
A transformation that distorts the shape of an object
1. Read the shearing factor (sh).
2. Shearing an object relative to x axis produced by an expression.
X1=x + sh*y
Y1=y
3.Display the polygon.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int i,j,sh,n,x[50],y[50],sx[50],sy[50];
initgraph(&gd,&gm," ");

cleardevice();
for(i=0;i<4;i++)
{
printf("Enter the %d value \n",i+1);
scanf("%d%d",&x[i],&y[i]);
sy[i]=y[i];
sx[i]=x[i];
}
for(i=0;i<3;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[i],y[i],x[0],y[0]);
printf("\n 1.x-shearing\n 2.y-shearing\n 3.Exit");
printf("\n Enter ur choice:");
scanf("%d",&n);
printf("Enter the shearing factor:");
scanf("%d",&sh);
if(n==3)
exit(0);
switch(n)
{
case 1:
for(i=0;i<4;i++)
{

sx[i]=x[i]+(sh*y[i]);
sy[i]=y[i];
}
break;
case 2:
for(i=0;i<4;i++)
{
sx[i]=x[i];
sy[i]=(x[i]*sh)+y[i];
}
break;
}
for(i=0;i<3;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[i],y[i],x[0],y[0]);
for(i=0;i<3;i++)
line(sx[i],sy[i],sx[i+1],sy[i+1]);
line(sx[i],sy[i],sx[0],sy[0]);
getch();
closegraph();
}
OUTPUT OF PROGRAM FOR SHEARING

Enter the 1 value


100 100
Enter the 2 value
0 100
Enter the 3 value
00
Enter the 4 value
100 0
1.x-shearing

2.y shearing

Enter ur choice 1
Enter the shearing factor :2

1.x-shearing

3.Exit

2.y shearing
3.Exit
Enter ur choice 2
Enter the shearing factor :2

RESULT:
Thus the program to implement Two Dimensional shearing was executed.

Ex.No: 4

COMPOSITE 2D TRANSFORMATIONS

Aim:
To write a C program to perform composite 2D transformation.
Algorithm:
1. Start the program
2. Declare the variables and initialise the graphics driver.
3. Get the translation factors, scaling factors, and angle of rotations form the user.

4. Call the input() function to draw the polygon.


5. Then perform the composite transformations.
6. Display the corresponding outputs by calling the output().
7. Stop the driver.
8. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int n,i,a[20][2],fx,fy,y,tx,ty,temp;
float sx,sy,k;
void input()
{
printf("enter the no of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the co-ordinates:");
scanf("%d%d",&a[i][0],&a[i][1]);
}
}
void output()
{
cleardevice();
for(i=0;i<n-1;i++)

{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
line(a[i][0],a[i][1],a[0][0],a[0][1]);
}
void rotation()
{
output();
printf("enter the rotating angle:");
scanf("%d",&y);
printf("enter the pivot point:");
scanf("%d%d",&fx,&fy);
k=(y*3.14)/180;
for(i=0;i<=n;i++)
{
temp=a[i][0]-a[i][1]*sin(k);
a[i][1]=a[i][0]*sin(k)+a[i][1];
a[i][0]=temp;
}
output();
}
void scaling()
{
output();
printf("enter the scaling factor sx,sy:");
scanf("%f%f",&sx,&sy);

printf("enter the fixed point:");


scanf("%d%d",&fx,&fy);
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]*sx+fy*(1-sx);
a[i][1]=a[i][1]*sy+fy*(1-sy);
}
rotation();
}
void composite()
{
output();
printf("enter the transformation vertex tx,ty:");
scanf("%d%d",&tx,&ty);
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]+tx;
a[i][1]=a[i][1]+ty;
}
scaling();
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"Z:\\BGI\\");
input();

composite();
getch();
}
Output:
enter the no of vertices:4
enter the co-ordinates:50 50
enter the co-ordinates:50 100
enter the co-ordinates:100 100
enter the co-ordinates:100 50

Enter the traslation vertex: 100 100


Ente the scaling factor:0.5 0.5

Enter the fixed point: 100 100

Enter the rotating angle: 30


Enter the pivot point: 100 100

Enter the rotating angle: 30


Enter the pivot point: 100 100

Result:
Thus the program in C to perform composite 2D transformation was done successfully.

Ex. No. 5 : Cohen Sutherland 2D line clipping and Windowing.

Aim : To study and Implement Cohen Sutherland 2D line clipping and Windowing

Algorithm

1.

Start the program

3.

Declare the variables and initialise the graphics driver.

4.

Get the Window coordinates and end coordinates of the line form the user.

5.

Call the clip function to find the points that are to be clipped by the window.

6.

Then perform the transformations.

7.

Display the corresponding output of clipped line.

8.

Stop the driver.


9.

Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm;
float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;
float start[4],end[4],code[4];
clrscr();
initgraph(&gd,&gm,"");
printf("\n\tEnter the bottom-left coordinate of viewport: ");
scanf("%f %f",&xmin,&ymin);
printf("\n\tEnter the top-right coordinate of viewport: ");
scanf("%f %f",&xmax,&ymax);
printf("\n\tEnter the coordinates for starting point of line: ");
scanf("%f %f",&x1,&y1);printf("\n\tEnter the coordinates for ending point of line: ");

scanf("%f %f",&x2,&y2);
for(i=0;i<4;i++)
{
start[i]=0;
end[i]=0;
}
m=(y2-y1)/(x2-x1);
if(x1<xmin)
start[0]=1;
if(x1>xmax)
start[1]=1;
if(y1>ymax)
start[2]=1;
if(y1<ymin)
start[3]=1;
if(x2<xmin)
end[0]=1;
if(x2>xmax)
end[1]=1;
if(y2>ymax)
end[2]=1;
if(y2<ymin)
end[3]=1;
for(i=0;i<4;i++)
code[i]=start[i]&&end[i];
if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0))

{
if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&(end[0]==0)&&(end[1]==0)&&(
end[2]==0)&&(end[3]==0))
{
cleardevice();
printf("\n\t\tThe line is totally visible\n\t\tand not a clipping candidate");
rectangle(xmin,ymin,xmax,ymax);line(x1,y1,x2,y2);
getch();
}
else
{
cleardevice();
printf("\n\t\t\t\tLine is partially visible");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);getch();
if((start[2]==0)&&(start[3]==1)){x1=x1+(ymin-y1)/m;y1=ymin;}if((end[2]==0)&&(end[3]==1))
{
x2=x2+(ymin-y2)/m;
y2=ymin;

if((start[2]==1)&&(start[3]==0))
{
x1=x1+(ymax-y1)/m;
y1=ymax;

}
if((end[2]==1)&&(end[3]==0))

{
x2=x2+(ymax-y2)/m;
y2=ymax;
}
if((start[1]==0)&&(start[0]==1))
{
y1=y1+m*(xmin-x1);
x1=xmin;
}
if((end[1]==0)&&(end[0]==1))
{
y2=y2+m*(xmin-x2);
x2=xmin;
}
if((start[1]==1)&&(start[0]==0))
{
y1=y1+m*(xmax-x1);
x1=xmax;
}
if((end[1]==1)&&(end[0]==0))
{

y2=y2+m*(xmax-x2);

x2=xmax;
}
clrscr();
cleardevice();
printf("\n\t\tAfter clippling:");

rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
}
else
{
clrscr();
cleardevice();
printf("\nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
}getch();
closegraph();
}

OUTPUT:
Enter the bottom-left coordinate of viewport: 50 50
Enter the top-right coordinate of viewport:200 200
Enter the coordinates for starting point of line:25 25
Enter the coordinates for ending point of line:220 220
BEFORE CLIPPING

AFTER CLIPPING

RESULT:
Thus the c program to implement Cohen Sutherland 2D line clipping and Windowing was
executed successfully.

Ex. No. 6:

SUTHERLAND - HODGEMAN POLYGON CLIPPING ALGORITHM

Aim:
To write a C program to implement Sutherland Hodgeman polygon clipping algorithm.

Algorithm:
1. Start the program
2. Declare the variables and initialise the graphics driver.
3. Get the Window coordinates and end coordinates of the polygon form the user.
4. Call the clip function to find the points that are to be clipped by the window.
5. Then perform the transformations.
6. Display the corresponding output of clipped sides of the polygon.
7. Stop the driver.
8. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
int k;
float xmin,ymin,xmax,ymax,arr[20],m;
void clipl(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;

if(x1>=xmin && x2>=xmin)


{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1<xmin && x2>=xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1>=xmin && x2<xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
}
}
void clipt(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
else

m=100000;
if(y1<=ymax && y2<=ymax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1>ymax && y2<=ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1<=ymax && y2>ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
}
}
void clipr(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);

else
m=100000;
if(x1<=xmax && x2<=xmax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1>xmax && x2<=xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1<=xmax && x2>xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
}
}
void clipb(float x1,float y1,float x2,float y2)
{
if(y2-y1)

m=(x2-x1)/(y2-y1);
else
m=100000;
if(y1>=ymin && y2>=ymin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1<ymin && y2>=ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1>=ymin && y2<ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
}
}
void main()
{

int gdriver=DETECT,gmode,n,poly[20],i;
float xi,yi,xf,yf,polyy[20];
clrscr();
printf("Coordinates of rectangular clip window :\nxmin,ymin :");
scanf("%f%f",&xmin,&ymin);
printf("xmax,ymax :");
scanf("%f%f",&xmax,&ymax);
printf("\n\nPolygon to be clipped :\nNumber of sides :");
scanf("%d",&n);
printf("Enter the coordinates :");
for(i=0;i<2*n;i++)
scanf("%f",&polyy[i]);
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
for(i=0;i<2*n+2;i++)
poly[i]=round(polyy[i]);
initgraph(&gdriver,&gmode,"Z:\\BGI\\");
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
printf("\t\tUNCLIPPED POLYGON");
setcolor(WHITE);
fillpoly(n,poly);
getch();
cleardevice();
k=0;
for(i=0;i<2*n;i+=2)

clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
for(i=0;i<k;i++)

poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
printf("\tCLIPPED POLYGON");
getch();
closegraph();
}
Output:
Co-ordinates of rectangular clip window:
Xmin,Ymin:20 20
Xmax,Ymax: 100 100
Polygon to be clipped:
Enter the co-ordinates:
UNCLIPPED POLYGON:

CLIPPED POLYGON:

Result:
Thus the C program to implement Sutherland Hodgeman polygon clipping algorithm was done
successfully.

EX:NO:7

THREE DIMENSIONAL TRANSFORMATION

AIM:
To implement the

various 3D transformations like translation, scaling, rotation.

ALGORITHM:
To draw polygon
1. Read the number of vertices.
2. Read the x & y co ordinate of the vertices and store it in an array.
3. Draw the polygon using drawpoly().
Translation

It is applied to an object by repositioning it along a straight line path from one co ordinate to
another.
1. Read the translation distance tx & ty.
2. Add tx & ty to the co ordinates to move to the new position.
3. Display the polygon.
Scaling

It alters the size of an object by multiplying the co ordinate values of each vertex by scaling
factors.
1. Read the scaling factor sx.
2. Multiply the scaling sx with each co ordinate vertex to alter the size.
3. Display the polygon.
Rotation

1.
2.
3.

It is applied to an object by repositioning it along a circular path in the xy plane


Read the rotation factor or pivot point (a) and distance(xr,yr) from origin.
Polygon is rotated by displacing each vertex through the specified rotation angle (a).
Display the polygon.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
void main()
{
int a,b,c,d,e,f,g;
int x1,x2,x3,y,ch;
int gd,gm;

detectgraph(&gd,&gm);
initgraph(&gd,&gm," ");
printf("Enter the co-ordinates");
scanf("%d%d%d%d",&a,&b,&c,&d);
bar3d(a,b,c,d,(c-a)/4,1);
getch();
while(1)
{
printf("1.translation\n2.scaling\n3.Rotation4.Exit\n");
printf("Enter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
cleardevice();
printf("Enter the translation value");
scanf("%d",&x1);
a=a+x1;
b=b+x1;
c=c+x1;
d=d+x1;
bar3d(a,b,c,d,(c-a)/4,1);
getch();
break;

case 2:
cleardevice();
printf("Enter the scaling value");
scanf("%d",&x2);
a=a*x2;
b=b*x2;
c=c*x2;
d=d*x2;
bar3d(a,b,c,d,(c-a)/4,1);
getch();
break;
case 3:
cleardevice();
printf("Enter the rotation value");
scanf("%d",&x3);
a=a*cos(x3)+a*sin(x3);
b=b*sin(x3)-b*cos(x3);
c=c*cos(x3)+c*sin(x3);
d=d*sin(x3)-d*cos(x3);
bar3d(a,b,c,d,(c-a)/4,1);
getch();
break;
case 4:
exit(0);

}
}
}
OUTPUT OF 3D TRANSFORMATION
Enter the co-ordinates
200
200
300
300
1.Translation
2.Scaling
3.Rotation
4.Exit
Enter ur choice 1

Enter the translation value 2

1.Translation
2.Scaling
3.Rotation
4.Exit
Enter ur choice 2
Enter the scaling value 2

1.Translation
2.Scaling
3.Rotation
4.Exit
Enter ur choice 3
Enter the Roatation angle 20

RESULT:
Thus the program to implement various 3D transformation like translation, scaling, rotation was
executed.

Ex.No: 8

COMPOSITE THREE DIMENSIONAL TRANSFORMATIONS

Aim:
To write a C program to implement composite 3D transformations.
Algorithm:
1. Start the program
2. Declare the variables and initialise the graphics driver.
3. Get the translation factors, scaling factors form the user.
4. Call the function to draw the 3d bar.
5. Then perform the composite transformations.
6. Display the corresponding outputs.
7. Stop the driver.
8. Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
void draw_bar3d()
{
int x1,x2,y1,y2,i,tx,ty,tz,z;
float sx,sy,sz;

x1=200;y1=200;x2=250;y2=250,z=20;
bar3d(x1,y1,x2,y2,z,1);
printf("Enter translation factor and scale factor:");
scanf("%d%d%d%f%f%f",&tx,&ty,&tz,&sx,&sy,&sz);
getch();
cleardevice();
printf("After translation:\n");
setcolor(111);
x1=x1+tx;y1=y1+ty;x2=x2+tx;y2=y2+ty;
bar3d(x1,y1,x2,y2,z,1);
getch();
cleardevice();
x1=x1*sx;y1=y1*sy;z=z*sz;
printf("After scaling:\n");
bar3d(x1,y1,x2,y2,z,1);
getch();
}
void main()
{
int x1,y1,x2,y2,ch;
int gdriver=DETECT,gmode,gerror;
initgraph(&gdriver,&gmode,"D:\\TC\\BGI\\");
draw_bar3d();
getch();
}

Output:
Enter translation factor and scale factor: 100 100 20 1.5 0.5 1

After translation:

After scaling:

Result:
Thus the C program to implement composite 3D transformations was done
successfully.

Ex.No: 10.a) DRAWING TWO DIMENSIONAL OBJECTS AND SCENES

Aim:
To write a C program to draw 2D dimensional objects and scenes.

Algorithm:
1. Start the program.
2. Declare the variables and initialise the graphics driver.
3. Get the input parameters to draw the 2D objects form the user.

4. Call the input function to draw the 2D objects.


5. Then perform the loop with clear screen and delay for the various transformations.
6. Display the corresponding outputs by calling the output functions.
7. Stop the driver.
8. Stop the program.

Program:
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<math.h>
void main()
{
int gd,gm;
int x,y;
int i,j,kk;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(WHITE);
line(0,400,640,400);
rectangle(300,330,340,400);
rectangle(310,320,330,330);
setcolor(4);
line(319,280,319,398);
line(320,280,320,398);
rectangle(320,280,330,300);
outtextxy(340,280,"PRESS ANY KEY TO IGNITE THE ROCKET");
getch();

for(j=400;j<640;j++)
{
cleardevice();
setcolor(WHITE);
line(0,j,640,j);
rectangle(300,j-70,340,j);
rectangle(310,j-80,330,j-70);
setcolor(RED);
line(319,280,319,400);
line(320,280,320,400);
rectangle(320,280,330,300);
setcolor(YELLOW);
circle(325,300,2);
delay(5);
}
for(i=400;i>340;i--)
{
cleardevice();
setcolor(RED);
line(319,i,319,i-120);
line(320,i,320,i-120);
rectangle(320,i-120,330,i-100);
setcolor(YELLOW);
circle(325,i-100,2);
delay(25);
}
cleardevice();
kk=0;

for(j=100;j<350;j++)
{
if(j%20==0)
{
setcolor(kk);
kk=kk+3;
delay(50);
}
ellipse(320,30,0,360,j+100,j+0);
}
for(j=100;j<350;j++)
{
if(j%20==0)
{
setcolor(BLACK);
delay(2);
}
ellipse(320,30,0,360,j+100,j+0);
}
cleardevice();
for(i=0;i<70;i++)
{
setcolor(i);
settextstyle(GOTHIC_FONT,HORIZ_DIR,6);
outtextxy(110,150,"HELLO WORLD!");
delay(90);
}
getch();

}
Output:

Result:
Thus the C program to draw 2D dimensional objects and scenes was done successfully.

Ex.No: 10.b)

Drawing three dimensional objects and Scenes

Aim:
To write a C program to draw 3D dimensional objects and scenes.
Algorithm:
1. Start the program.
2. Declare the variables and initialise the graphics driver.
3. Get the input parameters to draw the 3D objects form the user.
4. Call the input function to draw the 3D objects.
5. Then perform the loop with clear screen and delay for the various transformations.

6. Display the corresponding outputs by calling the output functions.


7. Stop the driver.
8. Stop the program.
Program:
#include<GL/glut.h>
void axis(double length)
{
glPushMatrix();
glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(0,0,length);
glEnd();
glTranslated(0,0,length -0.2);
glutWireCone(0.04,0.2,12,9);
glPopMatrix();
}
void displayWire(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0*64/48.0,2.0*64/48.0,-2.0,2.0,0.1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2.0,2.0,2.0,0.0,0.0,0.0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3d(0,0,0);
axis(0.5);
glPushMatrix();

glRotated(90,0,1.0,0);
axis(0.5);
glRotated(-90.0,1,0,0);
axis(0.5);
glPopMatrix();
glPushMatrix();
glTranslated(0.5,0.5,0.5);
glutWireCube(1.0);
glPopMatrix();
glPushMatrix();
glTranslated(1.0,1.0,0);
glutWireSphere(0.25,10,8);
glPopMatrix();
glPushMatrix();
glTranslated(1.0,0,1.0);
glutWireCone(0.2,0.5,10,8);
glPopMatrix();
glPushMatrix();
glTranslated(1,1,1);
glutWireTeapot(0.2);
glPopMatrix();
glPushMatrix();
glTranslated(0,1.0,0);
glRotated(90.0,1,0,0);
glutWireTorus(0.1,0.3,10,10);
glPopMatrix();
glPushMatrix();
glTranslated(1.0,0,0);

glScaled(0.15,0.15,0.15);
glutWireDodecahedron();
glPopMatrix();
glPushMatrix();
glTranslated(0,1.0,1.0);
glutWireCube(0.25);
glPopMatrix();
glPushMatrix();
glTranslated(0,0,1.0);
GLUquadricObj * qobj;
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_LINE);
gluCylinder(qobj,0.2,0.2,0.4,8,3);
glPopMatrix();
glFlush();
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow(" Transformation test wire frames");
glutDisplayFunc(displayWire);
glClearColor(1.0f,1.0f,1.0f,0.0f);
glViewport(0,0,640,480);
glutMainLoop();
}

Output:

Result:
Thus the C program to draw 3D dimensional objects and scenes was done successfully.

Potrebbero piacerti anche