Graphics Programming

39
GRAPHICS PROGRAM /* Simple example to draw circle */ #include"graphics.h" #include"conio.h" void main() { int gd=DETECT,gm; initgraph(&gd, &gm, "c:/tc/bgi "); circle(330,180,100); getch(); closegraph(); restorecrtmode(); } Code explanation The first line to look at is: GRAPHICS.H ,this file contains definitions and explaination of all the graphic functions and constants.While GRAPHICS.LIB file contains standard graphic functions. Turbo C++ graphic functions have two categaries :Text mode graphic functions and graphic mode functions.Here we are dealing with graphic mode function.so just forget about text mode function right now.To switch from text mode to graphic mode,we have function called as ” initgraph ” . initgraph : This function initialises the graphic mode.It selects the best resolution and direct that value to mode in variable gm.The two int variables gd, gm are graphic driver and graphic mode respectively.The gm handles value that tells us which resolution and monitor we are using. The gd specifies the graphic driver to be used.In our program we have gd=DETECT means we have passed the highest possible value available for the detected driver.If you don’t want that value then you have to assign the constant value for gd,gm.The ” &” symbol is used for initgraph to pass address of the constants. Path ( ” C:\\tc\\bgi”) : It specifies the directory path where initgraph looks for graphics drivers (*.BGI) first. If files are not there then initgraph will look for the current directory of your program.If it unable to find wihtin current working directory then it will parse an error.You can leave it blank ( ” ” ) if the *.BGI files are within the working directory. 1

description

.

Transcript of Graphics Programming

Page 1: Graphics Programming

GRAPHICS PROGRAM

/* Simple example to draw circle */

#include"graphics.h"

#include"conio.h"

void main()

{

int gd=DETECT,gm;

initgraph(&gd, &gm, "c:/tc/bgi ");

circle(330,180,100);

getch();

closegraph();

restorecrtmode();

}

Code explanation

The first line to look at is: GRAPHICS.H ,this file contains definitions and explaination of all the graphic functions and constants.While GRAPHICS.LIB file contains standard graphic functions.

Turbo C++ graphic functions have two categaries :Text mode graphic functions and graphic mode functions.Here we are dealing with graphic mode function.so just forget about text mode function right now.To switch from text mode to graphic mode,we have function called as ” initgraph ” .

initgraph : This function initialises the graphic mode.It selects the best resolution and direct that value to mode in variable gm.The two int variables gd, gm are graphic driver and graphic mode respectively.The gm handles value that tells us which resolution and monitor we are using. The gd specifies the graphic driver to be used.In our program we have gd=DETECT means we have passed the highest possible value available for the detected driver.If you don’t want that value then you have to assign the constant value for gd,gm.The ” &” symbol is used for initgraph to pass address of the constants.

Path ( ” C:\\tc\\bgi”) : It specifies the directory path where initgraph looks for graphics drivers (*.BGI) first. If files are not there then initgraph will look for the current directory of your program.If it unable to find wihtin current working directory then it will parse an error.You can leave it blank ( ” ” ) if the *.BGI files are within the working directory.

Circle( ) : Circle function takes X and Y values with respect to top left corner of the screen and third co-ordinate is nothing but radius of circle.In our example we have passed X=330,Y=180 and radius equal to 100 in terms of pixels as arguments.

1

Page 2: Graphics Programming

Closegraph( ) : The closegraph() swithces back the screen from grpahics mode to text mode. If you don’t use this function then you may have undesirable effects.Here this function is called afer the getch() function as screen shouldn’t switch to text mode till user hits any key.

Restorcrtmode( ) : This mode will restore the original video mode detected by initgraph function.

getch( ) : getch( ) function gets a character from console but does not echo it on screen.This is used to pause the screen till user hits any key.

Note:

1) Make sure you have entered the correct path for the include & library directories.You can change the path by pointing your mouse to : Options > Directories.Enter the valid path for the include directory and libraries,and output directories.

2) After installation of Turbo C,you have to adjust the settings of linker.Go to Options>Linker > Libraries> and then check the ” Graphics Library“.This will help to solve the linker errors for the graphics programs.Please do not uncheck any other option already selected by compiler.

3) Graphic initialisation depends on the path mentioned in initgraph path.Be sure to enter slash between c,tc,bgi.The path C & TC depends on user if he installed TC in d: drive then it will be d,tc.Read the above code’s path carefully.

4) If you want help on specific function then point your mouse to “Help> Contents“,and then browse the content for the function you want.If you want fast-help then put the cursor on the first letter of the function or term and press CTRL+F1,it will point you to the help file of that term/function.

Function Documentation

void DrawArc ( double  r, double  start, double  sweep  

)

This procedure draws a circular arc, which always begins at the current point. The arc itself has radius r, and starts at the angle specified by the parameter start, relative to the center of the circle. This angle is measured in degrees counterclockwise from the 3 o'clock position along the x-axis, as in traditional mathematics. For example, if start is 0, the arc begins at the 3 o'clock position; if start is 90, the arc begins at the 12 o'clock position; and so on. The fraction of the circle drawn is specified by the parameter sweep, which is also measured in degrees. If sweep is 360, DrawArc draws a complete circle; if sweep is 90, it draws a quarter of a circle. If the value of sweep is positive, the arc is drawn counterclockwise from the current point. If sweep is negative, the arc is drawn clockwise from the current point. The current point at the end of the DrawArc operation is the final position of the pen along the arc.

