cg record

65
LINE DRAWING USING BRESENHAM ALGORITHM EX.NO: 1A DATE: AIM: To write a C program for Line Drawing Using Bresenham Algorithm. ALGORITHM: Step 1 : Start the program. Step 2 : Declare the necessary variables. Step 3 : Initialize the graph using dx, dy, gd, gm. Step 4 : Assign the values of x1, y1 to x,y respectively. Step 5 : Similarly, absolute values to dx, dy. Step 6 : put pixel and set 15 to pixel position. Step 7 : Using do-while loop, put e,x,y,I values. Step 8 : Stop the program. PROGRAM: #include<stdio.h> #include<conio.h> #include<dos.h> #include<math.h> #include<graphics.h> void main() { float x,y,x1,y1,x2,y2,dx,dy,e; int i,gd=DETECT,gm; clrscr(); printf("\n ENTER THE VALUE OF X1:"); scanf("%f",&x1); printf("\n ENTER THE VALUES OF Y1:"); scanf("%f",&y1);

Upload: raji-sharmi

Post on 28-Jan-2016

214 views

Category:

Documents


0 download

DESCRIPTION

cg record...

TRANSCRIPT

Page 1: cg record

LINE DRAWING USING BRESENHAM ALGORITHM

EX.NO: 1A

DATE:

AIM:

To write a C program for Line Drawing Using Bresenham Algorithm.

ALGORITHM:

Step 1 : Start the program.

Step 2 : Declare the necessary variables.

Step 3 : Initialize the graph using dx, dy, gd, gm.

Step 4 : Assign the values of x1, y1 to x,y respectively.

Step 5 : Similarly, absolute values to dx, dy.

Step 6 : put pixel and set 15 to pixel position.

Step 7 : Using do-while loop, put e,x,y,I values.

Step 8 : Stop the program.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<dos.h>

#include<math.h>

#include<graphics.h>

void main()

{

float x,y,x1,y1,x2,y2,dx,dy,e;

int i,gd=DETECT,gm;

clrscr();

printf("\n ENTER THE VALUE OF X1:");

scanf("%f",&x1);

printf("\n ENTER THE VALUES OF Y1:");

scanf("%f",&y1);

printf("\n ENTER THE VALUES OF X2:");

scanf("%f",&x2);

Page 2: cg record

printf("\n ENTER THE VALUES OF Y2:");

scanf("%f",&y2);

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

dx=abs(x2-x1);

dy=abs(y2-y1);

x=x1;

y=y1;

e=2*(dy-dx); i=1;

do

{

putpixel(x,y,15);

while(e>=0)

{

y=y+1;

e=e-(2*dx);

}

x=x+1;

e=e+(2*dy);

i=i+1;

delay(100);

}

while(i<=dx);

closegraph();

getch();

}

OUTPUT:

ENTER THE VALUE OF X1: 600

ENTER THE VALUE OF Y1: 400

ENTER THE VALUE OF X2: 300

ENTER THE VALUE OF Y2: 400

Page 3: cg record

CIRCLE DRAWING USING BRESENHAM ALGORITHM

Ex. No : 1B Date :

AIM:

To write a C program for Circle Drawing Using Bresenham Algorithm.

ALGORITHM:

Step 1 : Start the program.

Step 2 : Declare the necessary variables.

Step 3 : Create the function for Circle.

Step 4 : Enter the radius and center values.

Step 5 : Initialize the graph with gd, gm and assign y<-radius.

Step 6 : Start the circle function and p<- 1- radius.

Step 7 : Check the while loop until the condition is satisfied.

Step 8 : Check the if –else condition until the condition is satisfied.

Step 9 : Assign all operation for circle function and the values.

Step 10 : Stop the Program.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void circlefun(int xcenter,int ycenter,int x,int y);

void main()

{

int x=0,radius,xcenter,ycenter;

int y,p,gd=DETECT,gm;

clrscr();

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

printf("\n ENTER THE XCENTER & YCENTER:");

scanf("%d%d",&xcenter,&ycenter);

printf("\n ENTER THE RADIUS:");

Page 4: cg record

scanf("%d",&radius);

y=radius;

circlefun(xcenter,ycenter,x,y);

p=1-radius;

while(x<y)

{

if(p<0)

x=x+1;

else

{

x=x+1;

y=y-1;

}

if(p<0)

p=p+2*x+1;

else

p=p+2*(x-y)+1;

circlefun(xcenter,ycenter,x,y);

}

getch();

}

void circlefun(int xcenter,int ycenter,int x,int y)

{

putpixel(xcenter+x,ycenter+y,1);

putpixel(xcenter-x,ycenter+y,1);

putpixel(xcenter+x,ycenter-y,1);

putpixel(xcenter-x,ycenter-y,1);

putpixel(xcenter+y,ycenter+x,1);

putpixel(xcenter-y,ycenter+x,1);

putpixel(xcenter+y,ycenter-x,1);

putpixel(xcenter-y,ycenter-x,1);

Page 5: cg record

}

OUTPUT:

ENTER THE X CENTER AND Y CENTER : 500 250

ENTER THE RADIUS : 70

RESULT:

Thus the above program “CIRCLE DRAWING USING BREASENHAM” is executed successfully.

ELLIPSE DRAWING USING BRESENHAM ALGORITHM

Ex. No : 1C Date :

AIM:

To write a C program for Ellipse Drawing Using Bresenham Algorithm

