Friday, 14 October 2011

COMPOSITE TRANSFORMATIONS-3D


EX NO 12                             COMPOSITE TRANSFORMATIONS-3D

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.

SUTHERLAND HODGEMAN ALGORITHM


EX NO :13          SUTHERLAND HODGEMAN ALGORITHM
 AIM:       
 To clip a Polygon using Sutherland Hodgeman Algorithm.
 Algorithm
1.           Input Coordinates of all vertices of the polygon
2.           Input coordinates of the clipping window
3.           Consider the left edge of the window
4.           Compare the vertices of each edge of the polygon , individually with the clipping plane
5.           Save the resulting intersections and vertices in the new list of vertices according to four possible relationships between the edge and the clipping boundary discussed earlier
6.           Repeat the steps 4 and 5 for remaining edges of the clipping window. Each time the resultant list of vertices is successively passed to process the next edge of the clipping window
7.           Stop

RESULT :     Thus the program is executed successfully.

Thursday, 13 October 2011

CIRCLE DRAWING USING MIDPOINT ALGORITHM


Ex no:3  CIRCLE  DRAWING  USING MIDPOINT ALGO.
Date:       

Aim:
     To write a C++ Program to draw a circle using MIdpoint algorithm.

Algorithm:
Step1: Start the program.

Step2: Initialize the variables.

Step3: Call the initgraph() function

Step4: Get the values for centre end points for drawing a circle

Step5: Calculate the distance to be traveled by the circle.

Step6: Put the pixels in the given color to draw the circle.

Step7: Display the output.

Step8:   Stop the program

PROGRAM
#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "graphics.h"
main()
{
intgd=DETECT,gm;
intxcenter,ycenter,radius;
intp,x,y;
initgraph(&gd,&gm,"c:\\tc\\bgi");
x=0;
printf("Enter The Radius Value:\n");
scanf("%d",&radius);
y=radius;
printf("Enter The xcenter and ycenter Values:\n");
scanf("%d%d",&xcenter,&ycenter);
plotpoints(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;
plotpoints(xcenter,ycenter,x,y);
}
getch();
return(0);
}
intplotpoints(intxcenter,intycenter,intx,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);
return(0);
}

OUTPUT
Enter the Radius Value                       :             80                            
Enter The xcenter and ycenterValues :           230         260



LINE DRAWING USING BRESENHAMS ALGORITHM


EX .NO:2                  LINE DRAWING USING BRESENHAMS ALGORITHM
Date:
AIM:
            To write a C program to draw a line using Bresenhams algorithm.
ALGORITHM:

Step1: Start.

Step2:Declare variables x,y,x1,y1,x2,y2,p,dx,dy and also declare gdriver=DETECT,    
          Mode.

Step3: Initialize the graphic mode with the path location in TC folder

Step4: Input the two line end-points and store the left end-points in (x1,y1).

Step5: Load (x1, y1) into the frame buffer; that is, plot the first point put x=x1,y=y1

Step6: Calculate dx=x2-x1 and dy=y2-y1, and obtain the initial value of decision  
          Parameter p as: a.p=(2dy-dx).

Step7: Starting from first point (x,y) perform the following test:

Step8: Repeat step 9 while(x<=x2).

Step9: If p<0, next point is (x+1,y) and p=(p+2dy).

Step10: Otherwise, the next point to plot is (x+1,y+1) and p=(p+2dy-2dx).

Step11: Place pixels using putpixel at points (x,y) in specified colour.

Step12: Close Graph

Step13: Stop.

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,y1,x2,y2,p,dx,dy;
intgdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\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=(dy-dx);
while(x<=x2)
{   if(p<0)
{   x=x+1;
    p=2*x-dx;
}
 else
{
x=x+1;
y=y+1;
p=p+2*dy;
}
putpixel(x,y,7);
}
getch();
closegraph();
}

OUTPUT:
Enter the x-coordinate of the first point: 200
Enter the y-coordinate of the first point: 300
Enter the x-coordinate of the second point: 300
Enter the y-coordinate of the second point: 300
RESULT:
                      Thus the program of line drawing using Bresenhams  algorithm is executed and output is obtained.

EX .NO:1                LINE DRAWING USING DDA ALGORITHM
DATE:

AIM:
To write a C program to draw a line using DDA algorithm.
ALGORITHM:
Step 1: Input the line endpoints and store the left endpoint in (x1, y1) and right
endpoint in (x2, y2)
Step 2: Calculate the values of ∆x and ∆y using ∆x = xb – xa, ∆y = yb – ya
Step 3: if the values of ∆x > ∆y assign values of steps as ∆x otherwise the values
of steps as ∆y
Step 4: Calculate the values of X increment and Y increment and assign
the value x= xa and y = ya
Step 5: for k=1 to steps do
X = X + X increment
Y= Y + Y increment
Putpixel(ceil(x), ceil(y),15)

