Transcript
Page 1: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

CS2405 - COMPUTER GRAPHICS LAB

1. Output primitives. 2. Implementation of Bresenhams Algorithm – Line, Circle, Ellipse.

3. Implementation of Line, Circle and ellipse Attributes 4. Two Dimensional transformations - Translation, Rotation, Scaling,

Reflection, Shear.

5. Composite 2D Transformations 6. Cohen Sutherland 2D line clipping and Windowing

7. Sutherland – Hodgeman Polygon clipping Algorithm 8. Three dimensional transformations - Translation, Rotation, Scaling

9. Composite 3D transformations 10. Drawing three dimensional objects and Scenes

11. Generating Fractal images

Page 2: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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 3: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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 4: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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 5: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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 6: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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 7: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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 8: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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 9: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

Page 10: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

}

}

Page 11: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Output:

Page 12: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex.No:2a

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

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

}

Page 13: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Output

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

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

Page 14: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex.No:2b

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)

{

Page 15: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

x++;

if (p<0) p=p+twody;

else {

y=y+1; p=p+twodydx;

}

putpixel(x,y,RED); }

getch(); closegraph();

}

Page 16: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Output

Enter the x-coordinate of the starting point : 100

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

Enter the y-coordinate of the ending point : 200

Page 17: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex.No:2c

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

Page 18: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

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

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

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

Page 19: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Output

Enter the x-coordinate of the centre point : 100

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

Page 20: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex.No:2d

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

Page 21: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

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

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

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

Page 22: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Output

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

Enter the radius : 50

Page 23: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex.No:2e

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

{

Page 24: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

y--;

py=py-tworx2; 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 25: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Output

Enter the x-coordinate of the centre point : 100

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

Enter the y-coordinate of the radius : 60

Page 26: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex.No:2f

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;

Page 27: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

d1=d1+dx+ry2;

} 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 28: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

}

Output

Enter the x-coordinate of the centre point : 100

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

Enter the y-coordinate of the radius : 60

Page 29: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex. No. 3a :

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

Page 30: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

getch();

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

}

}

Page 31: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

OUTPUT

******Line Styles******* 1.Solid Line

2.Dotted Line

3.Center Line 4.Dashed Line

5.Userbit Line 6.Exit

Enter Ur Choice: 1

Solid Line

Entrer ur Choice: 2

Dotted Line

……………………..

Page 32: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Enter ur choice : 3

Enter ur Choicre: 6

Page 33: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex.No:3b

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

Page 34: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

setfillstyle(SOLID_FILL, RED);

circle(100, 100, 50); 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();

Page 35: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

cleardevice();closegraph();

break; case 7:

exit(0); }

} }

Page 36: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

OUTPUT

"******Circle Attributes*******

1.Empty Fill 2.Soild Fill

3.Line Fill 4.Wide dot Fill

5.close dot Fill

6.User Fill 7.Exit

Enter your choice: 1

Enter Ur Choice:2

Page 37: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Enter Ur Choice:3

Entrer ur Choice:6

Page 38: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex.No:3c

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

Page 39: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

floodfill(100, 100, WHITE);

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

Page 40: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

break;

case 7: exit(0);

} }

}

Page 41: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

OUTPUT:

"******Ellipse Attributes******* 1.Empty Fill

2.Soild Fill 3.Line Fill

4.Wide dot Fill 5.close dot Fill

6.User Fill

7.Exit

Entrer ur choice: 1

Page 42: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Entrer ur choice: 2

Enter ur choice : 6

Page 43: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex.No.4

Program

#include<stdio.h>

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

Page 44: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

printf("3.scaling\n");

printf("4.shearing\n");

printf("5.reflection\n");

printf("6.exit\n");

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

Page 45: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

break;

case 5:

input();

reflection();

break;

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

Page 46: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

{

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

}

}

void translation()

{

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

Page 47: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

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

Page 48: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

}

output();

delay(10);

menu();

}

void shearing()