ALGORITHM:

Step 1: Start the program.

Step 2: Initialize the variables.

Step 3: Call the initgraph() function.

Step 4: Get the initialize points P1,P2.

Step 5: Get the values of Co-Ordinates of the ellipse (x,y).

Step 6: Enter the coordinates a,b of the ellipse .

Step 7: Display the output.

Step 8: Stop the program.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

Page 6: cg record

#include<math.h>

void disp();

float x,y;

int xc,yc;

void main()

{

int gd=DETECT,gm,a,b;

float p1,p2;

clrscr();

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

printf("*** Ellipse Generating Algorithm ***\n");

printf("Enter the value of Xc\t");

scanf("%d",&xc);

printf("Enter the value of Yc\t");

scanf("%d",&yc);

printf("Enter X axis length\t");

scanf("%d",&a);

printf("Enter Y axis length\t");

scanf("%d",&b);

x=0;y=b;

disp();

p1=(b*b)-(a*a*b)+(a*a)/4;

while((2.0*b*b*x)<=(2.0*a*a*y))

{

x++;

if(p1<=0)

p1=p1+(2.0*b*b*x)+(b*b);

else

{

y--;

p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);

Page 7: cg record

}

disp();

x=-x;

disp();

x=-x;

delay(50);

}

x=a;

y=0;

disp();

p2=(a*a)+2.0*(b*b*a)+(b*b)/4;

while((2.0*b*b*x)>(2.0*a*a*y))

{

y++;

if(p2>0)

p2=p2+(a*a)-(2.0*a*a*y);

else

{

x--;

p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);

}

disp();

y=-y;

disp();

y=-y;

delay(50);

}

getch();

closegraph();

}

void disp()

Page 8: cg record

{

putpixel(xc+x,yc+y,7);

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

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

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

}

OUTPUT:

*** Ellipse Generating Algorithm ***

Enter the value of Xc 250

Enter the value of Yc 250

Enter X axis length 200

Enter Y axis length 80

RESULT:

Thus using C++ program implementation of Bresenham’s algorithm for line, circle and ellipse drawing is done.

ATTRIBUTES OF OUTPUT PRIMITIVES

AIM:

To write a C program to display the attributes of output primitives.

FUNCTIONS USED:

LINE DRAWING

line(x1,y1,x2,y2);

Line function draws a line between two specified points (x1,y1) towards (x2,y2). This function comes handy if you want to draw box like shapes or just plotting the graphsetc.

e.g. line(100,50,100,400);

You can set the line style using setlinestyle functions. This function specifies the type of line, pattern & the thickness that is going to appear on the screen. You have options like solid, dotted, centered, dashed etc.

Page 9: cg record

The other attributes with the command are as follows:

setLinestyle(style,0,1);

setLinetype(lt);

setLinewidthScaleFactor(lw);

setPolylinecolourIndex(lc);

Color palettes

The graphics.h has declaration about 16 colors. In order to use the color in your program you have to use the functions like setcolor(), setbkcolor() & setfillstyle(). The function setcolor() sets the value of current drawing color to color. setfillstyle() sets the current fill pattern and fill color. setbkcolor() sets the value for background color, which is by default black.

Below is the table that describes the value for each color that are declared in the graphics.h file.

Color Value

Black 0

Blue 1

Green 2

Cyan 3

Red 4

Magenta 5

Brown 6

Lightgray 7

Darkgray 8

Lightblue 9

Lightgreen 10

Lightcyan 11

Lightred 12

Lightmagenta 13

Yellow 14

White 15

CIRCLE DRAWING

Circle()

Page 10: cg record

The function circle() is used to draw a circle using (x,y) as center point.

Syntax:

circle(x,y,radius)

The other attributes with the command are as follows: setfillstyle(style,lc),setcolor(lc).

ELLIPSE DRAWING

Ellipse(x,y,stangle,endangle.xrad,yrad)

This function draws an elliptical arc. Here (x,y) are the co-ordinates of center of the ellipse. (stangle,endangle) are the starting and ending angles.

If stangle=0 and endangle=360 then this function draws complete ellipse.

Eg.ellipse(100,150,0,360,100,50);

The other attributes with the command are as follows:

setfillcolor(lc),setfillstyle(ls),setlinecolor(lc),setlinewidth(lw)

ALGORITHM

Step 1: Start the program.

Step 2: Draw the line. Set the attributes like style, type, width etc.

Step 3: Draw the circle. Set the attributes using commands.

Step 4: Draw the ellipse. Set the attributes like fillcolor, fillstyle, linecolor, linewidth.

Step 5: Stop the program.

PROGRAM

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<string.h>

void main()

{

char ch=’y’;

int gd=DETECT,gm,x1,y1,x2,y2,red,sa,ea,xrad,yrad,I;

initgraph(&gd,&gm,””);

while(ch==’y’)

{

cleardevice();

Page 11: cg record

setbkcolor(9);

outtextxy(100,130,”Enter your choice:”);

outtextxy(100,150,”1.Line”);

outtextxy(100,170,”2.Circle”);

outtextxy(100,190,”3.Box”);

outtextxy(100,210,”4.Arc”);

outtextxy(100,230,”5.Ellipse”);

outtextxy(100,250,”6.Rectangle”);

outtextxy(100,270,”7.Exit”);

ch=getch();

cleardevice();

switch(ch)

{

case ‘1’:

line(100,200,300,400);

break;

case ‘2’:

circle(200,200,100);

break;

case ‘3’:

setfillstyle(5,4);

bar(100,300,200,100);

break;

case ‘4’:

setfillstyle(5,4);

arc(200,200,100,300,100);

break;

case ‘5’:

setfillstyle(5,4);

fillellipse(100,100,50,100);

break;

Page 12: cg record

case ‘6’:

settextstyle(DEFAULT_FONT,0,2);

outtextxy(120,140,”BEC”);

line(100,100,100,300);

line(300,300,100,300);

line(100,100,300,100);

line(300,100,300,300);

break;

case ‘7’:

closegraph();

return;

}

ch=’y’;

getch();

}

}