PROGRAM:
#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "graphics.h"
main()
{
intgd=DETECT,gm;
intxa,xb,ya,yb;
intdx,dy,steps,k,xinc,yinc,x,y;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enter the two left end pixel points(xa,ya):\n");
scanf("%d%d",&xa,&ya);
printf("Enter the two Right end pixel points(xb,yb):\n");
scanf("%d%d",&xb,&yb);
dx=xb-xa;
dy=yb-ya;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=dx/steps;
yinc=dy/steps;
x=xa;
y=ya;
putpixel(x,y,6);
for(k=1;k<=steps;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,6);


}
getch();
return(0);
}
OUTPUT:
Enter the two left end pixel points(xa,ya):\n")
scanf("%d%d",&xa,&ya);
printf("Enter the two Right end pixel points(xb,yb):\n");
scanf("%d%d",&xb,&yb);
Enter The Two Left Endpoints(xa,ya)   :       100 200
 Enter The Two Right Endpoints(xb,yb) :      200   200

RESULT:
Thus the program of line drawing using DDA algorithm is executed and output is obtained .

COMPUTER GRAPHICS LAB



SUB CODE
   : CS2405   SUBJECT     : COMPUTER GRAPHICS LAB
                 INDEX
S.NO
TITLE OF THE EXPERIMENT
1
Line using DDA algorithm .
2
Line using Bresenhams algorithm.
3
Circle using Bresenhams algorithm.
4
Ellipse using Bresenhamsalgorithm .
5.
Output Primitives
6.
Two Dimensional transformation.
7.
Three  Dimensional transformation.
8.
Window to Viewport mapping
9.
Cohen Sutherland and clipping Algorithm
10.
Sutherland Hodgeman clipping algorithm.
11.
Drawing point and line using  OPENGL
12.
Drawing 3D scene using OPENGL
13
2D composite transformation     
14
3D composite transformation

Wednesday, 12 October 2011

OPERATIONS ON COMPLEX NUMBERS USING FILES AS STORAGE


OPERATIONS ON COMPLEX NUMBERS USING FILES AS STORAGE

AIM:
To write a C++ program to perform addition of complex numbers using files.

ALGORITHM:

Step 1: Start the program.
Step 2: Create a class called COMPLEX and create its members: real and imaginary.
Step 3: Generate the random numbers by using rand() function.
Step 4: Write the randomly generated numbers in a file called “complex1.txt”.
Step 5: Add the two complex numbers.
Step 6: Store the Resultant complex number in another file called “result.txt”
Step 7: Display the resultant value.
Step 8: Stop the program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
#include<time.h>
class complex
{
public:
int real;
int imag;
complex(int r, int i)
{
real = r;
imag = i;
}
complex()
{
real = imag = 0;
}
void display(void);
};
void complex::display(void)
{
cout<<real<<((imag<0)?"-i":"+i")<<imag<<"\n";
}
void main()
{
clrscr();
ofstream ocom("complex1.txt");
float real,imag;
time_t ti;
srand((unsigned) time(&ti));
real = rand()%100;
imag = rand()%100;
ocom<<"("<<real<<((imag<0)?"-i":"+i")<<imag<<")"<<"+";
real = rand()%100;
imag = rand()%100;
ocom<<"("<<real<<((imag<0)?"-i":"+i")<<imag<<")"<<"\n";
ocom.close();
ifstream icom("complex1.txt");
char no,t,ch,op;
icom>>no;
icom>>real;
icom>>ch;
icom>>no;
icom>>imag;
imag=(ch=='+')?imag:-imag;
icom>>no;
icom>>op;
complex a(real,imag);
icom>>no;
icom>>real;
icom>>ch;
icom>>no;
icom>>imag;
imag=(ch=='+')?imag:-imag;
icom>>no;
icom>>op;
complex b(real,imag);
complex c;
switch(op)
{
case '+':
c.real = a.real+b.real;
c.imag = a.imag+b.imag;
break;
case '-':
c.real = a.real-b.real;
c.imag = a.imag-b.imag;
break;
case '*':
c.real = (a.real*b.real)-(a.imag*b.imag);
c.imag = (a.real*b.imag)+(a.imag*b.real);
break;
case '/':
float qt;
qt = b.real*b.real+b.imag*b.imag;
c.real = (a.real*b.real+a.imag*b.imag)/qt;
c.imag = (a.imag*b.real-a.real*b.imag)/qt;
break;
default:
cout<<"\n Invalid";
}
cout<<"\n complex 1:";
a.display();
cout<<"\n complex 2:";
b.display();
cout<<"\n Resultant complex:";
c.display();
ofstream out("result.txt");
out<<"("<<c.real<<((c.imag<0)?"-i":"+i")<<c.imag<<")";
out.close();
}

OUTPUT:
complex1.txt:
(0+i72)+(39+i71)
result.txt
(39+i143)

RESULT:
Thus the program for addition of complex numbers using files was executed.