PRAVEEN KUMAR WELCOMES YOU

Featured Posts

Friday, 14 October 2011

OUTPUT PRIMITIVES


EX.NO:5          OUTPUT PRIMITIVES

Aim:

     To write a C Program to display the output primitives.

Algorithm:
1.           Start the  program .
2.           Initialize the variables.
3.           Call the  initgraph() function
4.           Set color for the output primitives.
5.           Using Outtextxy() display the choosen particular primitives.
6.           Using switch case mention the various primitives and their attributes.
7.           The various primitives are arc, line ,circle, rectangle and ellipse.
8.           close the graph and run the program.
9.           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,rad,sa,ea,xrad,yrad,i;
initgraph(&gd,&gm,"");
while(ch=='y')
{
cleardevice();
setbkcolor(9);
outtextxy(100,150,"Enter 1 to get 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;
case '6':
settextstyle(DEFAULT_FONT,0,2);
outtextxy(120,140,"VEL TECH");
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();
}
}

EX.NO:6              2 – DIMENSIONAL TRANSFORMATION

AIM
To perform the various 2-dimensional transformations such as translation,   rotation.

Algorithm
Step 1: Input the figure.
Step 2: Display the menu as 1.Translation  2.Rotation  3.Scaling 4.Shearing 5.reflection 6 ..Exit
Step 3: Get the choice from the user.
Step 4: If the choice is 1 get the translation vector. Add the translation vector to the original coordinate position to move to a new position.
Step 5: If the choice is 2 get the rotation angle. Rotate the figure with respect to the specified angle.
Step  6: If the choice is 3  get the scaling factor. scale the figure with respect to the specified scaling factor.
Step 7: If the choice is 4 get the shearing factor.and shear the e the figure with respect to the specified shearing factor
Step 8: If the choice is 5 get the reflecting  axis. Reflect  the figure with respect to the specified axis.
Step 9:  If choice is  6 exit the program.

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
void menu();
void input();
void output();
void translation();
void rotation();
void scaling();
void shearing();
void reflection();
int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y;
float sx,sy;

void menu()
{
printf("menu\n");
printf("1.Translation\n");
printf("2.rotation\n");
printf("3.scaling\n");
printf("4.shearing\n");
printf("5.reflection\n");
printf("6.exit\n");
printf("enter the choice:");
scanf("%d",&option);
switch(option)
{
case  1:
input();
translation();
break;
case 2:
input();
rotation();
break;
case 3:
input();
scaling();
break;

case 4 :
input();
shearing();
break;
case 5:
input();
reflection();
break;
case 6:
exit(0);
break;
}
}

void input()
{
printf("enter the number of vertices:" );
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the coordinates:");
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);
}
}


void output()
{
cleardevice();
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
}

void translation()
{
output();
printf("enter the tranformation vertex tx,ty:\n");
scanf("%d%d",&tx,&ty);
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]+tx;
a[i][1]=a[i][1]+ty;
}
output();
delay(10);
menu();
}

void rotation()
{
output();
printf("enter the rotating angle:");
scanf("%d",&y);
printf("enter the pivot point:");
scanf("%d%d",&fx,&fy);
k=(y*3.14)/180;
for(i=0;i<=n;i++)
{
a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k);
a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k);
}
output();
delay(10);
menu();
}

void scaling()
{
output();
printf("enter the scaling factor\n");
scanf("%f%f",&sx,&sy);
printf("enter the fixed point:");
scanf("%d%d",&fx,&fy);
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]*sx+fy*(1-sx);
a[i][1]=a[i][1]*sy+fy*(1-sy);
}
output();
delay(10);
menu();
}

void shearing()
{
output();
printf("enter the shear value:");
scanf("%d",&sh);
printf("enter the fixed point:");
scanf("%d%d",&fx,&fy);
printf("enter the axis for shearing if x-axis then 1 if y-axis the 0:");
scanf("%d",&axis);
for(i=0;i<=n;i++)
{
if(axis==1)
{
a[i][0]=a[i][0]+sh*(a[i][1]-fy);
}
else
{
a[i][1]=a[i][1]+sh*(a[i][0]-fx);
}
}
output();
delay(10);
menu();
}

void reflection()
{
output();
for(i=0;i<=n;i++)
{
temp=a[i][0];
a[i][0]=a[i][1];
a[i][1]=temp;
}
output();
delay(10);
menu();
}

void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tcplus\\bgi");
menu();
getch();
}


OUTPUT
 Menu
 1.             Translation
2.             Rotation
3.             Scaling
4.             Shearing
5.             Reflection
6.             Exit