OUTPUT:

Enter your choice:

1.Line

2.Circle

3.Box

4.Arc

5.Ellipse

6.Rectangle

7.Exit

BIE

Page 13: cg record

RESULT:

Thus the program was executed successfully.

Page 14: cg record

TWO DIMENSIONAL TRANSFORMATIONS

AIM:

To write a C program for Two Dimensional Transformations.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the necessary variables and initialize the graph, gd, gm.

Step 3: Use do-while loop and declare the function clear device.

Step 4: Create six cases translation, rotation, scaling , reflection, shearing and exit.

Step 5: In case 1 enter the translation values and print the translation object.

Step 6: In case 2 enter the rotation values and print rotation object.

Step 7: In case 3 enter the scaling values and print the scaling object.

Step 8: In case 4 enter the reflection values and print the reflection object.

Step 9: In case 5 enter the shearing values and print the shearing object.

Step 9: Stop the program.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<graphics.h>

int ch,x,y,az,i,w,ch1,ch2,xa,ya,ra,a[10],b[10],da,db;

float x1,y1,az1,w1,dx,dy,theta,x1s,y1s,sx,sy,a1[10],b1[10];

void main()

{

int gm ,gr;

Page 15: cg record

clrscr();

detectgraph(&gm,&gr);

initgraph(&gm,&gr,"d:\\tc\\BGI");

printf("Enter the upper left corner of the rectangle:\n");

scanf("%d%d",&x,&y);

printf("Enter the lower right corner of the rectangle:\n");

scanf("%d%d",&az,&w);

rectangle(x,y,az,w);

da=az-x;

db=w-y;

a[0]=x;

b[0]=y;

a[1]=x+da;

b[1]=y;

a[2]=x+da;

b[2]=y+db;

a[3]=x;b[3]=y+db;

while(1)

{

printf("******2D Transformations*******\n");

printf("1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Shearing\n6.Exit\nEnter your choice:\n");

scanf("%d",&ch);

switch(ch)

{

case 1:

detectgraph(&gm,&gr);

initgraph(&gm,&gr,"d:\\tc\\BGI");

rectangle(x,y,az,w);

printf("*******Translation*******\n\n");

printf("Enter the value of shift vector:\n");

Page 16: cg record

scanf("%f%f",&dx,&dy);

x1=x+dx;

y1=y+dy;

az1=az+dx;

w1=w+dy;

rectangle(x1,y1,az1,w1);

break;

case 2:

detectgraph(&gm,&gr);

initgraph(&gm,&gr,"d:\\tc\\BGI");

rectangle(x,y,az,w);

printf("*******Rotation*******\n\n");

printf("Enter the value of fixed point and angle of rotation:\n");

scanf("%d%d%d",&xa,&ya,&ra);

theta=(float)(ra*(3.14/180));

for(i=0;i<4;i++)

{

a1[i]=(xa+((a[i]-xa)*cos(theta)-(b[i]-ya)*sin(theta)));

b1[i]=(ya+((a[i]-xa)*sin(theta)+(b[i]-ya)*cos(theta)));

}

for(i=0;i<4;i++)

{

if(i!=3)

line(a1[i],b1[i],a1[i+1],b1[i+1]);

else

line(a1[i],b1[i],a1[0],b1[0]);

}

break;

case 3:

detectgraph(&gm,&gr);

initgraph(&gm,&gr,"d:\\tc\\BGI");

Page 17: cg record

rectangle(x,y,az,w);

printf("********Scaling*******\n\n");

printf("Enter the value of scaling factor:\n");

scanf("%f%f",&sx,&sy);

x1=x*sx;

y1=y*sy;

az1=az*sx;

w1=w*sy;

rectangle(x1,y1,az1,w1);

break;

case 4:

detectgraph(&gm,&gr);

initgraph(&gm,&gr,"d:\\tc\\BGI");

rectangle(x,y,az,w);

printf("*******Reflection*********\n");

printf("1.About x-axis\n2.About y-axis\n3.About both axis\nEnter your choice:\n");

scanf("%d",&ch1);

switch(ch1)

{

case 1:

printf("Enter the fixed point\n");

scanf("%d%d",&xa,&ya);

theta=(float)(90*(3.14/180));

for(i=0;i<4;i++)

{

a1[i]=(xa+((a[i]-xa)*cos(theta)-(-b[i]-ya)*sin(theta)));

b1[i]=(ya+((a[i]-xa)*sin(theta)+(-b[i]-ya)*cos(theta)));

}

for(i=0;i<4;i++)

{

if(i!=3)

Page 18: cg record

line(a1[i],b1[i],a1[i+1],b1[i+1]);

else

line(a1[i],b1[i],a1[0],b1[0]);

}

break;

case 2:

printf("Enter the fixed point\n");

scanf("%d%d",&xa,&ya);

theta=(float)(270*(3.14/180));

for(i=0;i<4;i++)

{

a1[i]=(xa+((-a[i]-xa)*cos(theta)-(b[i]-ya)*sin(theta)));

b1[i]=(ya+((-a[i]-xa)*sin(theta)+(b[i]-ya)*cos(theta)));

}

for(i=0;i<4;i++)

{

if(i!=3)

line(a1[i],b1[i],a1[i+1],b1[i+1]);

else

line(a1[i],b1[i],a1[0],b1[0]);

}

break;

case 3:

printf("Enter the fixed point\n");

scanf("%d%d",&xa,&ya);

theta=(float)(180*(3.14/180));

for(i=0;i<4;i++)

{

a1[i]=(xa+((-a[i]-xa)*cos(theta)-(-b[i]-ya)*sin(theta)));

b1[i]=(ya+((-a[i]-xa)*sin(theta)+(-b[i]-ya)*cos(theta)));

}

Page 19: cg record

for(i=0;i<4;i++)

{

if(i!=3)

line(a1[i],b1[i],a1[i+1],b1[i+1]);

else

line(a1[i],b1[i],a1[0],b1[0]);

}

break;

}

break;

case 5:

detectgraph(&gm,&gr);

initgraph(&gm,&gr,"d:\\tc\\BGI");

rectangle(x,y,az,w);

printf("*******Shearing******\n\n");

printf("1.x-direction shear\n2.y-direction shear\nEnter your choice:\n");

scanf("%d",&ch2);

switch(ch2)

{

case 1:

printf("Enter the value of shear:\n");

scanf("%f",&x1s);

x1=x+(y*x1s);

y1=y;

az1=az+(w*x1s);

w1=w;

rectangle(x1,y1,az1,w1);

break;

case 2:

printf("Enter the value of shear:\n");

scanf("%f",&y1s);

Page 20: cg record

x1=x;

y1=y+(x*y1s);

az1=az;

w1=w+(az*y1s);

rectangle(x1,y1,az1,w1);

break;

}

break;

case 6:

exit(0);

}

}

getch();

}

