0% found this document useful (0 votes)
166 views52 pages

CG Practicals

The document is the practical file of Abhishek (Roll No. 87175101) for the course Computer Graphics and Animation (CSE-403N). It contains 11 practical assignments related to computer graphics topics like line drawing algorithms, circle drawing, transformations, clipping, and more. The file tracks the assignments completed with date and signature.

Uploaded by

Drishti Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views52 pages

CG Practicals

The document is the practical file of Abhishek (Roll No. 87175101) for the course Computer Graphics and Animation (CSE-403N). It contains 11 practical assignments related to computer graphics topics like line drawing algorithms, circle drawing, transformations, clipping, and more. The file tracks the assignments completed with date and signature.

Uploaded by

Drishti Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 52

ROLL NO.

-87175101

PRACTICAL FILE OF
COMPUTER GRAPHICS AND
ANIMATION
(CSE-403N)

Submitted to: Submitted by:


Mrs. Asha Abhishek
Asst. Prof. CSE Deptt. CSE 7th sem.
Roll no. 87175101

Department of Computer science and Engineering


State Institute of Engineering and Technology
(Nilokheri)
ROLL NO.-87175101

INDEX:-
S.NO PRACTICAL DATE SIGN
1. Write a program to implement DDA line drawing
algorithm.
2. Write a program to implement Bresenham’s line
drawing algorithm.
3. Implement the Bresenham’s circle drawing
algorithm.
4. Write a program to draw a decagon whose all
vertices are connected with every other vertex
using lines.
5. Write a program to move an object using the
concepts of 2-D transformations.
6. Write a program to implement the midpoint circle
drawing algorithm using any Object Oriented
Programming Language like Python, C++,Java.
7. Implement the line clipping algorithm using any
Object Oriented Programming Language like
Python, C++, Java.
8. Implement boundary fill algorithm using any
Object Oriented Programming Language like
Python, C++, Java.
9. Draw HUT using C.

10. Perform the Polygon Clipping Algorithm using


any Object oriented language like Python, C+
+,Java.
11. (A) Program in c for printing a rectangle with the
help of DDA algorithm.
(B) Program in C for printing a rectangle with the
help of Bresenham’s line drawing algorithm.
ROLL NO.-87175101

OUTPUT :- PRACTICAL NO 1
ROLL NO.-87175101

PRACTICAL NO: 1
AIM :- Write a program to implement DDA line drawing algorithm.
Algorithm :-

Step 1: Calculate dx, dy

dx = x1-x0;

dy = y1-y0;

Step 2: Choose number of steps to put pixel as

steps = abs(dx) > abs(dy) ? abs (dx) : abs (dy);

Step 3: Calculate increment in x & y for each steps

Xinc = dx / (float) steps;

Yinc = dy / (float) steps;

Step 4: Put pixel for each step

X = x0;

Y = y0;

for ( int i = 0 ; i <= steps ; i++)

putpixel ( X , Y , WHITE )

X += Xinc ;

Y += Yinc ;

}
ROLL NO.-87175101

Program Code
#include<stdio.h>
#include<graphics.h>
#include<math.h>
float round(float a);
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,steps,k;
float xincr,yincr,x,y,dx,dy;
printf("enter x1,y1");
scanf("%d%d",&x1,&y1);
printf("enter x2,y2");
scanf("%d%d",&x2,&y2);
initgraph(&gd,&gm,"c:\\turboc3\\BGI");
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
{
steps=abs(dx);
}
else
{
steps=abs(dy);
}
xincr=dx/steps;
yincr=dy/steps;
x=x1;
y=y1;
for(k=1;k<=steps;k++)
{
delay(100);
x+=xincr;
y+=yincr;
putpixel(round(x),round(y),WHITE);
}
outtextxy(200,20,"DDA");
outtextxy(x1+5,y1-5,"(x1,y1)");
outtextxy(x2+5,y2+5,"(x2,y2)");
getch();
closegraph();
}
float round(float a)
{ int b=a+0.5;
return b;
}
ROLL NO.-87175101
ROLL NO.-87175101

OUTPUT :PRACTICAL NO 2
ROLL NO.-87175101

