237762544 Cs2405 Computer Graphics Lab Manual New

118
CS2405-COMPUTER GRAPHICS LABORATORY 2014-2015, VII Semester Prepared by M.J.Jeyasheela Rakkini A.P M.Flora Mary A.P DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING MOOKAMBIGAI COLLEGE OF ENGINEERING SRINIVASANAGAR, KALAMAVUR-622 502.

description

anna university Information Technology 7th semester Computer graphics

Transcript of 237762544 Cs2405 Computer Graphics Lab Manual New

Page 1: 237762544 Cs2405 Computer Graphics Lab Manual New

CS2405-COMPUTER GRAPHICS LABORATORY

2014-2015, VII Semester

Prepared by M.J.Jeyasheela Rakkini A.P

M.Flora Mary A.P

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

MOOKAMBIGAI COLLEGE OF ENGINEERING

SRINIVASANAGAR, KALAMAVUR-622 502.

Page 2: 237762544 Cs2405 Computer Graphics Lab Manual New

CS2405 - COMPUTER GRAPHICS LAB

1. Output primitives. 2. Implementation of Bresenhams Algorithm – Line, Circle, Ellipse.3. Implementation of Line, Circle and ellipse Attributes4. Two Dimensional transformations - Translation, Rotation, Scaling, Reflection, Shear.5. Composite 2D Transformations6. Cohen Sutherland 2D line clipping and Windowing7. Sutherland – Hodgeman Polygon clipping Algorithm8. Three dimensional transformations - Translation, Rotation, Scaling9. Composite 3D transformations10. Drawing three dimensional objects and Scenes11. Generating Fractal images

Page 3: 237762544 Cs2405 Computer Graphics Lab Manual New

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

Page 4: 237762544 Cs2405 Computer Graphics Lab Manual New

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

Page 5: 237762544 Cs2405 Computer Graphics Lab Manual New

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.

Page 6: 237762544 Cs2405 Computer Graphics Lab Manual New

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 Y-coordinate 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);

Page 7: 237762544 Cs2405 Computer Graphics Lab Manual New

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.

Page 8: 237762544 Cs2405 Computer Graphics Lab Manual New

An Example program using the basic graphic functions

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

void main()

{

    int gd=DETECT, gm;

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

  circle(100,100,50);

    outtextxy(75,170, "Circle");

    rectangle(200,50,350,150);

    outtextxy(240, 170, "Rectangle");

    ellipse(500, 100,0,360, 100,50);

    outtextxy(480, 170, "Ellipse");

    line(100,250,540,250);

    outtextxy(300,260,"Line");

getch();

closegraph();

}

Page 9: 237762544 Cs2405 Computer Graphics Lab Manual New

Ex.No:1

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

Page 10: 237762544 Cs2405 Computer Graphics Lab Manual New

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

Page 11: 237762544 Cs2405 Computer Graphics Lab Manual New

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:

Page 12: 237762544 Cs2405 Computer Graphics Lab Manual New

Ex.No:2a

Page 13: 237762544 Cs2405 Computer Graphics Lab Manual New

Program:#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>void main(){int gd = DETECT, gm;int x1, y1, x2, y2,dx,dy,steps,k;float xincrement,yincrement,x,y;initgraph(&gd, &gm, "..\\bgi");printf("Enter the Starting Point of x axis : ");scanf("%d", &x1);printf("Enter the Starting of y axis : ");scanf("%d", &y1);printf("Enter the End Point of x axis : ");scanf("%d", &x2);printf("Enter the End Point of y axis : ");scanf("%d", &y2);clrscr();dx = x2 – x1;dy = y2 – y1;x=x1;y=y1;if(abs(dx) > abs(dy))steps=abs(dx);elsesteps=abs(dy);xincrement=dx/(float)steps;yincrement=dy/(float)steps;putpixel(ceil(x), ceil(y), RED);for(k=1;k<=steps;k++){x=x+xincrement;y=y+yincrement;putpixel(ceil(x),ceil(y), RED);}getch();closegraph();}

Output

Page 14: 237762544 Cs2405 Computer Graphics Lab Manual New

Enter the Starting Point of x axis : 100Enter the Starting of y axis : 100Enter the End Point of x axis : 200Enter the End Point of y axis : 200

Ex.No:2b

Page 15: 237762544 Cs2405 Computer Graphics Lab Manual New

Program

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>void main(){int gd = DETECT, gm;int x,y,x1,y1,x2,y2,p,dx,dy,twody,twodydx,xend;initgraph(&gd,&gm,"..\\BGI:");printf("\nEnter the x-coordinate of the starting point :");scanf("%d",&x1);printf("\nEnter the y-coordinate of the starting point :");scanf("%d",&y1);printf("\nEnter the x-coordinate of the Ending point :");scanf("%d",&x2);printf("\nEnter the y-coordinate of the ending point :");scanf("%d",&y2);clrscr();dx=x2-x1;dy=y2-y1;p=2*dy-dx;twody=2*dy;twodydx=2*(dy-dx);if (x1>x2){x=x2;y=y2;xend=x1;}else{x=x1;y=y1;xend=x2;}putpixel(x,y,RED);while(x<xend){x++;if (p<0)

Page 16: 237762544 Cs2405 Computer Graphics Lab Manual New

p=p+twody;else{y=y+1;p=p+twodydx;}putpixel(x,y,RED);}getch();closegraph();}

Page 17: 237762544 Cs2405 Computer Graphics Lab Manual New

Output

Enter the x-coordinate of the starting point : 100Enter the y-coordinate of the starting point : 100Enter the x-coordinate of the Ending point : 200Enter the y-coordinate of the ending point : 200

Ex.No:2c

Page 18: 237762544 Cs2405 Computer Graphics Lab Manual New

Program

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>int x,y,r,p,xcenter, ycenter;void circleplot(int,int,int,int);void main(){int gd = DETECT, gm;initgraph(&gd,&gm,"..\\BGI:");printf("\nEnter the x-coordinate of the centre point :");scanf("%d",&xcenter);printf("\nEnter the y-coordinate of the centre point :");scanf("%d",&ycenter);printf("\nEnter the radius :");scanf("%d",&r);x=0;y=r;p=1-r;while (x<y){x++;if (p<0)p=p+2*x+1;else{y--;p=p+2*(x-y)+1;}circleplot(xcenter,ycenter,x,y);}getch();closegraph();}void circleplot(int xcenter, int ycenter,int x, int y){putpixel(xcenter+x,ycenter+y,10);putpixel(xcenter-x,ycenter+y,10);putpixel(xcenter+x,ycenter-y,10);putpixel(xcenter-x,ycenter-y,10);

Page 19: 237762544 Cs2405 Computer Graphics Lab Manual New

putpixel(xcenter+y,ycenter+x,10);putpixel(xcenter-y,ycenter+x,10);putpixel(xcenter+y,ycenter-x,10);putpixel(xcenter-y,ycenter-x,10);}