TRANSLATION

Enter the choice : 1

Enter the number of Vertices: 3

Enter the coordinates :               30           150        10           200
Enter the coordinates :               10           200        60           200
Enter the coordinates :               60           200        30           150

Enter the translation vector Tx, Ty : 90            60

ROTATION

Enter the choice : 2
Enter the number of Vertices: 3
Enter the coordinates :       30           150        10           200
Enter the coordinates :       10           200        60           200
Enter the coordinates :       60           200        30           150

SCALING

Enter the choice : 3

Enter the number of Vertices: 3

Enter the coordinates :       30           150        10           200
Enter the coordinates :       10           200        60           200



REFLECTION

Enter the choice : 5

Enter the number of Vertices: 3

Enter the coordinates :       30           150       10          200
Enter the coordinates :       10           200        60           200
Enter the coordinates :       60           200        30           150]

Result:
Thus the program is executed successfully.







THREE DIMENSIONAL TRANSFORMATION


THREE DIMENSIONAL TRANSFORMATION
EX.NO:7

AIM
To perform the various 3-dimensional transformations such as translation, scaling, rotation.

Algorithm
Step 1: Input the figure.
Step 2: Display the menu as 1.Translation 2.Scaling 3.Rotation 4.Exit
Step 3: Get the choice from the user.
Step 4: If the choice is 1 a point or an object is translated from position P to position P' with the operation P'=T.P where tx,ty and tz specifying translation distances.
x'=x+ tx
y'=y+ ty
z'=z+ tz
Step 5: If the choice is 2 the scaling transformation of a position P can be written as P'=S.P where scaling parameters sx,sy and sz are assigned any positive values.
x'=x.sx
y'=y.sy
z'=z.sz
Step 6: If the choice is 3 get the rotation angle. Rotate the figure with respect to the axis of rotation.
Step 6a: About z axis rotation
x'=xcosӨ-ysinӨ
y'=xsinӨ+ycosӨ
z'=z
Rotation can be expressed as P'=Rz(Ө).P
Step 6b: About x axis rotation
y'=ycosӨ-zsinӨ
z'=ysinӨ+zcosӨ
x'=x
Rotation can be expressed as P'=Rx(Ө).P
Step 6c: About y axis rotation
z'=zcosӨ-xsinӨ
x'=zsinӨ+xcosӨ
y'=y
Rotation can be expressed as P'=Ry(Ө).P
Step 7: If choice is 4 exit the program.

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," ");
setfillstyle(0,getmaxcolor());
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter Translation Factor");
scanf("%d%d%d",&x,&y,&z);
axis();
printf("after translation");
bar3d(midx+(x+50),midy-(y+100),midx+x+60,midy-(y+90),5,1);
axis();
bar3d(midx+50,midy+100,midx+60,midy-90,5,1);
printf("Enter Scaling Factor");
scanf("%d%d%d",&x,&y,&z);
axis();
printf("After Scaling");
bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1);
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter Rotating Angle");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*cos(o*3.14/180)+100*sin(o*3.14/180);
x2=60*sin(o*3.14/180)-90*cos(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
axis();
printf("After Rotation about Z Axis");
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);
axis();
printf("After Rotation about X Axis");
bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);
axis();
printf("After Rotation about Y Axis");
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
getch();
closegraph();
}

OUTPUT
  Translation
  Enter Translation Factor : 50 60 70

COMPOSITE TRANSFORMATIONS


EX NO :8                                     COMPOSITE TRANSFORMATIONS

AIM
To perform the various 3-dimensional transformations such as translation, scaling, rotation.

Algorithm
A rotation matrix for any axis that does not coincide with a coordinate axis can be set up as a composite transformation involving combination of translations and the coordinate-axes rotations.

1.     Translate the object so that the rotation axis passes through the coordinate origin

2.     Rotate the object so that the axis rotation coincides with one of the coordinate axes

3.     Perform the specified rotation about that coordinate axis

4.     Apply inverse rotation axis back to its original orientation

5.     Apply the inverse translation to bring the rotation axis back to its original position


RESULT:
 Thus the program is executed successfully.

WINDOW TO VIEWPORT MAPPING


EX NO: 9                WINDOW TO VIEWPORT MAPPING

AIM:
To write a C program to perform Window to Viewport transformation.

Algorithm
Step1: Draw a world coordinate area selected for display called as window. This window defines what is to be viewed.
Step 2: Draw an area on a display device to which a window is mapped called as Viewport. The viewport defines where it is to be displayed.
Step 3: Now perform the mapping of a part of a world-coordinate scene to device coordinates referred as viewing transformation.


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,"c:\\tc\\bgi");
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);
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