OUTPUT:

Enter the upper left corner of the rectangle:100100Enter the lower right corner of the rectangle:200200 

******2DTransformations*******  1.Translation2.Rotation3.Scaling4.Reflection5.Shearing6.ExitEnter your choice: 1

*******Translation*******

Enter the value of shift vector:150150 

Page 21: cg record

******2DTransformations*******  1.Translation2.Rotation3.Scaling4.Reflection5.Shearing6.ExitEnter your choice: 2

*******Rotation******* Enter the value of fixed point and angle of rotation:30030070

******2DTransformations*******

1.Translation2.Rotation3.Scaling4.Reflection5.Shearing6.Exit

Page 22: cg record

Enter your choice: 3

********Scaling*******Enter the value of scaling factor:22 

******2DTransformations*******  1.Translation2.Rotation3.Scaling4.Reflection5.Shearing6.ExitEnter your choice: 4 

*******Reflection*********1.About x-axis2.About y-axis3.About both axisEnter your choice:1Enter the fixed point 150150

Page 23: cg record

Enter your choice: 2Enter the fixed point 150150

Enter your choice: 3Enter the fixed point 150150

******2DTransformations*******

Page 24: cg record

1.Translation2.Rotation3.Scaling4.Reflection5.Shearing6.ExitEnter your choice: 5 

*******Shearing*********1.x-direction shear2.y-direction shearEnter your choice: 1Enter the value of shear : 2

Enter your choice: 2Enter the value of shear : 2

RESULT:

Thus the above program”TWO DIMENSIONAL TRANSFORMATIONS” Is executed successfully.

Page 25: cg record

Ex.No:3(d) 2D Transformations – Shearing & Reflection

Aim: To write a C program for implementing the two dimensional transformation such as Shearing & Reflection.

Algorithm:

1. Start the program.

2. Initialize the graphics drive and mode.

3. Read the co-ordinates of 2D object, which is transformed.

4. For shearing read the shearing factor and apply the shearing factor formula with respect to x & y. i)x’ = x + shx*y

y’ = y

ii)y’=y + shy*x

x’= x

5. Stop the program.

PROGRAM

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<string.h>

#include<stdlib.h>

void main()