Output

Page 20: 237762544 Cs2405 Computer Graphics Lab Manual New

Enter the x-coordinate of the centre point : 100Enter the y-coordinate of the centre point : 100Enter the radius : 50

Ex.No:2d

Page 21: 237762544 Cs2405 Computer Graphics Lab Manual New

Program

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>int x,y,r,p,xcenter, ycenter;void circleplot(int,int,int,int);void main(){int gd = DETECT, gm;initgraph(&gd,&gm,"..\\BGI:");printf("\nEnter the x-coordinate of the centre point :");scanf("%d",&xcenter);printf("\nEnter the y-coordinate of the centre point :");scanf("%d",&ycenter);printf("\nEnter the radius :");scanf("%d",&r);x=0;y=r;p=3-(2*r);while (x<y){x++;if (p<0)p=p+(4*x)+6;else{y--;p=p+10+4*(x-y);}circleplot(xcenter,ycenter,x,y);}getch();closegraph();}void circleplot(int xcenter, int ycenter,int x, int y){putpixel(xcenter+x,ycenter+y,10);putpixel(xcenter-x,ycenter+y,10);putpixel(xcenter+x,ycenter-y,10);putpixel(xcenter-x,ycenter-y,10);

Page 22: 237762544 Cs2405 Computer Graphics Lab Manual New

putpixel(xcenter+y,ycenter+x,10);putpixel(xcenter-y,ycenter+x,10);putpixel(xcenter+y,ycenter-x,10);putpixel(xcenter-y,ycenter-x,10);}

Output

Page 23: 237762544 Cs2405 Computer Graphics Lab Manual New

Enter the x-coordinate of the centre point : 100Enter the y-coordinate of the centre point : 100Enter the radius : 50

Ex.No:2e

Page 24: 237762544 Cs2405 Computer Graphics Lab Manual New

Program#include<graphics.h>#include<stdio.h>#include<conio.h>#include<stdlib.h>#define round(a) ((int)(a+0.5))void ellipseplot(int,int,int,int);int xcenter,yc,rx,ry;void main(){int gd=DETECT,gm;int xcenter,ycenter,rx,ry;long rx2,ry2,tworx2,twory2,p,x,y,px,py;initgraph(&gd,&gm,"..\\BGI:");printf("\nEnter the x-coordinate of the centre point :");scanf("%d",&xcenter);printf("\nEnter the y-coordinate of the centre point :");scanf("%d",&ycenter);printf("\nEnter the x-coordinate of the radius :");scanf("%d",&rx);printf("\nEnter the y-coordinate of the radius :");scanf("%d",&ry);rx2=rx*rx;ry2=ry*ry;twory2=2*ry2;tworx2=2*rx2;x=0;y=ry;py=tworx2*y;px=0;ellipseplot(xcenter,ycenter,x,y);p=round(ry2-(rx2*ry)+(0.25*rx2));while(px<py){x++;px=px+twory2;if(p<0)p=p+ry2+px;else{y--;py=py-tworx2;

Page 25: 237762544 Cs2405 Computer Graphics Lab Manual New

p=p+ry2+px-py;}ellipseplot(xcenter,ycenter,x,y);}p=round(ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2);while(y>0){y--;py=py-tworx2;if(p>0)p=p+rx2-py;else{x++;px=px+twory2;p=p+rx2-py+px;}ellipseplot(xcenter,ycenter,x,y);}getch();closegraph();}void ellipseplot(int xcenter,int ycenter,int x,int y){putpixel(xcenter +x,ycenter +y,20);putpixel(xcenter -x,ycenter +y,20);putpixel(xcenter +x,ycenter -y,20);putpixel(xcenter -x,ycenter -y,20);}

Page 26: 237762544 Cs2405 Computer Graphics Lab Manual New

Output

Enter the x-coordinate of the centre point : 100Enter the y-coordinate of the centre point : 200Enter the x-coordinate of the radius : 50Enter the y-coordinate of the radius : 60

Ex.No:2f

Page 27: 237762544 Cs2405 Computer Graphics Lab Manual New

Program

#include<graphics.h>#include<stdio.h>#include<conio.h>#include<stdlib.h>void ellipseplot(int,int,int,int);int xcenter,yc,rx,ry;void main(){int gd=DETECT,gm;long xcenter,ycenter,rx,ry;long rx2,ry2,tworx2,twory2,d1,d2,x,y,dx,dy;initgraph(&gd,&gm,"..\\BGI:");printf("\nEnter the x-coordinate of the centre point :");scanf("%ld",&xcenter);printf("\nEnter the y-coordinate of the centre point :");scanf("%ld",&ycenter);printf("\nEnter the x-coordinate of the radius :");scanf("%ld",&rx);printf("\nEnter the y-coordinate of the radius :");scanf("%ld",&ry);clrscr();rx2=rx*rx;ry2=ry*ry;twory2=2*ry2;tworx2=2*rx2;x=0;y=ry;d1=ry2-rx2*ry+(0.25*rx2);dx=twory2*x;dy=tworx2*y;do{ellipseplot(xcenter,ycenter,x,y);if(d1<0){x=x+1;y=y;dx=dx+twory2;d1=d1+dx+ry2;}

Page 28: 237762544 Cs2405 Computer Graphics Lab Manual New

else{x=x+1;y=y-1;dx=dx+twory2;dy=dy-tworx2;d1=d1+dx-dy+ry2;}}while(dx<dy);d2=ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2;do{ellipseplot(xcenter,ycenter,x,y);if(d2>0){x=x;y=y-1;dy=dy-tworx2;d2=d2-dy+rx2;}else{x=x+1;y=y-1;dy=dy-tworx2;dx=dx+twory2;d2=d2+dx-dy+rx2;}}while(y>0);getch();closegraph();}void ellipseplot(int xcenter,int ycenter,int x,int y){putpixel(xcenter+x,ycenter+y,15);putpixel(xcenter-x,ycenter+y,15);putpixel(xcenter+x,ycenter-y,15);putpixel(xcenter-x,ycenter-y,15);}