PRACTICAL NO: 2
AIM :- Write a program to implement Bresenham’s line drawing
algorithm.
Algorithm :-
Step 1: Declare variable x1, x2, y1, y2, d, i1, i2, dx, dy
Step 2: Enter value of x1, y1, x2, y2
Where x1, y1 coordinates of starting point and x2, y2 coordinates of ending point
Step 3: Calculate dx = x2 – x1
dy = y2 – y1
i1 = 2 * dy;
i2 = 2 * (dy - dx)
d = i1 – dx
Step 4: Consider (x,y) as starting point and xendas maximum possible value of x.
If dx < 0
Then x = x2
y = y2
xend= x1
If dx > 0
Then x = x1
y = y1
xend= x2
Step 5: Generate points at (x,y) coordinates.
Step 6: Check if whole line is generated
If x >= xend
Stop
Step 7: Calculate coordinates of the next pixel
If d < 0
Then d = d + i1
If d >= 0
Then d = d + i2
ROLL NO.-87175101

Increment y = y + 1
Increment x = x + 1
Step 8: Draw a point of latest (x,y) coordinates
Step 9: Go to step 6
Step 10: End of algorithm

Program code :-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
int x,y,x1,y1,x2,y2,p,dx,dy;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\nEnter the x-coordinate of the first point ::");
scanf("%d",&x1);
printf("\nEnter the y-coordinate of the first point ::");
scanf("%d",&y1);
printf("\nEnter the x-coordinate of the second point ::");
scanf("%d",&x2);
printf("\nEnter the y-coordinate of the second point ::");
scanf("%d",&y2);
x=x1;
y=y1;
dx=x2-x1;
dy=y2-y1;
putpixel(x,y,2);
p=(2*dy-dx);

while(x<=x2)
{
if(p<0)
{
x=x+1;
p=p+2*dy;
}

else
{
x=x+1;
ROLL NO.-87175101

y=y+1;
p=p+(2*dy)-(2*dx);
}

putpixel(x,y,7);

getch();
closegraph();
}
ROLL NO.-87175101

OUTPUT:- PRACTICAL NO 3
ROLL NO.-87175101

PRACTICAL NO: 3
AIM :- Implement Bresenham’s Circle drawing algorithm.
Algorithm :-
Step 1: Set initial values of (xc,yc) and (x,y)
Step 2: Set decision parameter d to d = 3 – ( 2 * r )
Step 3: Call drawCircle(int xc, intyc, int x, int y) function
Step 4: Repeat steps 5 to 8 until x <= y
Step 5: Increment value of x
Step 6: If d < 0, set d = d + ( 4 * x ) + 6
Step 7: Else, set d = d + 4 * ( x – y ) + 10 and decrement y by 1
Step 8: Call drawCircle(int xc, intyc, int x, int y) function

drawCircle(int xc, intyc, int x, int y)


{
putpixel ( xc + x , yc + y, RED);
putpixel ( xc - x , yc + y, RED);
putpixel ( xc + x , yc - y, RED);
putpixel ( xc - x , yc - y, RED);
putpixel ( xc + y , yc + x, RED);
putpixel ( xc - y , yc + x, RED);
putpixel ( xc + x , yc - x, RED);
putpixel ( xc - x , yc – x, RED);

}
ROLL NO.-87175101

Program code :-
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int d,r,x,y,xc,yc;
clrscr();
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter Radius\n");
scanf("%d",&r);
printf("Enter Center of circle\n");
scanf("%d",&xc);
scanf("%d",&yc);
d=3-2*r;
x=0;
y=r;
while(x<=y)
{
putpixel(xc+x,yc+y,RED);
putpixel(xc-y,yc-x,RED);
putpixel(xc+y,yc-x,RED);
putpixel(xc-y,yc+x,RED);
putpixel(xc+y,yc+x,RED);
putpixel(xc-x,yc-y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc+y,RED);
if(d<=0)
{
d=d+4*x+6;
}
else
{
d=d+4*x-4*y+10;
y=y-1;
}
x=x+1;
}
getch();

}
ROLL NO.-87175101

OUTPUT : PRACTICAL 4
ROLL NO.-87175101

PRACTICAL NO: 4
AIM :- Write a program to draw a decagon whose all vertices are
connected with every other vertices using lines.
Theory :-
A polygon in a computer graphics system is a two-dimensional shape that is modelled and
stored within its database (memory). A decagon is a polygon with 10 sides, angles and
vertices. Here are some examples of a decagon.

line() is the only function used in the code of this program. line() is used to draw a line from a
point (x1,y1) to point (x2,y2) i.e. (x1,y1) and (x2, y2) are the end points of the line. The code
is like :
void line( int x1,int x2,int y1,int y2)

