Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Experiment No.

1
AIM: Write a program in C/C++ to draw a line in graphics mode.
CODE:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
int x, y;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
setcolor(12);//12:red
line(120, 259, 536, 389);//range(x):0..639 and range(y):0..479
getch();
return 0;
}
Experiment No.2
AIM: Write a program in C/C++ to draw a rectangle in graphics mode.
CODE:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
int gdriver = DETECT, gmode;
int l=120, t=259,r=536,b=389;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
setcolor(12);
rectangle(l,t,r,b);
getch();
return 0;
}
Experiment No.3
AIM: Write a program in C/C++ to draw a circle in graphics mode.
CODE:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
int gdriver = DETECT, gmode;
int radiusx=345,radiusy=256,radius=150;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
setcolor(12);
circle(radiusx,radiusy,radius);
getch();
return 0;
}
Experiment No.5
AIM: Write a program in C/C++ to draw a line using DDA line Algorithm.
CODE:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define ROUND(a) ((int)(a+0.5))
void lineDDA (int xa, int ya, int xb, int yb)
{int dx = xb - xa, dy = yb - ya, steps, k;
float xIncrement, yIncrement, x =xa, y =ya;
if(abs(dx)>abs(dy)) steps=abs(dx);
else
steps = abs(dy);
xIncrement = dx/(float)steps;
yIncrement = dy/(float)steps;
putpixel(ROUND(x),ROUND(y),12);
for(k=0; k<steps; k++) {
x+= xIncrement;
y+= yIncrement;
putpixel(ROUND(x),ROUND(y),12);}
}
int main(void)
{ int gdriver = DETECT, gmode, errorcode;
int l=120, t=259,r=536,b=389;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
setcolor(12);
lineDDA(l,t,r,b);
getch();
return 0;}
Experiment No.6
AIM: Write a program in C/C++ to draw a line using Bresenham’s line
Algorithm.
CODE:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define ROUND(a) ((int)(a+0.5))
void lineBres (int xa,int ya,int xb,int yb)
{
int dx,dy;
dx=abs(xa-xb);
dy=abs(ya-yb);
int p =2*dy-dx;
int twoDy,twoDyDx;t
woDy=2*dy;
twoDyDx= 2*(dy-dx);
int x,y,xEnd;
if (xa > xb){
x = xb;
y = yb;
xEnd = xa;
}
else {
x = xa;
y = ya;
xEnd = xb;
}
putpixel(x,y,12);
while(x<xEnd){
x++;
if (p<0)
p+=twoDy;
else {
y++;
p+=twoDyDx;
}
putpixel(x,y,12);
}
}
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int l=120, t=259,r=536,b=389;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
setcolor(12);
lineBres(l,t,r,b);
getch();
return 0;
}
Experiment No.7
AIM: Write a program in C/C++ to draw a circle using Midpoint circle
drawing Algorithm.
CODE:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define ROUND(a) ((int)(a+0.5))

void circlePlotPoints (int,int, int, int);

void circleMidpoint (int xCenter, int yCenter, int radius)


{
int x =0;
int y =radius;
int p = 1-radius;
circlePlotPoints (xCenter,yCenter,x,y);
while (x < y) {
x++ ;
if(p<0)
p+=2*x+1;
else {
y--;
p+=2*(x-y)+1;
}
circlePlotPoints (xCenter,yCenter,x,y);
}
}
void circlePlotPoints (int xCenter, int yCenter, int x, int y )
{
putpixel (xCenter + x, yCenter + y,12);
putpixel (xCenter - x, yCenter + y,12);
putpixel (xCenter + x, yCenter - y,12);
putpixel (xCenter - x, yCenter - y,12);
putpixel (xCenter + y, yCenter + x,12);
putpixel (xCenter - y, yCenter + x,12);
putpixel (xCenter + y, yCenter - x,12);
putpixel (xCenter - y, yCenter - x,12);
}
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int radiusx=345,radiusy=256,radius=150;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
setcolor(12);
circleMidpoint(radiusx,radiusy,radius);
getch();
return 0;
}
Experiment No.8
AIM: Write a program in C/C++ to draw a circle using Bresenham’s circle
Algorithm.
CODE:
# include<stdio.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>
void main()
{
int gd=DETECT,gm;
int r,x,y,p,xc=345,yc=256;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();
r=150;
x=0;
y=r;
putpixel(xc+x,yc-y,12);
p=3-(2*r);
for(x=0;x<=y;x++)
{
if (p<0)
{
y=y;
p=(p+(4*x)+6);
}
else
{
y=y-1;
p=p+((4*(x-y)+10));
}
putpixel(xc+x,yc-y,12);
putpixel(xc-x,yc-y,12);
putpixel(xc+x,yc+y,12);
putpixel(xc-x,yc+y,12);
putpixel(xc+y,yc-x,12);
putpixel(xc-y,yc-x,12);
putpixel(xc+y,yc+x,12);
putpixel(xc-y,yc+x,12);
}
getch();
}
Experiment No. 9
AIM: Write a program in C/C++ to show line clipping using Cohen-
Sutherland Line Clipping Algorithm.
CODE:
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
typedef unsigned int outcode;
enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };
calcode(float,float,float,float,float,float);
void lineclip(float x0,float y0,float x1,float y1,float xwmin,float ywmin,float
xwmax,float 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);
}
int calcode (float x,float y,float xwmin,float ywmin,float xwmax,float 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);
}
int main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\n\n\tEnter the co-ordinates of Line :");
printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);
printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);
printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);
cleardevice();
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
cleardevice();
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
}
Computer
Graphics
Lab
File

Prepared By:
Sameer A.Pandit
6th Semester IT
08118049
Computer
Graphics
Lab
File

Prepared By:
Shreyans Jain
6th Semester IT
08118055

You might also like