Page 29: 237762544 Cs2405 Computer Graphics Lab Manual New

Output

Enter the x-coordinate of the centre point : 100Enter the y-coordinate of the centre point : 200Enter the x-coordinate of the radius : 50Enter the y-coordinate of the radius : 60

Ex. No. 3a :

Page 30: 237762544 Cs2405 Computer Graphics Lab Manual New

Program

#include <graphics.h>#include <stdlib.h>#include <string.h>#include <stdio.h>#include <conio.h>void main(){int gd=DETECT,gm;int ch;clrscr();while(1){printf("******Line Styles*******\n");printf("\n1.Solid Line");printf("\n2.Dotted Line");printf("\n3.Center Line");printf("\n4.Dashed Line");printf("\n5.Userbit Line");printf("\n6.Exit");printf("\n\nEnter your choice:\n");scanf("%d",&ch);switch(ch){case 1:clrscr();initgraph(&gd,&gm," ");setlinestyle(0,1,3);line(100,30,250,250);getch();cleardevice();closegraph();break;case 2:clrscr();initgraph(&gd,&gm," ");clrscr();setlinestyle(1,1,3);line(100,30,250,250);getch();cleardevice();closegraph();

Page 31: 237762544 Cs2405 Computer Graphics Lab Manual New

break;case 3:clrscr();initgraph(&gd,&gm," ");setlinestyle(2,1,3);line(100,30,250,250);getch();cleardevice();closegraph();break;case 4:clrscr();initgraph(&gd,&gm," ");setlinestyle(3,1,3);line(100,30,250,250);getch();cleardevice();closegraph();break;case 5:clrscr();initgraph(&gd,&gm," ");setlinestyle(4,1,3);line(100,30,250,250);getch();cleardevice();closegraph();break;case 6:exit(0);}}}

OUTPUT

Page 32: 237762544 Cs2405 Computer Graphics Lab Manual New

******Line Styles*******1.Solid Line2.Dotted Line3.Center Line4.Dashed Line5.Userbit Line6.Exit

Enter Ur Choice: 1

Solid Line

Entrer ur Choice: 2

Dotted Line

……………………..

Enter ur choice : 3

Page 33: 237762544 Cs2405 Computer Graphics Lab Manual New

Enter ur Choicre: 6

Ex.No:3b

Page 34: 237762544 Cs2405 Computer Graphics Lab Manual New

Program

#include <graphics.h>#include <stdlib.h>#include <string.h>#include <stdio.h>#include <conio.h>void main(){int gd=DETECT,gm;int ch;clrscr();while(1){printf("******Circle Attributes*******\n");printf("\n1.Empty Fill");printf("\n2.Soild Fill");printf("\n3.Line Fill");printf("\n4.Wide dot Fill");printf("\n5.close dot Fill");printf("\n6.User Fill");printf("\n7.Exit");printf("\n\nEnter your choice:\n");scanf("%d",&ch);switch(ch){case 1:clrscr();initgraph(&gd,&gm," ");setfillstyle(EMPTY_FILL, RED);circle(100, 100, 50);floodfill(100, 100, WHITE);getch();cleardevice();closegraph();break;case 2:clrscr();initgraph(&gd,&gm," ");setfillstyle(SOLID_FILL, RED);circle(100, 100, 50);

Page 35: 237762544 Cs2405 Computer Graphics Lab Manual New

floodfill(100, 100, WHITE);getch();cleardevice();closegraph();break;case 3:clrscr();initgraph(&gd,&gm," ");setfillstyle(LINE_FILL, RED);circle(100, 100, 50);floodfill(100, 100, WHITE);getch();cleardevice();closegraph();break;case 4:clrscr();initgraph(&gd,&gm," ");setfillstyle(WIDE_DOT_FILL, RED);circle(100, 100, 50);floodfill(100, 100, WHITE);getch();cleardevice();closegraph();break;case 5:clrscr();initgraph(&gd,&gm," ");setfillstyle(CLOSE_DOT_FILL, RED);circle(100, 100, 50);floodfill(100, 100, WHITE);getch();cleardevice();closegraph();break;case 6:clrscr();initgraph(&gd,&gm," ");setfillstyle(USER_FILL, RED);circle(100, 100, 50);floodfill(100, 100, WHITE);getch();cleardevice();closegraph();break;

Page 36: 237762544 Cs2405 Computer Graphics Lab Manual New

case 7:exit(0);}}}

OUTPUT

Page 37: 237762544 Cs2405 Computer Graphics Lab Manual New

"******Circle Attributes*******1.Empty Fill2.Soild Fill3.Line Fill4.Wide dot Fill5.close dot Fill6.User Fill7.ExitEnter your choice: 1

Enter Ur Choice:2

Enter Ur Choice:3

Page 38: 237762544 Cs2405 Computer Graphics Lab Manual New

Entrer ur Choice:6

Ex.No:3c

Page 39: 237762544 Cs2405 Computer Graphics Lab Manual New

Program:#include <graphics.h>#include <stdlib.h>#include <string.h>#include <stdio.h>#include <conio.h>void main(){int gd=DETECT,gm;int ch;clrscr();while(1){printf("******Ellipse Attributes*******\n");printf("\n1.Empty Fill");printf("\n2.Soild Fill");printf("\n3.Line Fill");printf("\n4.Wide dot Fill");printf("\n5.close dot Fill");printf("\n6.User Fill");printf("\n7.Exit");printf("\n\nEnter your choice:\n");scanf("%d",&ch);switch(ch){case 1:clrscr();initgraph(&gd,&gm," ");setfillstyle(EMPTY_FILL, RED);ellipse(100, 100,0,360,50,25);floodfill(100, 100, WHITE);getch();cleardevice();closegraph();break;case 2:clrscr();initgraph(&gd,&gm," ");setfillstyle(SOLID_FILL, RED);ellipse(100, 100,0,360,50,25);floodfill(100, 100, WHITE);getch();

Page 40: 237762544 Cs2405 Computer Graphics Lab Manual New

