Lab Manual CG
Progam no. 5
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void main()
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
int sx,sy,xt,yt,r;
float t;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
printf("\t Program for basic transactions");
printf("\n\t Enter the points of triangle");
setcolor(1);
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
printf("\n 1.Transaction\n 2.Rotation\n 3.Scalling\n 4.exit");
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
case 1:
printf("\n Enter the translation factor");
scanf("%d%d",&xt,&yt);
nx1=x1+xt;
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
nx3=x3+xt;
ny3=y3+yt;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
case 2:
printf("\n Enter the angle of rotation");
scanf("%d",&r);
t=3.14*r/180;
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(x1*sin(t)+y1*cos(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(x2*sin(t)+y2*cos(t));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(x3*sin(t)+y3*cos(t));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
case 3:
printf("\n Enter the scalling factor");
scanf("%d%d",&sx,&sy);
nx1=x1*sx;
ny1=y2*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
case 4:
break;
default:
printf("Enter the correct choice");
closegraph();
}
Program no 7
// C++ program to implement Cohen Sutherland algorithm
// for line clipping.
#include <iostream.h>
// Defining region codes
const int INSIDE = 0; // 0000
const int LEFT = 1; // 0001
const int RIGHT = 2; // 0010
const int BOTTOM = 4; // 0100
const int TOP = 8; // 1000
// Defining x_max, y_max and x_min, y_min for
// clipping rectangle. Since diagonal points are
// enough to define a rectangle
const int x_max = 10;
const int y_max = 8;
const int x_min = 4;
const int y_min = 4;
// Function to compute region code for a point(x, y)
int computeCode(double x, double y)
// initialized as being inside
int code = INSIDE;
if (x < x_min) // to the left of rectangle
code |= LEFT;
else if (x > x_max) // to the right of rectangle
code |= RIGHT;
if (y < y_min) // below the rectangle
code |= BOTTOM;
else if (y > y_max) // above the rectangle
code |= TOP;
return code;
// Implementing Cohen-Sutherland algorithm
// Clipping a line from P1 = (x2, y2) to P2 = (x2, y2)
void cohenSutherlandClip(double x1, double y1,
double x2, double y2)
// Compute region codes for P1, P2
int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
// Initialize line as outside the rectangular window
int accept = 0;
while (true)
if ((code1 == 0) && (code2 == 0))
// If both endpoints lie within rectangle
accept = 1;
break;
else if (code1 & code2)
// If both endpoints are outside rectangle,
// in same region
break;
else
// Some segment of line lies within the
// rectangle
int code_out;
double x, y;
// At least one endpoint is outside the
// rectangle, pick it.
if (code1 != 0)
code_out = code1;
else
code_out = code2;
// Find intersection point;
// using formulas y = y1 + slope * (x - x1),
// x = x1 + (1 / slope) * (y - y1)
if (code_out & TOP)
// point is above the clip rectangle
x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);
y = y_max;
else if (code_out & BOTTOM)
// point is below the rectangle
x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);
y = y_min;
else if (code_out & RIGHT)
// point is to the right of rectangle
y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);
x = x_max;
else if (code_out & LEFT)
// point is to the left of rectangle
y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
x = x_min;
// Now intersection point x,y is found
// We replace point outside rectangle
// by intersection point
if (code_out == code1)
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
}
else
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
if (accept==1)
cout <<"Line accepted from " << x1 << ", "
<< y1 << " to "<< x2 << ", " << y2 << endl;
// Here the user can add code to display the rectangle
// along with the accepted (portion of) lines
else
cout << "Line rejected" << endl;
// Driver code
int main()
// First Line segment
// P11 = (5, 5), P12 = (7, 7)
cohenSutherlandClip(5, 5, 7, 7);
// Second Line segment
// P21 = (7, 9), P22 = (11, 4)
cohenSutherlandClip(7, 9, 11, 4);
// Third Line segment
// P31 = (1, 5), P32 = (4, 1)
cohenSutherlandClip(1, 5, 4, 1);
return 0;
}
Program no. 8
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void boundaryfill(int x,int y,int fill,int boundary)
{ int current;
current=getpixel(x,y);
if((current!=boundary)&&(current!=fill))
setcolor(fill);
putpixel(x,y,fill);
delay(5);
boundaryfill(x+1,y,fill,boundary);
boundaryfill(x-1,y,fill,boundary);
boundaryfill(x,y+1,fill,boundary);
boundaryfill(x,y-1,fill,boundary);
void main()
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
setcolor(10);
rectangle(250,200,310,260);
boundaryfill(280,250,12,10);
getch();
}
Program no. 10
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);
enum { TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
float xmin,xmax,ymin,ymax;
void clip(float x0,float y0,float x1,float y1)
outcode outcode0,outcode1,outcodeOut;
int accept = FALSE,done = FALSE;
outcode0 = CompOutCode(x0,y0);
outcode1 = CompOutCode(x1,y1);
do
if(!(outcode0|outcode1))
accept = TRUE;
done = TRUE;
}
else
if(outcode0 & outcode1)
done = TRUE;
else
float x,y;
outcodeOut = outcode0?outcode0:outcode1;
if(outcodeOut & TOP)
x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
else
if(outcodeOut & BOTTOM)
x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
else
if(outcodeOut & RIGHT)
y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
else
y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
if(outcodeOut==outcode0)
x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0);
else
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(150,20,"POLYGON AFTER CLIPPING");
rectangle(xmin,ymin,xmax,ymax);
outcode CompOutCode(float x,float y)
outcode code = 0;
if(y>ymax)
code|=TOP;
else
if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else
if(x<xmin)
code|=LEFT;
return code;
void main( )
float x1,y1,x2,y2;
/* request auto detection */
int gdriver = DETECT, gmode, n,poly[14],i;
clrscr( );
printf("Enter the no of sides of polygon:");
scanf("%d",&n);
printf("\nEnter the coordinates of polygon\n");
for(i=0;i<2*n;i++)
scanf("%d",&poly[i]);
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\Turboc3\\bgi");
outtextxy(150,20,"POLYGON BEFORE CLIPPING");
drawpoly(n+1,poly);
rectangle(xmin,ymin,xmax,ymax);
getch( );
cleardevice( );
for(i=0;i<n;i++)
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
getch( );
restorecrtmode( );
Input & output
Enter the no of sides of polygon:5
Enter the coordinates of polygon
50
50
200
100
350
350
80
200
40
80
Enter the rectangular coordinates of clipping window
150
150
300
300*/
Program 9
Aim: Draw Hut using C
#include<graphics.h>
#include<conio.h>
int main(){
int gd = DETECT,gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
/* Draw Hut */
setcolor(WHITE);
rectangle(150,180,250,300);
rectangle(250,180,420,300);
rectangle(180,250,220,300);
line(200,100,150,180);
line(200,100,250,180);
line(200,100,370,100);
line(370,100,420,180);
/* Fill colours */
setfillstyle(SOLID_FILL, BROWN);
floodfill(152, 182, WHITE);
floodfill(252, 182, WHITE);
setfillstyle(SLASH_FILL, BLUE);
floodfill(182, 252, WHITE);
setfillstyle(HATCH_FILL, GREEN);
floodfill(200, 105, WHITE);
floodfill(210, 105, WHITE);
getch();
closegraph();
return 0;
}
Program no 12
DDA algorithm coding for printing a triangle
/* Program in c for printing a triangle with the help of DDA aglorithm */
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void drawline(int x1,int y1,int x2,int y2)
{
int dx,dy,m,s;
float xi,yi,x,y;
dx = x2 - x1;
dy = y2 - y1;
if (abs(dx) > abs(dy))
s = abs(dx);
else
s = abs(dy);
xi = dx / (float) s;
yi = dy / (float) s;
x = x1;
y = y1;
putpixel(x1, y1, WHITE);
for (m = 0; m < s; m++)
{
x += xi;
y += yi;
putpixel(x, y, WHITE);
}
}
void main()
{
int gd = DETECT, gm = DETECT, x1, y1, x2, y2;
clrscr();
initgraph(&gd, &gm, "C:\\tc\\bgi");
drawline(20,100,70,30);
drawline(70,30,120,100);
drawline(20,100,120,100);
getch();
}
OUTPUT
PROGRAM NO. 11(a)
/* Program in c for printing a rectangle with the help of DDA aglorithm */
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void drawline(int x1,int y1,int x2,int y2)
int dx,dy,m,s;
float xi,yi,x,y;
dx = x2 - x1;
dy = y2 - y1;
if (abs(dx) > abs(dy))
s = abs(dx);
else
s = abs(dy);
xi = dx / (float) s;
yi = dy / (float) s;
x = x1;
y = y1;
putpixel(x1, y1, WHITE);
for (m = 0; m < s; m++)
x += xi;
y += yi;
putpixel(x, y, WHITE);
void main()
int gd = DETECT, gm = DETECT;
clrscr();
initgraph(&gd, &gm, "C:\\turboC3\\bgi");
drawline(150,450,450,450);
drawline(450,450,450,150);
drawline(450,150,150,150);
drawline(150,150,150,450);
getch();
}
Program 11(b)
/* Program in C for printing a rectangle with the help of Bresenham’s line drawing alglorithm */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void lineBres(int,int,int,int);
void setPixel(int,int);
void main()
int x,y,length,breadth;
int driver=DETECT,mode;
clrscr();
printf("Enter the co-ordinates of the lower left corner (a b): ");
scanf("%d %d",&x,&y);
printf("Enter the length of the rectangle: ");
scanf("%d",&length);
printf("Enter the breadth of the rectangle: ");
scanf("%d",&breadth);
getch();
initgraph(&driver,&mode,"C:\\TURBOC3\\BGI");
lineBres(x,y,x+length,y);
lineBres(x+length,y,x+length,y-breadth);
lineBres(x+length,y-breadth,x,y-breadth);
lineBres(x,y-breadth,x,y);
getch();
closegraph();
void lineBres(int x1,int y1,int x2,int y2)
float error,m;
int x,y;
x=x1;
y=y1;
if(x1==x2)
while(y!=y2)
if(y2-y1>0)
++y;
else
--y;
putpixel(x,y,2);
else
m=(float)(y2-y1)/(x2-x1);
error=0;
putpixel(x,y,2);
while(x!=x2)
error+=m;
if(error>.5)
{
if(x2-x1>0)
y+=1;
else
y-=1;
--error;
if(x2-x1>0)
++x;
else
--x;
putpixel(x,y,2);