0% found this document useful (0 votes)
202 views

Practical-1: Aim:-Write A Program To Implement DDA Line Drawing Algorithm. Algorithm

The document contains code and instructions for implementing various computer graphics algorithms: 1. The first section provides code to implement the DDA line drawing algorithm. It includes steps to calculate increments in x and y, and a for loop to put pixels along the line incrementally. 2. The second section provides code for Bresenham's line drawing algorithm. It includes steps to calculate decision parameters and increments in x and y, and uses these to incrementally draw pixels along the line. 3. The third section provides code to implement Bresenham's circle drawing algorithm. It includes initializing parameters, a function to draw pixels, and a loop to incrementally calculate and draw pixels along the circle. 4

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)
202 views

Practical-1: Aim:-Write A Program To Implement DDA Line Drawing Algorithm. Algorithm

The document contains code and instructions for implementing various computer graphics algorithms: 1. The first section provides code to implement the DDA line drawing algorithm. It includes steps to calculate increments in x and y, and a for loop to put pixels along the line incrementally. 2. The second section provides code for Bresenham's line drawing algorithm. It includes steps to calculate decision parameters and increments in x and y, and uses these to incrementally draw pixels along the line. 3. The third section provides code to implement Bresenham's circle drawing algorithm. It includes initializing parameters, a function to draw pixels, and a loop to incrementally calculate and draw pixels along the circle. 4

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/ 37

8716116

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

1
8716116

Code:-

#include<stdio.h>

#include<graphics.h>

#include<math.h>

float round(float a);

void main()

{ int gd=DETECT,gm; // gd=graphics driver (detects best graphics driver and assigns it as
default, gm=graphics mode.
int x1,y1,x2,y2,steps,k;
floatxincr,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");//initializes the graph

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;

2
8716116

y=y1;

for(k=1;k<=steps;k++)

{ delay(100);//for seeing the line drawing process slowly.

x+=xincr;

y+=yincr;

putpixel(round(x),round(y),WHITE);

outtextxy(200,20,"DDA"); // for printing text at desired screen location.

outtextxy(x1+5,y1-5,"(x1,y1)");

outtextxy(x2+5,y2+5,"(x2,y2)");

getch();

closegraph(); // closes the graph and comes back to previous graphic mode.

float round(float a)

int b=a+0.5;

return b;

3
8716116

PRACTICAL-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

4
8716116

Then d = d + i1

If d >= 0

Then d = d + i2

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

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

5
8716116

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;

y=y+1;

p=p+(2*dy)-(2*dx);

putpixel(x,y,7);

getch();

closegraph();

6
8716116

PRACTICAL-3

Aim:- Implement the Bresenham’s Circle 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);

7
8716116

Code:-

#include<stdio.h>

#include<dos.h>

#include<graphics.h>

void 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-y,yc-x,RED);

putpixel(xc-y,yc-x,RED);

void circleBres(int xc, intyc, int r)

{ int x=0,y=r;

int d=3-2*r;

drawCircle(xc,yc,x,y);

while(y>=x)

{ x++;

if(d>0)

{ y--;

d=d+4*(x-y)+10;

else

d=d+4*x+6;

drawCircle(xc,yc,x,y);

8
8716116

delay(50);

}}

int main()

{ int xc=50, yc=50, r2=30;

int gd=DETECT,gm;

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

circleBres(xc,yc,r2);

return 0;

getch();

9
8716116

PRACTICAL-4

Aim:- Write a program to draw a decagon whose all vertices are connected with
every other vertex 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 along with 35 diagonals.

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)

10
8716116

Code:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

int main()

int gd=DETECT,gm;

clrscr();

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

line(120,220,170,270);

line(170,270,220,220);

line(220,220,270,270);

line(270,270,320,220);

line(320,220,320,420);

line(320,420,270,370);

line(270,370,220,420);

line(220,420,170,370);

line(170,370,120,420);

line(120,420,120,220);

getch();

closegraph();

return(0);

11
8716116

PRACTICAL-5

Aim:- Write a program to move an object using the concepts 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 + t x and y’ = y + ty . tx is the translation
vector in the direction of x – direction. ty is 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.

Code:-
#include<graphics.h>

#include<stdlib.h>

#include<stdio.h>

#include<conio.h>

#include<math.h>

12
8716116

void main()

{ intgm;

int gd=DETECT;

int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;

intsx,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(3);

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.Scaling\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;

13
8716116

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

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

14
8716116

getch();

break;

case 3:

printf("\n Enter the scalling factor");

scanf("%f%f",&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();

break;

case 4:

break;

default:

printf("Enter the correct choice");

closegraph();

15
8716116

PRACTICAL-6

Aim:- write a program to implement the mid point circle drawing algorithm.
Algorithm:-
Step 1: Read the radius (r) of the circle.
Step 2: Initialize starting position as
X=0 y=r
Step 3: calculate initial value or decision parameter as
P=1-r
Step 4: do
{ plot(x,y)
if(d<0)
{ x=x+1
Y=y
D=d+2x+1
}
Else
{ x=x+1
Y=y-1
D=d+2x+2y+1
}while(x<y)
Step 5: determine the symmetry points
Step 6: stop

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;

16
8716116

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)
{ drawCircle(x,y,xc,yc);
++x;
if(pk < 0)
{ pk = pk + (2*x) + 3;
}
else
{ --y;
pk = pk + (2*x) + 5 - (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);
}

17
8716116

PRACTICAL-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.

18
8716116

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.
 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.

19
8716116

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 lines

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;

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

20
8716116

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;

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

21
8716116

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

22
8716116

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;

23
8716116

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

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

24
8716116

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

25
8716116

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

Code:-
#include<graphics.h>

#include<conio.h>

int main()

{ int gd = DETECT,gm;

26
8716116

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;

27
8716116

PRACTICAL-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.

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

28
8716116

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

29
8716116

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)

30
8716116

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

31
8716116

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

32
8716116

PRACTICAL-11(A)
Aim:- Program in c for printing a rectangle with the help of DDA algorithm.

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);
}
}
void main()
{ int gd = DETECT, gm = DETECT;
clrscr();
initgraph(&gd, &gm, "C:\\turboC3\\bgi");
drawline(150,450,450,450);

33
8716116

drawline(450,450,450,250);
drawline(450,250,150,250);
drawline(150,250,150,450);
getch();
}

34
8716116

PRACTICAL-11(B)
Aim:- Program in C for printing a rectangle with the help of Bresenham’s line
drawing algorithm.
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;
int x,y;
x=x1;
y=y1;

35
8716116

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

36
8716116

37

You might also like