Examples:

2

Page 3: Graphics Programming

DrawArc(r, 0, 360) Draws a circle to the left of the current point.DrawArc(r, 90, 180) Draws the left half of a semicircle starting from the

12 o'clock position. DrawArc(r, 0, 90) Draws a quarter circle from the 3 o'clock to the 12

o'clock position.DrawArc(r, 0, -90) Draws a quarter circle from the 3 o'clock to the 6

o'clock position.DrawArc(r, -90, -90) Draws a quarter circle from the 6 o'clock to the 9

o'clock position.void DrawLine ( double  dx,

double  dy  )

This procedure draws a line extending from the current point by moving the pen

dx inches in the x direction and dy inches in the y direction. The final position becomes the new current point.

double GetCurrentX (  ) 

This function returns the current x position.

double GetCurrentY (  ) 

This function returns the current y position.

double GetWindowHeight (  ) 

This function returns the height of the graphics window.

double GetWindowWidth (  ) 

This function returns the width of the graphics window.

void InitGraphics (  ) 

This procedure creates the graphics window on the screen. The call to InitGraphics must precede any calls to other functions in this package and must also precede any console output. In most cases, the InitGraphics call is the first statement in the function main.

void MovePen ( double  x, double  y  

)

This procedure moves the current point to the position (x, y), without drawing a line. The model is that of the pen being lifted off the graphics window surface and then moved to its new position.

/* Transformation */

#include <stdio.h>#include <conio.h>#include <math.h>#include <graphics.h>

3

Page 4: Graphics Programming

void main() { int i; float x,y,x0,x1,y0,y1,tx,ty; char msg[80]; int gd=DETECT,gm; clrscr(); printf("Enter the value of x0 : "); scanf("%f",&x0); printf("Enter the value of y0 : "); scanf("%f",&y0); printf("Enter the value of x1 : "); scanf("%f",&x1); printf("Enter the value of y1 : "); scanf("%f",&y1); printf("Enter the value of translation along x axis : "); scanf("%f",&tx); printf("Enter the value of translation along y axis : "); scanf("%f",&ty); x0=x0+300; y0=-y0+250; x1=x1+300; y1=-y1+250; initgraph( &gd, &gm, ""); setbkcolor(RED); line(300,0,300,600); line(0,250,800,250); sprintf(msg, "(%d,%d)",(int)(x0-300),(int)(-y0+250)); outtextxy(x0, y0, msg); line(x0,y0,x1,y1); sprintf(msg, "(%d,%d)",(int)(x1-300),(int)(-y1+250)); outtextxy(x1, y1, msg); x0=x0+tx; y0=y0-ty; x1=x1+tx; y1=y1-ty; setcolor(YELLOW); sprintf(msg, "(%d,%d)",(int)(x0-300),(int)(-y0+250)); outtextxy(x0, y0, msg); line(x0,y0,x1,y1); sprintf(msg, "(%d,%d)",(int)(x1-300),(int)(-y1+250)); outtextxy(x1, y1, msg); getch(); closegraph(); restorecrtmode();

}

/* Shearing */

#include <stdio.h>#include <conio.h>#include <math.h>

4

Page 5: Graphics Programming

#include <graphics.h>#define pi 3.1412

int x0,x1,y0,y1; void main() { float x0,y0,x1,y1,x2,y2,x3,y3,b,c; int gd=DETECT,gm; clrscr(); printf("\nEnter the value of x0 : "); scanf("%f",&x0); printf("Enter the value of y0 : "); scanf("%f",&y0); printf("Enter the value of x1 : "); scanf("%f",&x1); printf("Enter the value of y1 : "); scanf("%f",&y1); printf("Enter the value of x2 : "); scanf("%f",&x2); printf("Enter the value of y2 : "); scanf("%f",&y2); printf("Enter the value of x3 : "); scanf("%f",&x3); printf("Enter the value of y3 : "); scanf("%f",&y3); printf("\nEnter the shear along x axis : "); scanf("%f",&b); printf("\nEnter the shear along y axis : "); scanf("%f",&c); x0=x0+300; y0=-y0+250; x1=x1+300; y1=-y1+250; x2=x2+300; y2=-y2+250; x3=x3+300; y3=-y3+250; initgraph(&gd, &gm, ""); setbkcolor(RED); line(300,0,300,600); line(0,250,800,250); line(x0,y0,x1,y1); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x0,y0,x3,y3); if(c==0) { x1 = x1 + b * y1; y1 = -c * x1 + y1; x2 = x2 + b * y2; y2 = -c * x2 + y2; } if(b==0) { x2 = x2 + b * y2; y2 = -c * x2 + y2; x3 = x3 + b * y3; y3 = -c * x3 + y3; } if((b!=0)&&(c!=0)) { x1 = x1 + b * y1; y1 = -c * x1 + y1; x2 = x2 + b * y2;

5

Page 6: Graphics Programming

y2 = -c * x2 + y2; x3 = x3 + b * y3; y3 = -c * x3 + y3; } setcolor(BLUE); line(x0,y0,x1,y1); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x0,y0,x3,y3); getch(); closegraph(); restorecrtmode(); }