RESULT:
 Thus the program is executed successfully.

COHEN-SUTHERLAND CLIPPING ALGORITHM


EX NO: 11                       COHEN-SUTHERLAND CLIPPING ALGORITHM
AIM:
         To clip a line using Cohen-Sutherland clipping algorithm.
Algorithm:
The method speeds up the processing of line segments by performing initial tests that reduce the number of intersections that must be calculated.
1.              Every line endpoint is assigned a four digit binary code, called region code, that identifies the location of the point relative to the boundaries of the clipping rectangle.
2.              Each bit position in the region code is used to indicate one of the four relative coordinate positions of the point with respect to the clip window.
Bit 1: left
Bit 2: right
Bit 3: below
Bit 4: above
3.              Bit values in the region code are determined by comparing endpoint coordinates values (x, y) with respect to the clip boundaries. eg.Bit 1 is set to 1 if x<xwmin
4.              Once we have established region codes for all line endpoints, we can quickly determine which lines are completely outside or inside the clip window.
5.              Lines that cannot be identified as completely inside or outside a clip window are checked for intersection with boundaries.
6.              Intersection points with a clipping boundary can be calculated using the slope-intercept form of the line equation.

7.              The y coordinate of the intersection point at vertical line
 
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
float cxl,cxr,cyt,cyb;
code(float ,float);
void clip(float ,float,float,float);
void  rect(float ,float,float,float);
 main()
    {
                        float x1,y1,x2,y2;
                        int g=0,d;
                        initgraph(&g,&d,"c:\\tc\\bin");
                        settextstyle(1,0,1);
                        outtextxy(40,15,"BEFORE CLIPPING");
                        printf("\n Please Enter Left,Bottom,Right,Top Of Clip Window");
                        scanf("%f%f%f%f",&cxl,&cyb,&cxr,&cyt);
rect(cxl,cyb,cxr,cyt);
                        getch();
printf("\n Enter  The Line Coordinate");
                        scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
line(x1,y1,x2,y2);
                        getch();
                        cleardevice();
                        settextstyle(1,0,1);
                        outtextxy(40,15,"AFTER CLIPPING");
                        clip(x1,y1,x2,y2);
                        getch();
                        closegraph();
   }

void clip(float x1,float y1,float x2,float y2)
 {
                        int c,c1,c2;
                        float x,y;
                        c1=code(x1,y1);
                        c2=code(x2,y2);
                        getch();
           


                        while((c1!=0)||(c2!=0))
                         {
                                    if((c1&c2)!=0)
                                    goto out;
                                    c=c1;
                                    if(c==0)
                                                c=c2;
                                    if((c&1)==1)
                                      {
                                                y=y1+(y2-y1)*(cxl-x1);
                                                x=cxl;
                                      }
                                    else
                                    if((c&2)==2)
                                      {
                                                y=y1+(y2-y1)*(cxl-x1)/(x2-x1);
                                                x=cxr;
                                      }
                                    else
                                    if((c&8)==8)
                                      {
                                                x=x1+(x2-x1)*(cyb-y1)/(y2-y1);
                                                y=cyb;
                                     }
                                 else
                                 if((c&4)==4)
                                     {
                                                x=x1+(x2-x1)*(cyt-y1)/(y2-y1);
                                                y=cyt;
                                     }
                                 if(c==c1)
                                    {
                                                x1=x;
                                                y1=y;
                                                c1=code(x,y);
                                    }
                                 else
                                    {
                                                x2=x;
                                                y2=y;
                                                c2=code(x,y);
                                    }
                         }
                       
out:
                                    rect(cxl,cyb,cxr,cyt);
                                    line(x1,y1,x2,y2);
    }

code(float x ,float y)
 {
                        int c=0;
                        if(x<cxl)                                  c=1;
                        else
                                    if(x>cxr)                     c=2;
                        else
                                    if(y<cyb)                    c=c|8;
                        else
                                    if(y>cyt)                     c=c|4;
                        return c;
}


void rect(float xl,float yb,float xr,float yt)
  {
                        line(xl,yb,xr,yb);
                        line(xr,yb,xr,yt);
                        line(xr,yt,xl,yt);
                        line(xl,yt,xl,yb);
 }
OUTPUT


BEFORE CLIPPING


Please Enter Left , Bottom , Right , Top Of The Clip window

200
200
400
400
           

Please Enter The Line Coordinates (X1, Y1, X2, Y2)

150
300
400
450

RESULT :     Thus the program is executed successfully.