{

int gd=DETECT,gm,midx,midy,midw,n;

int xs1,xs2,xs3,ys1,ys2,ys3,ch,x1,y1,x2,y2,x3,y3,abs,c,b,w,z,sh;

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

printf("\nEnter the I co-ordinate\n");

scanf("%d%d",&x1,&y1);

printf("\nEnter the II coordinate\n");

scanf("%d%d",&x2,&y2);

midx=abs((x2-x1)/2);

midy=abs((y2-y1)/2);

cleardevice();

do

Page 26: cg record

{

printf("1.Shearing\n2.Reflection\n3.Quit\n");

scanf("%d",&n);

switch(n)

{

case 1:

cleardevice();

line(x1,y1,x2,y2);

moveto(x2,y2);

lineto(x1+midx,y1+midy);

lineto(x1,y1);

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

scanf("%d",&sh);

printf("Enter the choice 1 with respect to x,2 with respect to y\n");

scanf("%d",&ch);

printf("%d",ch);

if(ch==1)

{

xs1=x1+sh*y1;

xs2=x2+sh*y2;

line(x1,y1,xs2,y2);

moveto(xs2,y2);

lineto(xs1+abs((xs2-xs1)/2),y1+abs((y2-y1)/2));

lineto(x1,y1);

}

else

{

ys1=y1+sh*x1;

ys2=y2+sh*x2;

line(x1,y1,x2,ys2);

moveto(x2,ys2);

Page 27: cg record

lineto(x1+abs((x2-x1)/2),ys1+abs((ys2-ys1)/2));

lineto(x1,y1);

}

getch();

break;

case 2:

cleardevice();

line(x1,y1,x2,y2);

moveto(x2,y2);

lineto(x1+midx,y1+midy);

lineto(x1,y1);

if(y1>y2)

z=y1;

else

z=y2;

if(x1<x2)

w=x2;

else

w=x1;

line(0,z+10,w+50,z+10);

line(x1,z+20+(z-y1),x2,z+20+(z-y2));

moveto(x2,z+20+(z-y2));

lineto(x1+midx,z+20+(z-(y1+midy)));

lineto(x1,z+20+(z-y1));

getch();

break;

case 3:

exit(0);

break;

}}

while(n<=3);

Page 28: cg record

getch(); }

OUTPUT:

Enter the I co-ordinate

100 200

Enter the II co-ordinate

300 150

1.Shearing

2.Reflection

3.Quit

1

Enter the shearing factor

1

Enter the choice 1 with respect to x,2 with respect to y

1

1.Shearing

2.Reflection

3.Quit

1

Enter the shearing factor

1

Enter the choice 1 with respect to x,2 with respect to y

2

Page 29: cg record

1.Shearing

2.Reflection

3.Quit

2

1.Shearing

2.Reflection

3.Quit

3

COHEN SUTHERLAND 2D LINE CLIPPING AND WINDOWING

Page 30: cg record

Ex. No : 5

Date :

AIM:

To write a C program to implement cohen-sutherland 2d line clipping.

ALGORITHM:

Step 1: Enter the line end points and the window coordinates.

Step 2: Every line end point is assigned a code that identified the location of the point relative to the boundaries of the clipping rectangle.

Step 3: Check whether the line lies inside the window then it is entirely drawn.

Step 4: Check whether the line lies outside the window then it is entirely clipped.

Step 5: Otherwise check whether the line intersects the window:

a) Calculate differences between endpoints and clip boundaries.

b) Determine the intersection point and how much of the line is to be discarded

PROGRAM:

#include <stdio.h>

#include <stdlib.h>

#include <graphics.h>

#define MAX 20

enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 };

enum { FALSE, TRUE };

typedef unsigned int outcode;

outcode compute_outcode(int x, int y,

int xmin, int ymin, int xmax, int ymax)

{

outcode oc = 0;

if (y > ymax)

oc |= TOP;

else if (y < ymin)

oc |= BOTTOM;

Page 31: cg record

if (x > xmax)

oc |= RIGHT;

else if (x < xmin)

oc |= LEFT;

return oc;

}

void cohen_sutherland (double x1, double y1, double x2, double y2,

double xmin, double ymin, double xmax, double ymax)

{

int accept;

int done;

outcode outcode1, outcode2;

accept = FALSE;

done = FALSE;

outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);

outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);

do

{

if (outcode1 == 0 && outcode2 == 0)

{

accept = TRUE;

done = TRUE;

}

else if (outcode1 & outcode2)

{

done = TRUE;

Page 32: cg record

}

else

{

double x, y;

int outcode_ex = outcode1 ? outcode1 : outcode2;

if (outcode_ex & TOP)

{

x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);

y = ymax;

}

else if (outcode_ex & BOTTOM)

{

x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);

y = ymin;

}

else if (outcode_ex & RIGHT)

{

y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);

x = xmax;

}

else

{

y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);

x = xmin;

}

if (outcode_ex == outcode1)

{

x1 = x;

y1 = y;

outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);

Page 33: cg record

}

else

{

x2 = x;

y2 = y;

outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);

}

}

} while (done == FALSE);

if (accept == TRUE)

line (x1, y1, x2, y2);

}

void main()

{

int n;

int i, j;

int ln[MAX][4];

int clip[4];

int gd = DETECT, gm;

printf ("Enter the number of lines to be clipped:");

scanf ("%d", &n);

printf ("Enter the x- and y-coordinates of the line-endpoints:\n");

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

for (j=0; j<4; j++)

scanf ("%d", &ln[i][j]);

printf ("Enter the x- and y-coordinates of the bottom-left and right-top corners of the clip window:\n");

Page 34: cg record

for (i=0; i<4; i++)

scanf ("%d", &clip[i]);

initgraph (&gd, &gm, "..//bgi");

rectangle (clip[0], clip[1], clip[2], clip[3]);

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

line (ln[i][0], ln[i][1], ln[i][2], ln[i][3]);

getch();

cleardevice();

rectangle (clip[0], clip[1], clip[2], clip[3]);

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

{

cohen_sutherland (ln[i][0], ln[i][1], ln[i][2], ln[i][3],

clip[0], clip[1], clip[2], clip[3]);

getch();

}

closegraph();

}

OUTPUT:

Enter the number of lines to be clipped: 1

Enter the x- and y-coordinates of the line-endpoints:

150

300

400

450

Enter the x- and y-coordinates of the bottom-left and right-top corners of the clip window:

200

200

400