/* ROTATION OF A LINE */

#include <stdio.h>#include <conio.h>#include <math.h>#include <graphics.h>

6

Page 7: Graphics Programming

#define pi 3.1412

int x0,x1,y0,y1; void main() { double a,A; char msg[80]; int gd=DETECT,gm; clrscr(); printf("\nEnter the value of x0 : "); scanf("%d",&x0); printf("Enter the value of y0 : "); scanf("%d",&y0); printf("Enter the value of x1 : "); scanf("%d",&x1); printf("Enter the value of y1 : "); scanf("%d",&y1); printf("Enter the angle of rotation : "); scanf("%lf",&a); initgraph(&gd, &gm, ""); setbkcolor(BLUE); sprintf(msg, "(%d,%d)",(int)x0,(int)y0); outtextxy(x0, y0, msg); line(x0,y0,x1,y1); sprintf(msg, "(%d,%d)",(int)x1,(int)y1); outtextxy(x1, y1, msg); A=(a*pi)/180.0; x1=(double)x1*cos(A)-(double)y1*sin(A); y1=(double)x1*sin(A)+(double)y1*cos(A); x1=(int)x1; y1=(int)y1; setcolor(RED); line(x0,y0,x1,y1); sprintf(msg, "(%d,%d)",x1,y1); outtextxy(x1, y1, msg); getch(); closegraph(); restorecrtmode();}

/* DDA Line Drawing */

#include <stdio.h>#include <conio.h>#include <math.h>#include <graphics.h>

7

Page 8: Graphics Programming

void main() { int i; float x,y,x0,x1,y0,y1,len,dx,dy; int gd=DETECT,gm; char msg[80]; clrscr(); printf("Enter the value of x0 : "); scanf("%f",&x0); printf("Enter the value of y0 : "); scanf("%f",&y0); printf("Enter the value of x1 : "); scanf("%f",&x1); printf("Enter the value of y1 : "); scanf("%f",&y1); initgraph( &gd, &gm, ""); setbkcolor(RED); if(fabs(x1-x0)>fabs(y1-y0)) { len = fabs(x1-x0); } else { len = fabs(y1-y0); }

dx = (x1-x0)/len; dy = (y1-y0)/len;

x = x0; y = y0; x = floor(x + 0.5); y = floor(y + 0.5); putpixel(x,y,6); sprintf(msg, "(%d,%d)",(int)x,(int)y); outtextxy(x, y, msg); i = 1; while(i<=len) { x = x + dx; y = y + dy; putpixel(x,y,6); i++; } sprintf(msg, "(%d,%d)",(int)x,(int)y); outtextxy(x, y, msg); getch(); closegraph(); restorecrtmode(); }

/* Cohan Sutherland Line Clipping */

#include <stdio.h>#include <conio.h>#include <math.h>#include <stdlib.h>

8

Page 9: Graphics Programming

#include <graphics.h>

void main() { float x1,y1,x2,y2,x,y,x3,y3,x0,y0,m; char msg[80]; int gd=DETECT,gm; clrscr(); printf("Enter the value of x1 : "); scanf("%f",&x0); printf("Enter the value of y1 : "); scanf("%f",&y0); printf("Enter the value of x2 : "); scanf("%f",&x1); printf("Enter the value of y2 : "); scanf("%f",&y1); if((x0>x1)&&(y0>y1)) { x=x0; x0=x1; x1=x; y=y0; y0=y1; y1=y; }

m=(y1-y0)/(x1-x0); if(m<0)

exit(1); initgraph(&gd, &gm, ""); setbkcolor(RED); line(175,0,175,680); line(450,0,450,680); line(0,150,800,150); line(0,350,800,350); sprintf(msg, "(%d,%d)",(int)x0,(int)y0); outtextxy(x0, y0, msg); x1=(int)x1; y1=(int)y1; line(x0,y0,x1,y1); sprintf(msg, "(%d,%d)",(int)x1,(int)y1); outtextxy(x1, y1, msg); if(((x0<175)&&(x1<175))||((x0>450)&&(x1>450))||((y0<150)&&(y1<350))||((y0>150)&&(y1>350))) line(x0,y0,x1,y1); else if((x0<=175)&&(x1>=450))//||||||((x1>=450)&&(y0<=150)))) {

x2=175; x3=450; y2=m*(175-x0)+y0; y3=m*(450-x0)+y0;

if((x2>x3)&&(y2>y3)) { x=x2; x2=x3; x3=x; y=y2; y2=y3; y3=y; } setcolor(BLUE); line(x2,y2,x3,y3);}

9

Page 10: Graphics Programming

else if((y0<=150)&&(y1>=350)){ if(m==0) { y2=150; y3=350; x2=x0; x3=x1; } else { y2=150; y3=350; x2=1/m*(150-y0)+x0; x3=1/m*(350-y1)+x1; } if((x2>x3)&&(y2>y3)) { x=x2; x2=x3; x3=x; y=y2; y2=y3; y3=y; } setcolor(BLUE); line(x2,y2,x3,y3);}

else if((x0<=175)&&(y1>=350)){

x2=175; y3=350; y2=m*(175-x0)+y0; x3=1/m*(350-y1)+x1;}