{

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

{

Page 49: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

}

}

output();

delay(10);

menu();

}

void reflection()

{

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;

Page 50: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

menu();

getch();

}

Page 51: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

OUTPUT

Menu

1. Translation

2. Rotation 3. Scaling

4. Shearing 5. Reflection

6. Exit

TRANSLATION

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

Page 52: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Enter the translation vector Tx, Ty : 90 60

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

Page 53: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Enter the Rotating Angle : 90

Enter the Pivot Point : 100 200

`

Page 54: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Enter the scaling Factor : 0.3 0.4

Enter the Fixed Point : 100 200

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

Page 56: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

}

Page 59: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

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

Page 60: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Output

Page 62: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex.No:6

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 1

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

Page 63: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

}

void swappts(struct point *p1,struct point *p2) {

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)

{

Page 64: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

p1.y+=(winmin.x-p1.x)*m;

p1.x=winmin.x; }

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

Page 65: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

getch();

clrscr(); cleardevice();

printf("After Clipping"); rectangle(winmin.x,winmax.y,winmax.x,winmin.y);

clipline(winmin,winmax,p1,p2); getch();

}

Page 66: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

OUTPUT:

Enter the clip window coordinates : 100 100 200 200

Enter the line coordinates : 0 0 500 500

Before clipping

After clipping

Page 67: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex.No:7

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 0

struct 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) {

Page 68: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

if(inside(p1,b,wmin,wmax)==inside(p2,b,wmin,wmax))

return (FALSE); else

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

else ipt.x=p2.x;

break; case top:

ipt.y=wmax.y; if(p1.x!=p2.x)

ipt.x=p2.x+(wmax.y-p2.y)/m;

else ipt.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])

Page 69: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

first[b]=&p;

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

Page 70: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

{

struct point *first[N_EDGE]={0,0,0,0},s[N_EDGE]; 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(); }

Page 71: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Output

Enter the window minimum coordinates

200

200

Enter the window max coordinates

400

400

Enter the no of sides in polygon

3

Enter the coordinates(x,y)for pin ,pout

150

300

250

175

250

175

350

410

350

410

150

300

Page 72: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex.No:8

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

Page 73: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

}

OUTPUT:

3D TRANSFOTMATIONS

Enter x ,y values:350,250

Page 74: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Rotation:

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

Page 75: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

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

}

Page 76: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

OUTPUT:

Enter the rotation angle: 60

After rotation about z-axis

Page 77: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

After rotation about y-axis

After rotation about x-axis

Page 78: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Scaling

#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 79: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

OUTPUT:

Original image:

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

Page 80: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex.No.9

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

Page 81: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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;

Page 82: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

{

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:

Page 83: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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;

Page 84: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

case 2:

cout<<"enter the rotation angle:";

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

Page 85: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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;

}

cleardevice();

cout<<"before rotation";

c.drawcube(x2,y2);

Page 86: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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;

}

Page 87: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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;

}

}

while(op!=4);

getch();

}

Page 88: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

OUTPUT

Enter the point in the cube: 100 100 100

Enter the size of the edge: 50

Menu

1. translation

2. rotation

3. scaling

4. exit

Enter the choice:1

Enter the translation vector: 5,10,15

Page 89: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

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.

Page 90: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

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.

Page 91: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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 Application

Choose "Build" from the menu options. 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.

Page 92: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

Page 93: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

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

}

Page 94: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Output

Page 95: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex.No:11a

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

Page 96: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

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

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

} }

Page 97: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Output

Page 98: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Ex.No:11b

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

Page 99: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

{

dNewX = dA[0]; 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;

Page 100: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

dC[2] = 0;

dC[3] = 0.26; 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);

Page 101: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

}

public static void Main() {

Application.Run(new Form1()); }

}

Page 102: CS2405 - COMPUTER GRAPHICS LAB - …chettinadtech.ac.in/storage/14-06-30/14-06-30-13-19-59-2629-sakthi...CS2405 - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation

Output :


Recommended