Program Code :-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT, gm;
clrscr();
initgraph(&gd,&gm, "C:\\TURBOC3\\BGI");
line(100, 200, 150, 250);
line(150, 250, 200, 200);
line(200, 200, 250, 250);
line(250, 250, 300, 200);
line(300, 200, 300, 400);
line(300, 400, 250, 300);
line(250, 300, 200, 400);
ROLL NO.-87175101

line(200, 400, 150, 300);


line(150, 300, 100, 400);
line(100, 400, 100, 200);
getch();
closegraph();
return 0;
}
ROLL NO.-87175101

OUTPUT : PRACTICAL 5
TRANSLATION :-

ROTATION :-
ROLL NO.-87175101

PRACTICAL NO: 5
AIM :- Write a program to move an object using the concept of 2-d
transformations.
Theory :-

2- D transformation :- The fundamental of CG system is the ability to simulate both the


movement and the manipulation of object in the plane.

Operations:-

Here, P (x,y) is the original point and P’ (x’,y’) is the transformed point.

1. Translation – In this operation, object is displaced at a given distance and direction


from their original position. Here, x’ = x + txand y’ = y + ty . txis the translation vector
in the direction of x – direction. tyis the translation vector in the direction of y –
direction.
2. Rotation – In this operation, in which we will rotate the cursor at a particular angle θ
from their original position. Here, x’ = xcosθ – ysinθ and y’ = xsinθ + ycosθ. If θ is
positive then it rotates in anti-clockwise direction and if it is negative, then clockwise
direction.
3. Scaling – It is the operation to expand or compress the dimensions of an object. Here,
x’ = x.sx and y’ = y.sy .If scaling coefficient is greater than 1 means expand the size,
if scaling coefficient is less than 1 means compress the size and in case it is equal to 1,
then means no change.

Program Code :-
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void main()
{
int gm;
ROLL NO.-87175101

SCALING :-
ROLL NO.-87175101

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.Translation\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);
ROLL NO.-87175101

line(nx3,ny3,nx1,ny1);
getch();
break;

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();
break;
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);
ROLL NO.-87175101

line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
break;
case 4:
break;
default:
printf("Enter the correct choice");
}
closegraph();
}
ROLL NO.-87175101

OUTPUT : PRACTICAL 6
ROLL NO.-87175101

PRACTICAL NO: 6
AIM :- Write a program to implement Mid Point Circle drawing
algorithm any object oriented programming language like
python,C++,JAVA.
Algorithm :-
int x=0,y=r,p=1-r;
while (x<=y)
{
set pixel(x,y);
if(p<0)
p=p+2(x-y)+5;
else
p=p+2(x-y)+5;
y--;
x++;
}

Program Code :-
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void drawCircle(int x, int y, int xc, int yc);
void main()
{
int gd = DETECT, gm;
int r, xc, yc, pk, x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
cout<<"Enter the center co-ordinates\n";
cin>>xc>>yc;
cout<<"Enter the radius of circle\n";
cin>>r;
pk = 1 - r;
x = 0;
y = r;
while(x < y)
{
ROLL NO.-87175101

drawCircle(x,y,xc,yc);
++x;
if(pk < 0)
{
pk = pk + (2*x) + 1;
}
else
{
--y;
pk = pk + (2*x) + 1 - (2*y);
}
}
getch();
closegraph();
}

void drawCircle(int x, int y, int xc, int yc)


{
putpixel(x+xc,y+yc,GREEN);
putpixel(-x+xc,y+yc,GREEN);
putpixel(x+xc, -y+yc,GREEN);
putpixel(-x+xc, -y+yc, GREEN);
putpixel(y+xc, x+yc, GREEN);
putpixel(y+xc, -x+yc, GREEN);
putpixel(-y+xc, x+yc, GREEN);
putpixel(-y+xc, -x+yc, GREEN);
}
ROLL NO.-87175101

OUTPUT : PRACTICAL 7
ROLL NO.-87175101

PRACTICAL NO: 7
AIM :- write a program to implement the line clipping algorithm.
Theory:-

Cohen-Sutherland Line Clippings

This algorithm uses the clipping window as shown in the following figure. The minimum
coordinate for the clipping region is (XWmin,YWmin) (XWmin,YWmin) and the maximum
coordinate for the clipping region is (XWmax,YWmax) (XWmax,YWmax).

We will use 4-bits to divide the entire region. These 4 bits represent the Top, Bottom, Right,
and Left of the region as shown in the following figure. Here, the TOP and LEFT bit is set to
1 because it is the TOP-LEFT corner.

There are 3 possibilities for the line −

 Line can be completely inside the window- This line should be accepted.


 Line can be completely outside of the window- This line will be completely removed