if((x2>x3)&&(y2>y3)) { x=x2; x2=x3; x3=x; y=y2; y2=y3; y3=y; } setcolor(BLUE); line(x2,y2,x3,y3);}

else { if(x2<175)

x2=175; if(y2<150)

y2=150; if(x3>450)

x3=450; if(y3>350)

y3=350; setcolor(BLUE); line(x2,y2,x3,y3); }

getch();

10

Page 11: Graphics Programming

closegraph(); restorecrtmode(); }

/* Bresenham’s Line Drawing */

#include <conio.h>#include <stdio.h>#include <math.h>#include <graphics.h>

11

Page 12: Graphics Programming

void main() { int i,x,y,x0,x1,y0,y1,dx,dy,d; char msg[80]; int gd=DETECT,gm; clrscr(); printf("\nEnter the value of x0 : "); scanf("%d",&x0); printf("Enter the value of y0 : "); scanf("%d",&y0); printf("Enter the value of x1 : "); scanf("%d",&x1); printf("Enter the value of y1 : "); scanf("%d",&y1); initgraph(&gd, &gm, ""); setbkcolor(BLUE); dx=abs(x1-x0); dy=abs(y1-y0); d=2*dy-dx; if(x1>x0) { if(y1>y0) {

sprintf(msg, "(%d,%d)",(int)x0,(int)y0); outtextxy(x0, y0, msg); while((x0<x1)&&(y0<y1)) { if(d<0)

{ x0=x0+1; d=d+2*dy; putpixel(x0,y0,6); }

else { x0=x0+1; y0=y0+1; d=d+2*(dy-dx); putpixel(x0,y0,6); }

} sprintf(msg, "(%d,%d)",(int)x1,(int)y1); outtextxy(x1, y1, msg);

} else

{ sprintf(msg, "(%d,%d)",(int)x0,(int)y0); outtextxy(x0, y0, msg);

while((x0<x1)&&(y1<y0)) { if(d<0)

{ x0=x0+1; d=d+2*dy; putpixel(x0,y0,6); }

else { x0=x0+1; y0=y0-1; d=d+2*(dy-dx); putpixel(x0,y0,6); }

12

Page 13: Graphics Programming

} }

sprintf(msg, "(%d,%d)",(int)x1,(int)y1); outtextxy(x1, y1, msg); } else { if(y1>y0) {

sprintf(msg, "(%d,%d)",(int)x0,(int)y0); outtextxy(x0, y0, msg); while((x1<x0)&&(y0<y1)) { if(d<0)

{ x0=x0-1; d=d+2*dy; putpixel(x0,y0,6); }

else { x0=x0-1; y0=y0+1; d=d+2*(dy-dx); putpixel(x0,y0,6); }

} sprintf(msg, "(%d,%d)",(int)x1,(int)y1); outtextxy(x1, y1, msg);

} else

{ sprintf(msg, "(%d,%d)",(int)x0,(int)y0); outtextxy(x0, y0, msg); while((x1<x0)&&(y1<y0)) { if(d<0)

{ x0=x0-1; d=d+2*dy; putpixel(x0,y0,6); }

else { x0=x0-1; y0=y0-1; d=d+2*(dy-dx); putpixel(x0,y0,6); }

} }

sprintf(msg, "(%d,%d)",(int)x1,(int)y1); outtextxy(x1, y1, msg); } getch(); closegraph(); restorecrtmode();}/* Midpoint Circle Drawing */

#include <conio.h>#include <stdio.h>#include <math.h>#include <graphics.h>

13

Page 14: Graphics Programming

int x,y,xc,yc; void setpoint(); void main() { int i,d,R; char msg[80]; int gd=DETECT,gm; printf("\nEnter the value of x of centre : "); scanf("%d",&xc); printf("Enter the value of y of centre : "); scanf("%d",&yc); printf("Enter the value of Radius : "); scanf("%d",&R); x = 0; y = R; initgraph(&gd, &gm, ""); setbkcolor(CYAN); setcolor(RED); putpixel(xc,yc,4); sprintf(msg, "(%d,%d)",xc,yc); outtextxy(xc, yc, msg); d = 1 - R; while(y>x) {

if(d < 0) { x = x + 1; d = d + 2*x + 3; setpoint(); } else { x = x + 1; y = y - 1; d = d + 2*(x-y) + 5; setpoint(); }}

getch(); closegraph(); restorecrtmode();}

void setpoint() { putpixel(xc+x,yc+y,1); putpixel(xc-x,yc+y,1); putpixel(xc+x,yc-y,1); putpixel(xc-x,yc-y,1); putpixel(xc+y,yc+x,1); putpixel(xc-y,yc+x,1); putpixel(xc+y,yc-x,1); putpixel(xc-y,yc-x,1); }

/* Bressenhams Circle Drawing Algorithm */

#include <graphics.h>#include <stdlib.h>#include <stdio.h>#include <conio.h>

14

Page 15: Graphics Programming

int xc,yc,r;

void symm(int x,int y){

putpixel(x+xc,y+yc,WHITE);putpixel(-x+xc,-y+yc,WHITE);putpixel(x+xc,-y+yc,WHITE);putpixel(-x+xc,y+yc,WHITE);putpixel(y+xc,x+yc,WHITE);putpixel(-y+xc,-x+yc,WHITE);putpixel(-y+xc,x+yc,WHITE);putpixel(y+xc,-x+yc,WHITE);

}