Page 35: cg record

400

Result:Thus the C program to implement the line clipping was written, executed and the output wasverified successfully.

EX NO: 8

DATE:

WINDOWING TO VIEWPORT MAPPING

Aim :

To write a C program to clip a Window to Viewport Mapping.

Algorithm:

Step 1:Start the program.

Step 2:Get the maximum and minimum co-ordinates of the Window

Step 3:Get the maximum and minimum co-ordinates of the ViewPort

Page 36: cg record

Step 4:Get the co-ordinates of a point by fitting window in viewport.

Step 5:Display the output.

Step 6:Stop the program.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

main()

{

float sx,sy;

int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4;

int gd=DETECT,gm;

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

printf("Enter The Coordinate x1,y1,x2,y2,x3,y3\n");

scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);

cleardevice();

w1=5;

w2=5;

w3=635;

w4=465;

rectangle(w1,w2,w3,w4);

line(x1,y1,x2,y2);

line(x2,y2,x3,y3);

line(x3,y3,x1,y1);

getch();

v1=425;

v2=75;

v3=550;

v4=250;

sx=(float)(v3-v1)/(w3-w1);

Page 37: cg record

sy=(float)(v4-v2)/(w4-w2);

rectangle(v1,v2,v3,v4);

x1=v1+floor(((float)(x1-w1)*sx)+.5);

x2=v1+floor(((float)(x2-w1)*sx)+.5);

x3=v1+floor(((float)(x3-w1)*sx)+.5);

y1=v2+floor(((float)(y1-w2)*sy)+.5);

y2=v2+floor(((float)(y2-w2)*sy)+.5);

y3=v2+floor(((float)(y3-w2)*sy)+.5);

line(x1,y1,x2,y2);line(x2,y2,x3,y3);

line(x3,y3,x1,y1);

getch();

return 0;

}

OUTPUT:

Enter The Coordinate x1,y1,x2,y2,x3,y3

100

200

300

400

500

350

Page 38: cg record

RESULT:

Thus the above program has been executed and output is verified.

Ex. No : 6

Date :

Page 39: cg record

SUTHERLAND-HODGEMAN POLYGON CLIPPING

AIM

To write a C program to implement Sutherland-hodgeman polygon clipping.

ALGORITHM:1. Get the minimum and maximum coordinates of window.2. Get the number of sides of a polygon and its corresponding coordinates.3. If the polygon lies within region code window, display it.4. If any one of the polygon side is neither inside nor outside the boundary, find point of intersection and clip the regions that lies outside the boundary.5. Display the polygon after clipping.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#define round(a) ((int)(a+0.5))

int k;

float xmin,ymin,xmax,ymax,arr[20],m;

void clipl(float x1,float y1,float x2,float y2)

{

if(x2-x1)

m=(y2-y1)/(x2-x1);

if(x1>=xmin && x2>=xmin)

{

arr[k]=x2;

arr[k+1]=y2;

k+=2;

}

if(x1<xmin && x2>=xmin)

{

arr[k]=xmin;

arr[k+1]=y1+m*(xmin-x1);

arr[k+2]=x2;

arr[k+3]=y2;

Page 40: cg record

k+=4;

}

if(x1>=xmin && x2<xmin)

{

arr[k]=xmin;

arr[k+1]=y1+m*(xmin-x1);

k+=2;

}

}

void clipt(float x1,float y1,float x2,float y2)

{

if(y2-y1)

m=(x2-x1)/(y2-y1);

if(y1<=ymax && y2<=ymax)

{

arr[k]=x2;

arr[k+1]=y2;

k+=2;

}

if(y1>ymax && y2<=ymax)

{

arr[k]=x1+m*(ymax-y1);

arr[k+1]=ymax;

arr[k+2]=x2;

arr[k+3]=y2;

k+=4;

}

if(y1<=ymax && y2>ymax)

{

arr[k]=x1+m*(ymax-y1);

Page 41: cg record

arr[k+1]=ymax;

k+=2;

}

}

void clipr(float x1,float y1,float x2,float y2)

{

if(x2-x1)

m=(y2-y1)/(x2-x1);

if(x1<=xmax && x2<=xmax)

{

arr[k]=x2;

arr[k+1]=y2;

k+=2;

}

if(x1>xmax && x2<=xmax)

{

arr[k]=xmax;

arr[k+1]=y1+m*(xmax-x1);

arr[k+2]=x2;

arr[k+3]=y2;

k+=4;

}

if(x1<=xmax && x2>xmax)

{

arr[k]=xmax;

arr[k+1]=y1+m*(xmax-x1);

k+=2;

}

}

void clipb(float x1,float y1,float x2,float y2)

Page 42: cg record

{

if(y2-y1)

m=(x2-x1)/(y2-y1);

if(y1>=ymin && y2>=ymin)

{

arr[k]=x2;

arr[k+1]=y2;

k+=2;

}

if(y1<ymin && y2>=ymin)

{

arr[k]=x1+m*(ymin-y1);

arr[k+1]=ymin;

arr[k+2]=x2;

arr[k+3]=y2;

k+=4;

}

if(y1>=ymin && y2<ymin)

{

arr[k]=x1+m*(ymin-y1);

arr[k+1]=ymin;

k+=2;

}

}

void main()