cleardevice();closegraph();break;case 3:clrscr();initgraph(&gd,&gm," ");setfillstyle(LINE_FILL, RED);ellipse(100, 100,0,360,50,25);floodfill(100, 100, WHITE);getch();cleardevice();closegraph();break;case 4:clrscr();initgraph(&gd,&gm," ");setfillstyle(WIDE_DOT_FILL, RED);ellipse(100, 100,0,360,50,25);floodfill(100, 100, WHITE);getch();cleardevice();closegraph();break;case 5:clrscr();initgraph(&gd,&gm," ");setfillstyle(CLOSE_DOT_FILL, RED);ellipse(100, 100,0,360,50,25);floodfill(100, 100, WHITE);getch();cleardevice();closegraph();break;case 6:clrscr();initgraph(&gd,&gm," ");setfillstyle(USER_FILL, RED);ellipse(100, 100,0,360,50,25);floodfill(100, 100, WHITE);getch();cleardevice();closegraph();break;case 7:

Page 41: 237762544 Cs2405 Computer Graphics Lab Manual New

exit(0);}}}

OUTPUT:

Page 42: 237762544 Cs2405 Computer Graphics Lab Manual New

"******Ellipse Attributes*******1.Empty Fill2.Soild Fill3.Line Fill4.Wide dot Fill5.close dot Fill6.User Fill7.Exit

Entrer ur choice: 1

Entrer ur choice: 2

Page 43: 237762544 Cs2405 Computer Graphics Lab Manual New

Enter ur choice : 6

Ex.No.4

Program

#include<stdio.h>

Page 44: 237762544 Cs2405 Computer Graphics Lab Manual New

#include<conio.h>

#include<graphics.h>

#include<dos.h>

#include<math.h>

#include<stdlib.h>

void menu();

void input();

void output();

void translation();

void rotation();

void scaling();

void shearing();

void reflection();

int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y;

float sx,sy;

void menu()

{

printf("menu\n");

printf("1.Translation\n");

printf("2.rotation\n");

printf("3.scaling\n");

printf("4.shearing\n");

printf("5.reflection\n");

printf("6.exit\n");

Page 45: 237762544 Cs2405 Computer Graphics Lab Manual New

printf("enter the choice:");

scanf("%d",&option);

switch(option)

{

case 1:

input();

translation();

break;

case 2:

input();

rotation();

break;

case 3:

input();

scaling();

break;

case 4 :

input();

shearing();

break;

case 5:

input();

reflection();

break;

Page 46: 237762544 Cs2405 Computer Graphics Lab Manual New

case 6:

exit(0);

break;

}

}

void input()

{

printf("enter the number of vertices:" );

scanf("%d",&n);

for(i=0;i<n;i++)

{

printf("enter the coordinates:");

scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);

}

}

void output()

{

cleardevice();

for(i=0;i<n;i++)

{

line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);

}

}

void translation()

Page 47: 237762544 Cs2405 Computer Graphics Lab Manual New

{

output();

printf("enter the tranformation vertex tx,ty:\n");

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;

}

output();

delay(10);

menu();

}

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++)

{

a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k);

Page 48: 237762544 Cs2405 Computer Graphics Lab Manual New

a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k);

}

output();

delay(10);

menu();

}

void scaling()

{

output();

printf("enter the scaling factor\n");

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

}

output();

delay(10);

menu();

}

void shearing()

{

Page 49: 237762544 Cs2405 Computer Graphics Lab Manual New

output();

printf("enter the shear value:");

scanf("%d",&sh);

printf("enter the fixed point:");

scanf("%d%d",&fx,&fy);

printf("enter the axis for shearing if x-axis then 1 if y-axis the 0:");

scanf("%d",&axis);

for(i=0;i<=n;i++)

{

if(axis==1)

{

a[i][0]=a[i][0]+sh*(a[i][1]-fy);

}

else

{

a[i][1]=a[i][1]+sh*(a[i][0]-fx);

}

}

output();

delay(10);

menu();

}

void reflection()

{

Page 50: 237762544 Cs2405 Computer Graphics Lab Manual New

output();

for(i=0;i<=n;i++)

{

temp=a[i][0];

a[i][0]=a[i][1];

a[i][1]=temp;

}

output();

delay(10);

menu();

}

void main()

{

int gd=DETECT,gm;

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

menu();

getch();

}

Page 51: 237762544 Cs2405 Computer Graphics Lab Manual New

OUTPUT

Menu

1. Translation2. Rotation3. Scaling4. Shearing5. Reflection6. Exit

TRANSLATION

Page 52: 237762544 Cs2405 Computer Graphics Lab Manual New

Enter the choice : 1

Enter the number of Vertices: 3

Enter the coordinates : 30 150 10 200

Enter the coordinates : 10 200 60 200

Enter the coordinates : 60 200 30 150

Enter the translation vector Tx, Ty : 90 60

Page 53: 237762544 Cs2405 Computer Graphics Lab Manual New

ROTATION

Enter the choice : 2

Enter the number of Vertices: 3

Enter the coordinates : 30 150 10 200

Enter the coordinates : 10 200 60 200

Enter the coordinates : 60 200 30 150

Enter the Rotating Angle : 90

Enter the Pivot Point : 100 200