void bhsmcircle(int xc,int yc,int r){

int x=0,y=r,dp;dp=3-2*r;while(x!=y){

if(dp<0){

dp+=4*x+6;symm(++x,y);

}else{

dp+=4*(x-y)+10;symm(++x,--y);

}}

}

void main(){

int gdriver = DETECT, gmode;initgraph(&gdriver, &gmode, "");

printf("Enter the centre co-ordinates: ");scanf("%d %d",&xc,&yc);printf("Enter the radius: ");scanf("%d",&r);bhsmcircle(xc,yc,r);getch();

}

/* Bressenhams Line Drawing Algorithm */

#include <graphics.h>#include <stdio.h>#include <math.h>#include <conio.h>

15

Page 16: Graphics Programming

void bshmLine(int x1,int y1,int x2,int y2){

int x=x1,y=y1,dx,dy,s1,s2;int length,i,dp,temp,swap=0;

putpixel(x1,y1,WHITE);dx=abs(x2-x1);dy=abs(y2-y1);

if(x2<x1) s1=-1;else if(x2>x1) s1=1;else s1=0;

if(y2<y1) s2=-1;else if(y2>y1) s2=1;else s2=0;

dp=2*dy-dx;if(dy>dx){

temp=dx;dx=dy;dy=temp;swap=1;

}for(i=1;i<=dx;i++){

if(dp<0){

if(swap) putpixel(x,y=y+s2,WHITE); else putpixel(x=x+s1,y,WHITE); dp+=2*dy;

}else{

putpixel(x=x+s1,y=y+s2,WHITE);dp=dp+2*dy-2*dx;

}}

}

void main(){

int x1,y1,x2,y2;int gdriver = DETECT, gmode;initgraph(&gdriver, &gmode, "");

printf("Enter the starting co-ordinates: ");scanf("%d %d",&x1,&y1);printf("Enter the ending co-ordinates: ");scanf("%d %d",&x2,&y2);

bshmLine(x1,y1,x2,y2);getch();

}

/* DDA Line Drawing Algorithm */

#include <graphics.h>#include <stdio.h>#include <math.h>#include <conio.h>

16

Page 17: Graphics Programming

void ddaLine(int x1,int y1,int x2,int y2){

float x=x1,y=y1,dx,dy;int length,i;

putpixel(x1,y1,WHITE);

if(abs(x2-x1)>=abs(y2-y1))length=abs(x2-x1);

elselength=abs(y2-y1);

dx=(float)(x2-x1)/length;dy=(float)(y2-y1)/length;for(i=1;i<=length;i++){

x=x+dx;y=y+dy;putpixel((int)x,(int)y,WHITE);

}}

void main(){

int x1,y1,x2,y2;int gdriver = DETECT, gmode;initgraph(&gdriver, &gmode, "");

printf("Enter the starting co-ordinates: ");scanf("%d %d",&x1,&y1);printf("Enter the ending co-ordinates: ");scanf("%d %d",&x2,&y2);

ddaLine(x1,y1,x2,y2);getch();

}

/* Line clipping using cohen Sutherland algorithm */

#include<stdio.h>#include<graphics.h>

typedef unsigned int outcode;enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };

17

Page 18: Graphics Programming

void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;{

int gd,gm; outcode code0,code1,codeout; int accept = 0, done=0;

code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax); code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);

do{if(!(code0 | code1)){ accept =1 ; done =1; }else if(code0 & code1) done = 1; else { float x,y; codeout = code0 ? code0 : code1; if(codeout & TOP) {

x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);y = ywmax;

} else if( codeout & BOTTOM) {

x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0); y = ywmin;

} else

if ( codeout & RIGHT){ y = y0+(y1-y0)*(xwmax-x0)/(x1-x0); x = xwmax;}else{ y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0); x = xwmin; }

if( codeout == code0) { x0 = x; y0 = y; code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax); } else { x1 = x; y1 = y; code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax); }

} } while( done == 0);

if(accept) line(x0,y0,x1,y1);

rectangle(xwmin,ywmin,xwmax,ywmax);

getch();

}/*--------------------------------------------------------------------*/

18

Page 19: Graphics Programming

int calcode (x,y,xwmin,ywmin,xwmax,ywmax) float x,y,xwmin,ywmin,xwmax,ywmax; { int code =0;

if(y> ywmax)code |=TOP;

else if( y<ywmin)code |= BOTTOM;

else if(x > xwmax)code |= RIGHT;

else if ( x< xwmin)code |= LEFT;

return(code);}

/*-------------------------------------------------*/

main(){

float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;int gd,gm;

detectgraph(&gd,&gm);initgraph(&gd,&gm,"C:\\TC\\BGI");

printf("\n\n\tEnter the co-ordinates of Line :");

printf("\n\n\tX1 Y1 : ");scanf("%f %f",&x1,&y1);

printf("\n\n\tX2 Y2 : ");scanf("%f %f",&x2,&y2);

printf("\n\tEnter the co_ordinates of window :\n ");printf("\n\txwmin , ywmin : ");scanf("%f %f",&xwmin,&ywmin);printf("\n\txwmax , ywmax : ");scanf("%f %f",&xwmax,&ywmax);

line(x1,y1,x2,y2);rectangle(xwmin,ywmin,xwmax,ywmax);getch();cleardevice();

lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );getch();closegraph();

}