from the region.
ROLL NO.-87175101

 Line can be partially inside the window- We will find intersection point and draw
only that portion of line that is inside region.
Algorithm:-
Step 1 : Assign a region code for two endpoints of given line.
Step 2 : If both endpoints have a region code 0000
then given line is completely inside.
Step 3 : Else, perform the logical AND operation for both region codes.
Step 3.1 : If the result is not 0000, then given line is completely outside.
Step 3.2 : Else line is partially inside.
Step 3.2.1 : Choose an endpoint of the line that is outside the given rectangle.
Step 3.2.2 : Find the intersection point of the rectangular boundary (based on region
code).
Step 3.2.3 : Replace endpoint with the intersection point and update the region code.
Step 3.2.4 : Repeat step 2 until we find a clipped line either trivially accepted or trivially
rejected.
Step 4 : Repeat step 1 for other line

Program Code :-
#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;


ROLL NO.-87175101

// 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 = (x1, y1) 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;
ROLL NO.-87175101

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;

}
ROLL NO.-87175101

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

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; }
ROLL NO.-87175101

OUTPUT : PRACTICAL 8
ROLL NO.-87175101

PRACTICAL NO: 8
Aim:- write a program to implement boundary filled algorithm.
Algorithm:-
void boundaryFill4(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill4(x + 1, y, fill_color, boundary_color);
boundaryFill4(x, y + 1, fill_color, boundary_color);
boundaryFill4(x - 1, y, fill_color, boundary_color);
boundaryFill4(x, y - 1, fill_color, boundary_color);
}
}

Program Code :-
#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);
ROLL NO.-87175101

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

}
ROLL NO.-87175101

OUTPUT : PRACTICAL 9
ROLL NO.-87175101

PRACTICAL NO: 9
AIM :- Draw Hut using C.
Theory:-
 Setcolor():- The header file graphics.h contains setcolor() function which is used to
set the current drawing color to the new color.
Syntax :- void setcolor(int color);
 rectangle():- Rectangle draws a rectangle in the current line style, thickness, and
drawing color. (left,top) is the upper left corner of the rectangle, and (right,bottom)
is its lower right corner.
Syntax:- void rectangle(int left, int top, int right, int bottom);

 line():- line() is a library function of graphics.c in c programming language which is


used to draw a line from two coordinates.For example if you want to draw a line
from point(x1,y1) to point(x2,y2) you have to use line() function like
line(x1,y1,x2,y2);

syntax:- line(int x1,int y1, int x2,int y2);

 setfillstyle():- The header file graphics.h contains setfillstyle() function which sets the


current fill pattern and fill color.

Syntax : void setfillstyle(int pattern, int color)

 Floodfill():- floodfill() function is used to fill an enclosed area. Current fill pattern and


fill color is used to fill the area.

Syntax:- void floodfill(int x, int y, int border_color)

Program Code :-

#include<graphics.h>

#include<conio.h>
ROLL NO.-87175101

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;
}
ROLL NO.-87175101

OUTPUT : PRACTICAL 10
ROLL NO.-87175101

PRACTICAL NO: 10
Aim :- write a program to perform the polygon clipping algorithm.
Theory :-
A polygon boundary processed with a line clipper may be displayed as a series of
unconnected line segments, depending on the orientation of the polygon to the cIipping
window. What we reaIly want to display is a bounded area after clipping. For polygon
clipping, we require an algorithm that wiIl generate one or more closed areas that are then
scan converted for the appropriate area fill. The output of a polygon clipper should be a
sequence of vertices that defines the clipped polygon boundaries.

Program Code :-
#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

};
ROLL NO.-87175101
ROLL NO.-87175101

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;

}
ROLL NO.-87175101

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;
ROLL NO.-87175101

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( );}
ROLL NO.-87175101

OUTPUT : PRACTICAL 11(a)


ROLL NO.-87175101

PRACTICAL NO: 11(a)


AIM :- Program in c for printing a rectangle with the help of DDA
algorithm.
Program Code :-
#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);

}
ROLL NO.-87175101

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

}
ROLL NO.-87175101

OUTPUT : PRACTICAL 11(b)


ROLL NO.-87175101

PRACTICAL NO: 11(b)


AIM :- Program in C for printing a rectangle with the help of
Bresenham’s line drawing algorithm.
Program Code :-
#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;
ROLL NO.-87175101

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);
}} }
ROLL NO.-87175101
ROLL NO.-87175101
ROLL NO.-87175101

You might also like