`

Page 54: 237762544 Cs2405 Computer Graphics Lab Manual New

SCALING

Enter the choice : 3

Enter the number of Vertices: 3

Enter the coordinates : 30 150 10 200

Enter the coordinates : 10 200 60 200

Enter the coordinates : 60 200 30 150

Page 55: 237762544 Cs2405 Computer Graphics Lab Manual New

Enter the scaling Factor : 0.3 0.4

Enter the Fixed Point : 100 200

Page 56: 237762544 Cs2405 Computer Graphics Lab Manual New

SHEARING

Enter the choice : 4

Enter the number of Vertices: 3

Enter the coordinates : 30 150 10 200

Enter the coordinates : 10 200 60 200

Enter the coordinates : 60 200 30 150

Enter the shear Value : 5

Enter the fixed point : 50 100

Enter the Axis for shearing if x-axis then 1

if y-axis then 0

Page 57: 237762544 Cs2405 Computer Graphics Lab Manual New

REFLECTION

Enter the choice : 5

Enter the number of Vertices: 3

Enter the coordinates : 30 150 10 200

Enter the coordinates : 10 200 60 200

Enter the coordinates : 60 200 30 150

Page 58: 237762544 Cs2405 Computer Graphics Lab Manual New

Ex.No:5

Program

#include "Stdio.h"#include "conio.h"#include "math.h"#include "graphics.h"struct point{int x;int y;};typedef float matrix[3][3];matrix tm;void Identity( matrix m){int i,j;for(i=0;i<3;i++)for(j=0;j<3;j++)m[i][j]=(i==j);}void multiply(matrix a, matrix b){int r,c;matrix tmp;for(r=0;r<3;r++)for(c=0;c<3;c++)

Page 59: 237762544 Cs2405 Computer Graphics Lab Manual New

tmp[r][c]=a[r][0]*b[0][c]+a[r][1]*b[1][c]+a[r][2]*b[2][c];for(r=0;r<3;r++)for(c=0;c<3;c++)b[r][c]=tmp[r][c];}

void translate(int tx,int ty){matrix m;Identity(m);m[0][2]=tx;m[1][2]=ty;multiply(m,tm);}void scale(float sx,float sy, struct point refpt){matrix m;Identity(m);m[0][0]=sx;m[0][2]=(1-sx)*refpt.x;m[1][1]=sy;m[1][2]=(1-sy)*refpt.y;multiply(m,tm);}void rotate(float a , struct point refpt){matrix m;Identity(m);a=(a*3.14)/180.0;m[0][0]=cos(a);m[0][1]=-sin(a);m[0][2]=refpt.x*(1-cos(a))+refpt.y*sin(a);m[1][0]=sin(a);m[1][1]=-cos(a);m[1][2]=refpt.y*(1-cos(a))+refpt.x*sin(a);multiply(m,tm);}void transform(int npts, struct point *pts){int k;float tmp;for(k=0;k<npts;k++)

Page 60: 237762544 Cs2405 Computer Graphics Lab Manual New

{tmp=tm[0][0]*pts[k].x+tm[0][1]*pts[k].y+tm[0][2];pts[k].y=tm[1][0]*pts[k].x+tm[1][1]*pts[k].y+tm[1][2];pts[k].x=tmp;}}void main(){struct point pts[4]={220.0, 50.0, 320.0, 200.0, 150.0, 200.0, 220.0, 50.0};struct point refpt={50.0,50.0};int g=DETECT,d;initgraph(&g,&d,"c:\tc\bgi");setbkcolor(GREEN);setcolor(RED);drawpoly(4,pts);getch();Identity(tm);scale(0.5,0.5,refpt);rotate(90.0,refpt);translate(100,100);transform(4,pts);setcolor(WHITE);drawpoly(4,pts);getch();closegraph();}

Page 61: 237762544 Cs2405 Computer Graphics Lab Manual New

Output

Page 62: 237762544 Cs2405 Computer Graphics Lab Manual New

Ex.No:6

Page 63: 237762544 Cs2405 Computer Graphics Lab Manual New

Program

#include<stdio.h>#include<conio.h>#include<graphics.h>#define LEFT_EDGE 0x1#define RIGHT_EDGE 0x2#define BOTTOM_EDGE 0x4#define TOP_EDGE 0x8#define INSIDE(a) (!a)#define REJECT(a,b) (a&b)#define ACCEPT(a,b) (!(a|b))#define FALSE 0#define TRUE 1struct point{int x;int y;}p1,p2,tmp;struct win{int x;int y;}winmin,winmax;unsigned char encode(struct point pt,struct win winmin,struct win winmax){unsigned char code=0x00;if(pt.x<winmin.x)code=code|LEFT_EDGE;if(pt.x>winmax.x)code=code|RIGHT_EDGE;if(pt.y<winmin.y)code=code|BOTTOM_EDGE;if(pt.y>winmax.y)code=code|TOP_EDGE;return(code);}void swappts(struct point *p1,struct point *p2)

Page 64: 237762544 Cs2405 Computer Graphics Lab Manual New

{tmp=*p1;*p1=*p2;*p2=tmp;}void swapcodes(unsigned char *c1,unsigned char *c2){unsigned char tmp;tmp=*c1;*c1=*c2;*c2=tmp;}void clipline(struct win winmin, struct win winmax,struct point p1,struct point p2){unsigned char code1,code2;int done=FALSE,draw=FALSE;float m;while(!done){code1=encode(p1,winmin,winmax);code2=encode(p2,winmin,winmax);if(ACCEPT(code1,code2)){done=TRUE;draw=TRUE;}else if(REJECT(code1,code2))done=TRUE;else{if(INSIDE(code1)){swappts(&p1,&p2);swapcodes(&code1,&code2);}if(p2.x!=p1.x)m=(p2.y-p1.y)/(p2.x-p1.x);if(code1 &LEFT_EDGE){p1.y+=(winmin.x-p1.x)*m;p1.x=winmin.x;

Page 65: 237762544 Cs2405 Computer Graphics Lab Manual New

}else if(code1 &RIGHT_EDGE){p1.y+=(winmax.x-p1.x)*m;p1.x=winmax.x;}else if(code1 &BOTTOM_EDGE){if(p2.x!=p1.x)p1.x+=(winmin.y-p1.y)/m;p1.y=winmin.y;}else if(code1 &TOP_EDGE){if(p2.x!=p1.x)p1.x+=(winmax.y-p1.y)/m;p1.y=winmax.y;}}}if(draw)line(p1.x,p1.y,p2.x,p2.y);}void main(){int c,gm,gr;clrscr();printf("\nEnter the window minimum coordinates");scanf("%d%d",&winmin.x,&winmin.y);printf("\nEnter the window max coordinates");scanf("%d%d",&winmax.x,&winmax.y);printf("\nEnter the starting point");scanf("%d%d",&p1.x,&p1.y);printf("\nenter the end point");scanf("%d%d",&p2.x,&p2.y);detectgraph(&gm,&gr);initgraph(&gm,&gr,"d:\\tc\\BGI");printf("Before Clipping");line(p1.x,p1.y,p2.x,p2.y);rectangle(winmin.x,winmax.y,winmax.x,winmin.y);getch();clrscr();

Page 66: 237762544 Cs2405 Computer Graphics Lab Manual New

cleardevice();printf("After Clipping");rectangle(winmin.x,winmax.y,winmax.x,winmin.y);clipline(winmin,winmax,p1,p2);getch();}

Page 67: 237762544 Cs2405 Computer Graphics Lab Manual New

OUTPUT:

Enter the clip window coordinates : 100 100 200 200

Enter the line coordinates : 0 0 500 500

Before clipping

After clipping

Ex.No:7

Page 68: 237762544 Cs2405 Computer Graphics Lab Manual New

Program

#include<stdio.h>#include<conio.h>#include<graphics.h>typedef enum { left,right,bottom,top } edge;#define N_EDGE 4#define TRUE 1#define FALSE 0struct point{int x;int y;}p,wmin,wmax,p1,p2,ipt,i,pin[50],pout[50],first[50],s[50],i;int inside(struct point p,int b,struct point wmin,struct point wmax){switch(b){case left:if(p.x<wmin.x)return (FALSE);break;case right:if(p.x>wmax.x)return (FALSE);break;case bottom:if(p.y<wmin.y)return (FALSE);break;case top:if(p.y>wmax.y)return (FALSE);break;}return (TRUE);}int cross(struct point p1,struct point p2,int b,struct point wmin,struct point wmax){if(inside(p1,b,wmin,wmax)==inside(p2,b,wmin,wmax))return (FALSE);

Page 69: 237762544 Cs2405 Computer Graphics Lab Manual New

elsereturn (TRUE);}struct point intersect(struct point p1,struct point p2,int b,struct point wmin,struct point wmax){float m;if(p1.x!=p2.x)m=(p1.y-p2.y)/(p1.x-p2.x);switch(b){case left:ipt.x=wmin.x;ipt.y=p2.y+(wmin.x-p2.x)*m;break;case right:ipt.x=wmax.x;ipt.y=p2.y+(wmax.x-p2.x)*m;break;case bottom:ipt.y=wmin.y;if(p1.x!=p2.x)ipt.x=p2.x+(wmin.y-p2.y)/m;elseipt.x=p2.x;break;case top:ipt.y=wmax.y;if(p1.x!=p2.x)ipt.x=p2.x+(wmax.y-p2.y)/m;elseipt.x=p2.x;break;}return(ipt);}void clippoint(struct point p,int b,struct point wmin,struct point wmax,struct point *pout,int *cnt,struct point *first[],struct point *s){if(!first[b])first[b]=&p;else

Page 70: 237762544 Cs2405 Computer Graphics Lab Manual New

if(cross(p,s[b],b,wmin,wmax)){ipt=intersect(p,s[b],b,wmin,wmax);if(b<top)clippoint(ipt,b+1,wmin,wmax,pout,cnt,first,s);else{pout[*cnt]=ipt;(*cnt)++;}}s[b]=p;if(inside(p,b,wmin,wmax))if(b<top)clippoint(p,b+1,wmin,wmax,pout,cnt,first,s);else{pout[*cnt]=p;(*cnt)++;}}void closeclip(struct point wmin,struct point wmax,struct point *pout,int *cnt,struct point *first[],struct point *s){int b;for(b=left;b<=top;b++){if(cross(s[b],*first[b],b,wmin,wmax)){i=intersect(s[b],*first[b],b,wmin,wmax);if(b<top)clippoint(i,b+1,wmin,wmax,pout,cnt,first,s);else{pout[*cnt]=i;(*cnt)++;}}}int clippolygon(struct point wmin,struct point wmax,int n,struct point *pin,struct point *pout){struct point *first[N_EDGE]={0,0,0,0},s[N_EDGE];

Page 71: 237762544 Cs2405 Computer Graphics Lab Manual New

int i,cnt=0;for(i=0;i<n;i++)clippoint(pin[i],left,wmin,wmax,pout,&cnt,first,s);closeclip(wmin,wmax,pout,&cnt,first,s);return(cnt);}void main(){int c,gm,gr,n,j,np;detectgraph(&gm,&gr);initgraph(&gm,&gr,"d:\\tc\\BGI");printf("Enter the window minimum coordinates");scanf("%d%d",&wmin.x,&wmin.y);printf("Enter the window max coordinates");scanf("%d%d",&wmax.x,&wmax.y);rectangle(wmin.x,wmax.y,wmax.x,wmin.y);printf("Enter the no of sides in polygon:\n");scanf("%d",&n);printf("Enter the coordinates(x,y)for pin ,pout:\n");for(j=0;j<n;j++){scanf("%d%d",&pin[j].x,&pin[j].y);scanf("%d%d",&pout[j].x,&pout[j].y);}detectgraph(&gm,&gr);initgraph(&gm,&gr,"d:\\tc\\BGI");for(j=0;j<n;j++)line(pin[j].x,pin[j].y,pout[j].x,pout[j].y);rectangle(wmin.x,wmax.y,wmax.x,wmin.y);detectgraph(&gm,&gr);initgraph(&gm,&gr,"d:\\tc\\BGI");rectangle(wmin.x,wmax.y,wmax.x,wmin.y);np=clippolygon(wmin,wmax,n,pin,pout);for(j=0;j<np;j++){line(pout[j].x,pout[j].y,pout[(j+1)].x,pout[(j+1)].y);}getch();}

Output

Page 72: 237762544 Cs2405 Computer Graphics Lab Manual New

Enter the window minimum coordinates200200Enter the window max coordinates400400Enter the no of sides in polygon3Enter the coordinates(x,y)for pin ,pout150300250175250175350

410350410150300

Ex.No:8

Page 73: 237762544 Cs2405 Computer Graphics Lab Manual New

PROGRAM:

Translation:

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

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

Page 74: 237762544 Cs2405 Computer Graphics Lab Manual New

getch();

setcolor(9);

setfillstyle(CLOSE_DOT_FILL,3);

bar3d(l=10,t=50,r=100,b=150,dp=15,tf=1);

getch();

}

OUTPUT:

3D TRANSFOTMATIONS

Enter x ,y values:350,250

Rotation:

Page 75: 237762544 Cs2405 Computer Graphics Lab Manual New

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

Page 76: 237762544 Cs2405 Computer Graphics Lab Manual New

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

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

}

OUTPUT:

Enter the rotation angle: 60

Page 77: 237762544 Cs2405 Computer Graphics Lab Manual New

After rotation about z-axis

After rotation about y-axis

Page 78: 237762544 Cs2405 Computer Graphics Lab Manual New

After rotation about x-axis

Scaling

Page 79: 237762544 Cs2405 Computer Graphics Lab Manual New

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

Page 80: 237762544 Cs2405 Computer Graphics Lab Manual New

OUTPUT:

Original image:

Enter x, y, and z: 20, 30, 40

Ex.No.9

Page 81: 237762544 Cs2405 Computer Graphics Lab Manual New

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++)

{

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

}

Page 82: 237762544 Cs2405 Computer Graphics Lab Manual New

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;

Page 83: 237762544 Cs2405 Computer Graphics Lab Manual New

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++)

{

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++)

Page 84: 237762544 Cs2405 Computer Graphics Lab Manual New

{

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:";

cin>>theta;

theta=(theta*3.14)/180;

Page 85: 237762544 Cs2405 Computer Graphics Lab Manual New

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

Page 86: 237762544 Cs2405 Computer Graphics Lab Manual New

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;

}

cleardevice();

cout<<"before rotation";

c.drawcube(x2,y2);

getch();

cleardevice();

cout<<"after rotation";

c.drawcube(x4,y4);

getch();

Page 87: 237762544 Cs2405 Computer Graphics Lab Manual New

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

Page 88: 237762544 Cs2405 Computer Graphics Lab Manual New

c.drawcube(x4,y4);

getch();

cleardevice();

break;

case 4:

exit(0);

break;

}

}

while(op!=4);

getch();

}

OUTPUT

Enter the point in the cube: 100 100 100

Enter the size of the edge: 50

Menu

1. translation

2. rotation

Page 89: 237762544 Cs2405 Computer Graphics Lab Manual New

3. scaling

4. exit

Enter the choice:1

Enter the translation vector: 5,10,15

OpenGL to Work with Visual C++

Installation

1. Install Visual C++ 2008 Express Edition (To support OpenGL).2. Copy all the .h files into the C:\Program Files\Microsoft SDKs\Windows\v6.1\Include\GL folder.

Header Files (.h files) : Gl.h, glu.h, glut.h, freeglut.h, freeglut_ext.h , freeglut_std.h

Page 90: 237762544 Cs2405 Computer Graphics Lab Manual New

3. Copy all the .lib files into the C:\Program Files\Microsoft SDKs\Windows\v6.1\Lib folder.

Library files (.lib files) : opengl32.lib, glut32.lib, glu32.lib

4. Copy all the .dll files into the C:\Windows\system32 folder.

Dynamic Link Library Files (.dll) : freeglut.dll , glut32.dll

Working with Console Application Program in OpenGL

1. Creating a Project

1. Start Visual C++ and Under the File menu select New → Project (Ctrl+Shift+N).

2. Select project types as Win32 and Win32 Console Application.3. Give a User name for the Project.4. Add a GLUT program to the project in the window.

2. Linking OpenGL Libraries

1. Under the Project menu select Project Properties (Alt+F7) at the bottom.

2. Select Configuration Properties → Select “C/C++” → “Preprocessor” → In preprocessor definitions additionally include the path where gl/glut.h is present.

Example : C:\Program Files\Microsoft \ SDKs \Windows \v6.0A \Include

3. Select "Linker" folder and click "Input" .4. Select All Configurations from the Configuration drop-down box at

the top of the dialog. This ensures you are changing the settings for both the Debug and Release configurations.

5. Select "Additional Dependencies" and type the following contents:

opengl32.lib glut32.lib glu32.lib

Page 91: 237762544 Cs2405 Computer Graphics Lab Manual New

3. Compiling the Application

Choose "Build" from the menu options.Select "Build filename".

4. Run the Application

Choose "Debug" from the menu options.Select "Start without Debugging".

6. Save the Project

Select “File Menu” → select Save all (Ctrl+Shift+S).Save all the documents before quitting.

Working with Windows Application Program in OpenGL

1. Creating a Project

1. Start Visual C++ and Under the File menu select New → Project (Ctrl+Shift+N).

2. Select Win32 Project, enter a Username, and click OK. 3. In the Wizard click Next, then in Additional options check the box

of Empty Project, and click Finish.4. Under the Project menu select Add New Item (Ctrl+Shift+A). 5. Select C++ File (.cpp), enter a Name, and click OK.6. Add a GLUT program to the project in the window.

2. Link to the OpenGL libraries:

1. Under the Project menu select Project Properties (Alt+F7) at the bottom.

2. Select Configuration Properties → Linker → Input from the navigation panel on the left.

3. Select All Configurations from the Configuration drop-down box at the top of the dialog.

4. Type “opengl32.lib glut32.lib glu32.lib” in Additional Dependencies and click OK.

1.Compiling the ApplicationChoose "Build" from the menu options.

Page 92: 237762544 Cs2405 Computer Graphics Lab Manual New

Select "Build filename".

2.Run the Application

Choose "Debug" from the menu options.

Select "Start Without Debugging".

5. Save the Project

Select “File Menu” → select Save all (Ctrl+Shift+S).

Save all the documents before quitting.

Ex.No:10

Program

#include "stdafx.h"#include <gl/glut.h>#include <stdlib.h>void init (void){ GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);

Page 93: 237762544 Cs2405 Computer Graphics Lab Manual New

glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv (GL_LIGHT0, GL_POSITION, light_position); glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); glEnable(GL_DEPTH_TEST);}void display (void){ glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix (); glRotatef (20.0, 1.0, 0.0, 0.0); glPushMatrix (); glTranslatef (-0.75, 0.5, 0.0); glRotatef (90.0, 1.0, 0.0, 0.0); glutSolidTorus (0.275, 0.85, 15, 15); glPopMatrix (); glPushMatrix (); glTranslatef (-0.75, -0.5, 0.0); glRotatef (270.0, 1.0, 0.0, 0.0); glutSolidCone (1.0, 2.0, 15, 15); glPopMatrix (); glPushMatrix (); glTranslatef (0.75, 0.0, -1.0); glutSolidSphere (1.0, 15, 15); glPopMatrix (); glPopMatrix (); glFlush ();}void reshape(int w, int h){ glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); if (w <= h) glOrtho (-2.5, 2.5, -2.5*(GLfloat)h/(GLfloat)w, 2.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0); else glOrtho (-2.5*(GLfloat)w/(GLfloat)h, 2.5*(GLfloat)w/(GLfloat)h, -2.5, 2.5, -10.0, 10.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity ();}

Page 94: 237762544 Cs2405 Computer Graphics Lab Manual New

int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (500, 500); glutCreateWindow (argv[0]); init (); glutReshapeFunc (reshape); glutDisplayFunc(display); glutMainLoop(); return 0; }

Output

Page 95: 237762544 Cs2405 Computer Graphics Lab Manual New

Ex.No:11a

Program

Page 96: 237762544 Cs2405 Computer Graphics Lab Manual New

using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Text;using System.Windows.Forms;public class Form1 : Form{PictureBox picFractal=new PictureBox();public Form1(){picFractal.BackColor = Color.White;picFractal.Dock = DockStyle.Fill;picFractal.Location = new Point(0, 0);picFractal.Name = "picFractal";picFractal.Size = new Size(241, 223);picFractal.Click += new EventHandler(picFractal_Click);Controls.Add(picFractal);Name = "tsquare";Text="Fractal T-Square";Size = new Size(241, 223);WindowState = FormWindowState.Maximized;}public void GenerateTSquare(Graphics g, int iIterations, double iLeft, double iTop, double iWidth, double iHeight, Color oColor){g.FillRectangle(new SolidBrush(oColor), (float)iLeft, (float)iTop, (float)iWidth, (float)iHeight);if (iIterations > 1){double dNewWidth = iWidth / 2.0;double dNewHeight = iHeight / 2.0;GenerateTSquare(g, iIterations - 1, iLeft - (dNewWidth / 2.0), iTop - (dNewHeight / 2.0), dNewWidth, dNewHeight, oColor);GenerateTSquare(g, iIterations - 1, iLeft + iWidth - (dNewWidth / 2.0), iTop - (dNewHeight / 2.0), dNewWidth, dNewHeight, oColor);GenerateTSquare(g, iIterations - 1, iLeft - (dNewWidth / 2.0), iTop + iHeight - (dNewHeight / 2.0), dNewWidth, dNewHeight, oColor);GenerateTSquare(g, iIterations - 1, iLeft + iWidth - (dNewWidth / 2.0), iTop + iHeight - (dNewHeight / 2.0), dNewWidth, dNewHeight, oColor);

Page 97: 237762544 Cs2405 Computer Graphics Lab Manual New

}}public void DrawTSquare(int iIterations, Color oColor){picFractal.Image = DrawTSquare(iIterations, oColor, picFractal.Width, picFractal.Height);}public Bitmap DrawTSquare(int iIterations, Color oColor, int iWidth, int iHeight){Bitmap oImage = new Bitmap(iWidth, iHeight);Graphics g = Graphics.FromImage(oImage);GenerateTSquare(g, iIterations, (double)((iWidth - 2.0) / 4.0) + 1, ((iHeight - 2.0) / 4.0) + 1, (double)(iWidth - 2.0) / 2.0, (double)(iHeight - 2.0) / 2.0, oColor);return oImage;}public Image GetImage(){return picFractal.Image;}private void picFractal_Click(object sender, EventArgs e){DrawTSquare(5, Color.Black);}public static void Main() {Application.Run(new Form1());}}

Output

Page 98: 237762544 Cs2405 Computer Graphics Lab Manual New

Ex.No:11b

Page 99: 237762544 Cs2405 Computer Graphics Lab Manual New

Program

using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Text;using System.Windows.Forms;public class Form1 : Form{PictureBox picFractal=new PictureBox();public Form1(){picFractal.BackColor = Color.Black;picFractal.Dock = DockStyle.Fill;picFractal.Location = new Point(0, 0);picFractal.Name = "picFractal";picFractal.Size = new Size(241, 223);picFractal.Click += new EventHandler(picFractal_Click);Controls.Add(picFractal);Name = "Fern";Text="Fractal Fern";Size = new Size(241, 223);WindowState = FormWindowState.Maximized;}public void GenerateFern(Graphics g, int iIterations, double dStartX, double dStartY, double dScale, double[] dA, double[] dB, double[] dC, double[] dD, int[] iRand, Color oColor){Random rnd = new Random();int iRandNum;double dX = 0;double dY = 0;double dNewX = 0;double dNewY = 0;for (int i = 0; i < iIterations; i++){iRandNum = rnd.Next(0, 100);if (iRandNum < iRand[0]){dNewX = dA[0];

Page 100: 237762544 Cs2405 Computer Graphics Lab Manual New

dNewY = dA[1] * dY;}else if (iRandNum < iRand[1]){dNewX = (dB[0] * dX) + (dB[1] * dY) + dB[2];dNewY = (dB[3] * dX) + (dB[4] * dY) + dB[5];}else if (iRandNum < iRand[2]){dNewX = (dC[0] * dX) + (dC[1] * dY) + dC[2];dNewY = (dC[3] * dX) + (dC[4] * dY) + dC[5];}else{dNewX = (dD[0] * dX) + (dD[1] * dY) + dD[2];dNewY = (dD[3] * dX) + (dD[4] * dY) + dD[5];}dX = dNewX;dY = dNewY;g.FillRectangle(new SolidBrush(oColor), (float)(dStartX + (dNewX * dScale)), (float)(dStartY - (dNewY * dScale)), 1, 1);}}public void DrawFern(int iIterations, double fScale, Color oColor){double[] dA = new double[2];double[] dB = new double[6];double[] dC = new double[6];double[] dD = new double[6];int[] iRand = new int[4];dA[0] = 0;dA[1] = 0.16;dB[0] = 0.2;dB[1] = -0.26;dB[2] = 0;dB[3] = 0.23;dB[4] = 0.22;dB[5] = 1.6;dC[0] = -0.15;dC[1] = 0.28;dC[2] = 0;dC[3] = 0.26;

Page 101: 237762544 Cs2405 Computer Graphics Lab Manual New

dC[4] = 0.25;dC[5] = 0.44;dD[0] = 0.85;dD[1] = 0.04;dD[2] = 0;dD[3] = -0.04;dD[4] = 0.85;dD[5] = 1.6;iRand[0] = 1;iRand[1] = 8;iRand[2] = 15;iRand[3] = 85;picFractal.Image = DrawFernImage(iIterations, fScale, oColor, dA, dB, dC, dD, iRand, picFractal.Width, picFractal.Height);}public void DrawFern(int iIterations, double fScale, Color oColor, double[] dA, double[] dB, double[] dC, double[] dD, int[] iRand){picFractal.Image = DrawFernImage(iIterations, fScale, oColor, dA, dB, dC, dD, iRand, picFractal.Width, picFractal.Height);}public Bitmap DrawFernImage(int iIterations, double fScale, Color oColor, double[] dA, double[] dB, double[] dC, double[] dD, int[] iRand, int iWidth, int iHeight){Bitmap oImage = new Bitmap(iWidth,iHeight);Graphics g = Graphics.FromImage(oImage);GenerateFern(g, iIterations, iWidth / 2.0, (double)iHeight, fScale, dA, dB, dC, dD, iRand, oColor);return oImage;}

public Image GetImage(){return picFractal.Image;}

private void picFractal_Click(object sender, EventArgs e){DrawFern(40000, 60.0, Color.Red);}public static void Main()

Page 102: 237762544 Cs2405 Computer Graphics Lab Manual New

{Application.Run(new Form1());}}

Page 103: 237762544 Cs2405 Computer Graphics Lab Manual New

Output :