/* Flood fill algo. using user defined function */

#include<stdio.h>#include<graphics.h>#include<dos.h>

19

Page 20: Graphics Programming

void fill_right(x,y)int x , y ;{ if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED)) {

putpixel(x,y,RED);fill_right(++x,y);x = x - 1 ;fill_right(x,y-1);fill_right(x,y+1);

}}

void fill_left(x,y)int x , y ;{ if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED)) {

putpixel(x,y,RED);

fill_left(--x,y);x = x + 1 ;fill_left(x,y-1);fill_left(x,y+1);

}}

/*------------------------------------------------------*/

void main(){

int x , y ,a[10][10];int gd, gm ,n,i;

detectgraph(&gd,&gm);initgraph(&gd,&gm," ");

printf("\n\n\tEnter the no. of edges of polygon : ");scanf("%d",&n);printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");

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

printf("\tX%d Y%d : ",i,i);scanf("%d %d",&a[i][0],&a[i][1]);

}

a[n][0]=a[0][0];a[n][1]=a[0][1];

printf("\n\n\tEnter the seed pt. : ");scanf("%d%d",&x,&y);

cleardevice();setcolor(WHITE);

for(i=0;i<n;i++) /*- draw poly -*/{ line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);}

fill_right(x,y);

20

Page 21: Graphics Programming

fill_left(x-1,y);

getch();}

/* Program to implement Ellipse Drawing Algorithm */

#include<iostream.h>#include<graphics.h>#include<conio.h>#include<math.h>

21

Page 22: Graphics Programming

#include<dos.h>#include<stdlib.h>#include<stdio.h>class myCircle