{

int gdriver=DETECT,gmode,n,poly[20],i;

float xi,yi,xf,yf,polyy[20];

clrscr();

printf("Coordinates of rectangular clip window :\nxmin,ymin:");

Page 43: cg record

scanf("%f%f",&xmin,&ymin);

printf("Coordinates of rectangular clip window :\nxmax,ymax:");

scanf("%f%f",&xmax,&ymax);

printf("\n\nPolygon to be clipped :\nNumber of sides :");

scanf("%d",&n);

printf("Enter the coordinates :");

for (i=0;i<2*n;i++)

scanf("%f",&polyy[i]);

polyy[i]=polyy[0];

polyy[i+1]=polyy[1];

for(i=0;i<2*n+2;i++)

poly[i]=round(polyy[i]);

initgraph(&gdriver,&gmode,"C:\\TC\\BGI");

rectangle(xmin,ymax,xmax,ymin);

printf("\tUNCLIPPED POLYGON");

fillpoly(n,poly);

getch();

cleardevice();

k=0;

for(i=0;i<2*n;i+=2)

clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);

n=k/2;

for(i=0;i<k;i++)

polyy[i]=arr[i];

polyy[i]=polyy[0];

polyy[i+1]=polyy[1];

k=0;

for(i=0;i<2*n;i+=2)

clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);

n=k/2;

for(i=0;i<k;i++)

Page 44: cg record

polyy[i]=arr[i];

polyy[i]=polyy[0];

polyy[i+1]=polyy[1];

k=0;

for(i=0;i<2*n;i+=2)

clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);

n=k/2;

for(i=0;i<k;i++)

polyy[i]=arr[i];

polyy[i]=polyy[0];

polyy[i+1]=polyy[1];

k=0;

for(i=0;i<2*n;i+=2)

clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);

for(i=0;i<k;i++)

poly[i]=round(arr[i]);

if(k)

fillpoly(k/2,poly);

rectangle(xmin,ymax,xmax,ymin);

printf("\tCLIPPED POLYGON");

getch();

closegraph();

}

OUTPUT:

Coordinates of rectangular clip window:

xmin,ymin: 150 150

Coordinates of rectangular clip window:

xmax,ymax: 300 300

Polygon to be clipped :

Number of sides : 5

Enter the coordinates :

Page 45: cg record

50 50 200 100 350 350 80 200 40 80

UNCLIPPED POLYGON

CLIPPED POLYGON

Result:Thus the C program to implement the polygon clipping was written, executed and the output was verified successfully.

Ex.No:73D transformations - Translation, Rotation, ScalingAim:To write a C program to implement three dimensional transformations such as Translation,Rotation and Scaling.Algorithm:1. Start the program.2. Draw the 3D object and also display the choices.3. Get the choice and perform the corresponding actions.4. The choices is ‘1’ then get the translation values and move the 3D-object.5. The choices is ‘2’ then get the scaling vectors and change the size of 3D-object.6. The choices is ‘3’ then get the rotation angle to rotate the 3D-object.7. The choices is ‘4’ then exit from the output window.8. Terminate the program.PROGRAM:#include<stdio.h>#include<graphics.h>#include<dos.h>

Page 46: cg record

#include<conio.h>#include<math.h>#include<process.h>int points[50];int points1[50];int x=25;int n=5;void construct(int[]);void construct1(int[],int[]);void main(){int gd=DETECT;int gm=DETECT;char z;initgraph(&gm,&gd,"D:/tc/bgi");do{int points[50]={100,100,100,150,150,150,150,100,100,100};int a;cleardevice();printf("\n\n\n\t\t\t\t1.Translation");printf("\n\n\n\t\t\t\t2.Scaling");printf("\n\n\n\t\t\t\t3.Rotation");printf("\n\n\n\t\t\t\t4.Exit");printf("\n\n\n\tEnter the choice:");scanf("%d",&a);switch(a){case 1:{int tx,ty,i,point[50],point1[50];construct(points);printf("Enter the Translation Values:\n");scanf("%d%d",&tx,&ty);for(i=0;i<=2*n;i=i+2){point[i]=points[i]+tx;point1[i]=points1[i]+tx;}for(i=1;i<2*n;i=i+2){point[i]=points[i]+ty;point1[i]=points1[i]+ty;}construct1(point,point1);getch();break;}case 2:

Page 47: cg record

{int sx,sy,sz,point[50],point1[50],i;construct(points);printf("Enter the scaling values:\n");scanf("%d%d%d",&sx,&sy,&sz);point[0]=points[0];point[1]=points[1];point[2]=points[2];point[3]=points[1]+sy*(points[3]-points[1]);point[4]=points[2]+sx*(points[4]-points[2]);point[5]=points[7]+sy*(points[5]-points[7]);point[6]=points[0]+sx*(points[6]-points[0]);point[7]=points[7];point[8]=points[8];point[9]=points[9];for(i=0;i<2*n;i++){point1[i]=point[i]+(x*sz);}construct1(point,point1);getch();break;}case 3:{int point[50],point1[50],i;float an;construct(points);printf("Enter the angle:\n");scanf("%f",&an);an=(float)an*3.14/180;for(i=0;i<=8;i+=2){point[i]=points[0]+(points[i]-points[0])*cos(an)-(points[i+1]-points[1])*sin(an);}for(i=1;i<=9;i+=2){point[i]=points[1]+(points[i]-points[1])*cos(an)-(points[i-1]-points[0])*sin(an);}for(i=0;i<=9;i+=2){point1[i]=points1[0]+(points1[i]-points1[0])*cos(an)-(points1[i+1]-points1[1])*sin(an);}for(i=1;i<=9;i+=2){point1[i]=points1[1]+(points1[i]-points1[1])*cos(an)-(points1[i-1]-points1[0])*sin(an);}construct1(point,point1);getch();

Page 48: cg record

break;}case 4:{exit(0);break;}}printf("Do you want to continue say(Y/N)?:");scanf("%s",&z);}while(z=='Y');getch();closegraph();}void construct(int points[50]){int x=25,n=5,i;cleardevice();for(i=0;i<2*n;i++){points1[i]=points[i]+x;}construct1(points,points1);}void construct1(int x[50],int y[50]){drawpoly(n,x);drawpoly(n,y);line(x[0],x[1],y[0],y[1]);line(x[2],x[3],y[2],y[3]);line(x[4],x[5],y[4],y[5]);line(x[6],x[7],y[6],y[7]);}OUTPUT:

Page 49: cg record
Page 50: cg record
Page 51: cg record
Page 52: cg record

Result: Thus the C program to implement the 3D-Transformations was written, executedand the output was verified successfully.

Composite 3D transformationsAim:To write a C program to implement 3 Dimensional Transformations.Algorithm:1. Enter the choice for transformation.2. Perform the translation, rotation, scaling of 3D object.3. Get the needed parameters for the transformation from the user.4. Increase of rotation, object can be rotated about x or y or z axis.5. Display the transmitted object in the screen.PROGRAM:#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>int maxx,maxy,midx,midy;void axis(){getch();cleardevice();line(midx,0,midx,maxy);line(0,midy,maxx,midy);}void main(){int gd,gm,x,y,z,o,x1,x2,y1,y2;detectgraph(&gd,&gm);initgraph(&gd,&gm,"d:\\tc\\bgi");setfillstyle(0,getmaxcolor());maxx=getmaxx();maxy=getmaxy();midx=maxx/2;midy=maxy/2;axis();bar3d(midx+100,midy-150,midx+60,midy-100,10,1);printf("\Enter the translation factor");scanf("%d%d",&x,&y);axis();printf("After translation");bar3d(midx+100,midy-150,midx+60,midy-100,10,1);bar3d(midx+x+100,midy-(y+150),midx+x+60,midy-(y+100),10,1);axis();bar3d(midx+100,midy-150,midx+60,midy-100,10,1);printf("Enter the scaling factor");scanf("%d%d%d",&x,&y,&z);axis();printf("After scaling");

Page 53: cg record

bar3d(midx+100,midy-150,midx+60,midy-100,10,1);bar3d(midx+(x*100),midy-(y*150),midx+(x*60),midy-(y*100),10*z,1);axis();bar3d(midx+100,midy-150,midx+60,midy-100,10,1);printf("Enter the rotation angle");scanf("%d",&o);x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);y1=50*sin(o*3.14/180)+100*cos(o*3.14/180);x2=60*cos(o*3.14/180)-90*sin(o*3.14/180);y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);axis();printf("After rotating about Z-axis");bar3d(midx+100,midy-150,midx+60,midy-100,10,1);bar3d(midx+x1,midy-y1,midx+x2,midy-y2,10,1);axis();printf("After rotating about x-axis");bar3d(midx+100,midy-150,midx+60,midy-100,10,1);bar3d(midx+100,midy-x1,midx+60,midy-x2,10,1);axis();printf("After rotating about Y-axis");bar3d(midx+100,midy-150,midx+60,midy-100,10,1);bar3d(midx+x1,midy-150,midx+x2,midy-100,10,1);getch();closegraph();}OUTPUT:

Page 54: cg record
Page 55: cg record
Page 56: cg record
Page 57: cg record
Page 58: cg record

Result:Thus the C program to implement the Composite 3D-Transformations was written, executed and the output was verified successfully.

EX NO : 10 GENERATING FRACTAL IMAGESDATE :Aim:To generate fractal images.Algorithm:Step1: The Sierpinski Triangle is created by infinite removalsStep2: Each triangle is divided into 4 smaller upside down trianglesStep3: The center of the 4 triangles is removedStep4: As the process is iterated infinite number of times, the total area of the set goes to infinity as the size of the each new triangle goes to zeroStep5: After closer examination magnification factor is 2.Step6: With each magnification there are 3 divisions of a triangleDimension D=ln(3)/ln(2)D=1.5850PROGRAM#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <math.h>#include <graphics.h>void DrawSierpinski(void);void main(void){int gd=VGA;int gm=VGAHI;initgraph(&gd, &gm, "\\tc\\bgi");DrawSierpinski();getch();}void DrawSierpinski(void){char Direct;int iterate;unsigned int x1, y1, x2, y2;x1 = x2 = 320;y1 = y2 = 0;for(iterate = 0; iterate < 10000; iterate++){Direct = random(3);if(Direct == 0){x1 = (x2 + 320) / 2;y1 = (y2 + 0) / 2;}

Page 59: cg record

else if(Direct == 1){x1 = (x2 + 0) / 2;y1 = (y2 + 480) / 2;}else if(Direct == 2){x1 = (x2 + 640) / 2;y1 = (y2 + 480) / 2;}putpixel(x1, y1, WHITE);x2 = x1;y2 = y1;}}

OUTPUT :

RESULT: Thus generation of Sierpinski triangle using the above program has been executed and outputis verified.