{private:int x,y,r,d,x1,y1,minor,major,dtheta,ratio,a,b,x2,y2;public:myCircle(); //Constructorvoid showCircle();};

myCircle::myCircle(){x=0;y=0;cout<<"

"Enter The Major & Minor Axis Of Ellipse ":=";cin>>major>>minor;cout<<"

"Enter The Center Of The Ellipse ":=";cin>>x>>y;}

void myCircle::showCircle(){char *s;

int ax,ay; float ar; x1=1; ratio=major/minor; getaspectratio(&ax,&ay);

ar=1;// ar=ay/ax;

while(x1<=major){y1=minor*sqrt((1-(x1*x1/major*major)));putpixel((x+x1*ar+320),(y+y1+240),5);putpixel((x+x1*ar+320),(y-y1+240),5);putpixel((x-x1*ar+320),(y-y1+240),5);putpixel((x-x1*ar+320),(y+y1+240),5);

dtheta=1/sqrt(x1*x1+y1*y1);// x2=x1+cos(dtheta)-ratio*y1*sin(dtheta);// y2=y1+cos(dtheta)+1/ratio*x1*sin(dtheta); x1++;// y1=y2; }

setcolor(5);outtextxy(318+x,235+y,".");setcolor(15);sprintf(s,"Center(%d,%d)",x,y);outtextxy(20,10,"The Center Is At");outtextxy(20,20,s);sprintf(s,"Radius=%d",r);outtextxy(20,30,s);getch();}

void main(){int gd=DETECT,gm,i,j,xx=190,xxx=480;clrscr();myCircle a;char *mess[]={"B","R","E","S","E","N","H","A","M","'","S","

","C","I","R","C","L","E"," ","A","L","G","O","R","I","T","H","M"};initgraph(&gd,&gm,"..\bgi");

22

Page 23: Graphics Programming

cleardevice();rectangle(120,40,320,240);rectangle(320,40,520,240);rectangle(120,240,320,440);rectangle(320,240,520,440);for(i=0,j=27;i<16,j>=14;i++,j--)

{xx+=10;outtextxy(xx,10,mess[i]);xxx-=10;outtextxy(xxx,10,mess[j]);delay(100);}

for(i=130;i<=510;i+=10)for(j=50;j<=430;j+=10)putpixel(i,j,15);

for(i=130;i<=510;i+=10){if(i==320)continue;outtextxy(i,237,"+");}

for(i=50;i<=430;i+=10){if(i==240)continue;outtextxy(317,i,"-");}

outtextxy(310,230,"O");outtextxy(530,240,"X");outtextxy(320,450,"-Y");outtextxy(100,240,"-X");outtextxy(320,30,"Y");a.showCircle();}

/* Program to implement 2-D transformations */

#include <iostream.h>#include <stdlib.h>#include <graphics.h>#include <stdio.h>

23

Page 24: Graphics Programming

#include <math.h>#include <conio.h>#define PI 3.14

float cord[10][3];int n;

void myclrscr(){

int gdriver = DETECT, gmode;initgraph(&gdriver, &gmode, "");

}

void getPoints(){

int i;printf("Enter the no of defining vertices: ");scanf("%d",&n);for(i=0;i<n;i++){

printf("Enter coordinate %d: ",i+1);scanf("%f %f",&cord[i][0],&cord[i][1]);cord[i][2]=1;

}}

void drawPolygon(){

int i;for(i=0;i<n-1;i++)

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

}

void mul(float mat1[][3],float m1,int n,float mat2[][3],int m2,float ans[][3]){

int i,j,k;float sum;

for(i=0;i<m1;i++)for(j=0;j<m2;j++){

sum=0;for(k=0;k<n;k++)

sum+=mat1[i][k]*mat2[k][j];ans[i][j]=sum;

}}

void translate(int tx,int ty,int draw){

int i;float tMat[3][3]={ 1,0,0,

0,1,0, 0,0,1 };

tMat[2][0]=tx;tMat[2][1]=ty;

mul(cord,n,3,tMat,3,cord);

if(draw) drawPolygon();}

24

Page 25: Graphics Programming

void scale(float sx,float sy,int xf,int yf){

int i;float tMat[3][3]={1,0,0,

0,1,0, 0,0,1 };

tMat[0][0]=sx;tMat[1][1]=sy;tMat[2][0]=xf-sx*xf;tMat[2][1]=yf-sy*yf;

mul(cord,n,3,tMat,3,cord);

drawPolygon();}

void rotate(float angle,int xf,int yf){

int i;float tMat[3][3]={1,0,0,

0,1,0, 0,0,1 };

angle*=PI/180;translate(-xf,-yf,0);tMat[0][0]=cos(angle);tMat[0][1]=sin(angle);tMat[1][0]=-sin(angle);tMat[1][1]=cos(angle);

mul(cord,n,3,tMat,3,cord);

translate(xf,yf,0);drawPolygon();

}

void main(){

float xf,yf,sx,sy,tx,ty,r;int gdriver = DETECT, gmode;initgraph(&gdriver, &gmode, "e:\\proggy\\tc\\bgi");

getPoints();drawPolygon();getch();myclrscr();cout<<"1- Translation"<<"\n";cout<<"2- Scaling"<<"\n";cout<<"3- Rotation "<<"\n";cout<<"0- Exit"<<"\n";int choice;while(1){

cout<<"Enter your choice: ";cin>>choice;switch(choice){

case 1 : printf("Enter the translating co-ordinates:"); scanf("%f %f",&tx,&ty); myclrscr(); translate(tx,ty,1); break;

25

Page 26: Graphics Programming

case 2 : printf("Enter the Scaling factors:"); scanf("%f %f",&sx,&sy); printf("Enter the refrence co-ordinates: "); scanf("%f %f",&xf,&yf); myclrscr(); scale(sx,sy,xf,yf); break;

case 3: printf("Enter the Rotating angle :"); scanf("%f",&r); printf("Enter the refrence co-ordinates: "); scanf("%f %f",&xf,&yf); myclrscr(); rotate(r,xf,yf); break;

case 0 : exit(0);getch();

}}getch();

}

/* Program for 3-D Transformation */

#include<iostream.h>#include<dos.h>#include<stdio.h>#include<math.h>

26

Page 27: Graphics Programming

#include<conio.h>#include<graphics.h>#include<process.h>

int gd=DETECT,gm;double x1,x2,y1,y2;

void show_message(){char *mess[]={"-","=","["," ","3","D","-","T","r","a","n","s",

"f","o","r","m","a","t","i","o","n"," ","]","=","-"};int xx=28,xxx=52,i,j;_setcursortype(_NOCURSOR);for(i=0,j=24;i<15,j>=12;i++,j--)

{gotoxy(xx,1);cout<<mess[i];xx++;gotoxy(xxx,1);cout<<mess[j];xxx--;delay(50);}

_setcursortype(_NORMALCURSOR);}

void draw_cube(double edge[20][3]) { initgraph(&gd,&gm,"..\bgi");

int i;clearviewport();for(i=0;i<19;i++) { x1=edge[i][0]+edge[i][2]*(cos(2.3562)); y1=edge[i][1]-edge[i][2]*(sin(2.3562)); x2=edge[i+1][0]+edge[i+1][2]*(cos(2.3562)); y2=edge[i+1][1]-edge[i+1][2]*(sin(2.3562));

line(x1+320,240-y1,x2+320,240-y2); }line(320,240,320,25);line(320,240,550,240);line(320,240,150,410);getch();

closegraph(); }

void scale(double edge[20][3]) {

double a,b,c;int i;cout<<"

" Enter The Scaling Factors ":=";cin>>a>>b>>c;

initgraph(&gd,&gm,"..\bgi");clearviewport();

for(i=0;i<20;i++){edge[i][0]=edge[i][0]*a;edge[i][1]=edge[i][1]*b;edge[i][2]=edge[i][2]*c;}

draw_cube(edge); closegraph();

27

Page 28: Graphics Programming

}

void translate(double edge[20][3]) { int a,b,c; int i;

cout<<"" Enter The Translation Factors ":=";

cin>>a>>b>>c; initgraph(&gd,&gm,"..\bgi");

clearviewport(); for(i=0;i<20;i++)

{edge[i][0]+=a;edge[i][0]+=b;edge[i][0]+=c;

} draw_cube(edge); closegraph(); }

void rotate(double edge[20][3]) { int ch; int i; double temp,theta,temp1; clrscr(); cout<<"

-=[ Rotation About ]=-"; cout<<"

1:==>" X-Axis "";cout<<"

2:==>" Y-Axis ""; cout<<"

3:==>" Z-Axis ""; cout<<"

" Enter Your Choice ":=";cin>>ch;switch(ch) { case 1:

cout<<"" Enter The Angle ":=";

cin>>theta;theta=(theta*3.14)/180;for(i=0;i<20;i++) { edge[i][0]=edge[i][0]; temp=edge[i][1]; temp1=edge[i][2]; edge[i][1]=temp*cos(theta)-temp1*sin(theta); edge[i][2]=temp*sin(theta)+temp1*cos(theta); }draw_cube(edge);break;

case 2:cout<<"

" Enter The Angle ":=";cin>>theta;theta=(theta*3.14)/180;for(i=0;i<20;i++)

28

Page 29: Graphics Programming

{ edge[i][1]=edge[i][1]; temp=edge[i][0]; temp1=edge[i][2]; edge[i][0]=temp*cos(theta)+temp1*sin(theta); edge[i][2]=-temp*sin(theta)+temp1*cos(theta); }draw_cube(edge);break;

case 3:cout<<"

" Enter The Angle ":=";cin>>theta;theta=(theta*3.14)/180;for(i=0;i<20;i++) { edge[i][2]=edge[i][2]; temp=edge[i][0]; temp1=edge[i][1]; edge[i][0]=temp*cos(theta)-temp1*sin(theta); edge[i][1]=temp*sin(theta)+temp1*cos(theta); }draw_cube(edge);break;

}}

void reflect(double edge[20][3]){

int ch; int i; clrscr(); cout<<"

-=[ Reflection About ]=-"; cout<<"

1:==>" X-Axis "";cout<<"

2:==>" Y-Axis ""; cout<<"

3:==>" Z-Axis ""; cout<<"

" Enter Your Choice ":="; cin>>ch; switch(ch)

{ case 1:

for(i=0;i<20;i++) { edge[i][0]=edge[i][0]; edge[i][1]=-edge[i][1]; edge[i][2]=-edge[i][2]; }draw_cube(edge);break;

case 2: for(i=0;i<20;i++)

{ edge[i][1]=edge[i][1]; edge[i][0]=-edge[i][0]; edge[i][2]=-edge[i][2];

29

Page 30: Graphics Programming

}draw_cube(edge);break;

case 3: for(i=0;i<20;i++)

{ edge[i][2]=edge[i][2]; edge[i][0]=-edge[i][0]; edge[i][1]=-edge[i][1]; }draw_cube(edge);break;

}}

void perspect(double edge[20][3]){

int ch; int i; double p,q,r; clrscr(); cout<<"

-=[ Perspective Projection About ]=-"; cout<<"

1:==>" X-Axis "";cout<<"

2:==>" Y-Axis ""; cout<<"

3:==>" Z-Axis ""; cout<<"

" Enter Your Choice ":=";cin>>ch;switch(ch) { case 1:

cout<<"" Enter P ":=";

cin>>p;for(i=0;i<20;i++) { edge[i][0]=edge[i][0]/(p*edge[i][0]+1); edge[i][1]=edge[i][1]/(p*edge[i][0]+1); edge[i][2]=edge[i][2]/(p*edge[i][0]+1); }draw_cube(edge); break;

case 2:cout<<"

" Enter Q ":=";cin>>q;for(i=0;i<20;i++) { edge[i][1]=edge[i][1]/(edge[i][1]*q+1); edge[i][0]=edge[i][0]/(edge[i][1]*q+1); edge[i][2]=edge[i][2]/(edge[i][1]*q+1); }draw_cube(edge);break;

case 3:

30

Page 31: Graphics Programming

cout<<"" Enter R ":=";

cin>>r;for(i=0;i<20;i++) { edge[i][2]=edge[i][2]/(edge[i][2]*r+1); edge[i][0]=edge[i][0]/(edge[i][2]*r+1); edge[i][1]=edge[i][1]/(edge[i][2]*r+1); }draw_cube(edge);break;

} closegraph();

}

void main(){

int choice; double edge[20][3]=

{100,0,0,100,100,0,0,100,0,0,100,100,0,0,100,0,0,0,100,0,0,100,0,100,100,75,100,75,100,100,100,100,75,100,100,0,100,100,75,100,75,100,75,100,100,0,100,100,0,100,0,0,0,0,0,0,100,100,0,100

}; while(1) {

clrscr(); show_message();

cout<<"

1:==>" Draw Cube "";cout<<"2:==>" Scaling "";cout<<"3:==>" Rotation "";cout<<"4:==>" Reflection "";cout<<"5:==>" Translation "";cout<<"6:==>" Perspective Projection "";cout<<"7:==>" Exit "";cout<<"

" Enter Your Choice ":=";

31

Page 32: Graphics Programming

cin>>choice;switch(choice)

{ case 1:

draw_cube(edge);break;

case 2:scale(edge);break;

case 3:rotate(edge);break;

case 4:reflect(edge);break;

case 5:translate(edge);break;

case 6: perspect(edge);

break;

case 7:exit(0);

default: cout<<"

a" Press A Valid Key...!!! ""; getch(); break;

} closegraph();

}}

32