Program: Output Primitives in C - Francis Xavier Engineering College

Transcription

Program: Output Primitives in C - Francis Xavier Engineering College
Francis Xavier Engineering College, Tirunelveli
Ex No :1(a)
OUTPUT PRIMITIVES
Aim:
To write a C program to display the output primitives.
Algorithm:
1.
2.
3.
4.
Initialize the variables.
Call the initgraph() function
Set color for the output primitives.
The various primitives are arc, line, circle, rectangle and ellipse. Use the graphics functions
to display the primitive structures.
5. Using outtextxy() display the chosen particular primitives.
6. Use closegraph() to free the memory and shut down the graphics system.
7. Compile and execute the program.
Description:
graphics.h:
This is a library file that contains definitions and explanations of graphics functions and constants.
initgraph():
This function allows to switch over to the graphics mode that offers the best resolution and stores it
in gm. The gm number tells us which monitor we are using, and its resolution, the number of video pages it
supports and colors that are available.
Graphics driver files are the files with BGI extension. The variable gd has been assigned to
DETECT, thereby asking initgraph() to figure out which BGI file is needed. This file is loaded into
memory on execution.
Fig 1. Sequence of Using the Graphics Primitives
1
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
List of Output primitives:
void line(int x1, int y1, int x2, int y2);
It draws a line between two specified points.
void rectangle(int left, int top, int right, int bottom);
It draws a rectangle in the current line style, thickness,
and drawing color.
void circle(int x, int y, int radius);
It draws a circle in the current drawing color.
void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);
It draws an elliptical arc in the current drawing color.
void sector(int x, int y, int stangle, int endangle, int xradius, int yradius);
It draws and fills an elliptical pie slice in the current drawing color.
void drawpoly(int numpoints, int *polypoints);
It draws a polygon using the current line style and color.
void outtextxy(int x, int y, char *textstring);
It displays a string at the specified location in graphics mode.
void arc(int x, int y, int stangle, int endangle, int radius);
It draws a circular arc in the current drawing color.
void putpixel(int x, int y, int color);
It plots a pixel at a specified point.
Program: Output Primitives in C
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int gd,gm;
int poly[12]={350,450,350,410,430,400,350,350,300,430,350,450};
gd=DETECT;
initgraph(&gd,&gm,"D:\\TC\\BGI");
circle(100,100,50);
outtextxy(75,170,"Circle");
rectangle(200,50,350,150);
2
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
outtextxy(240,170,"Rectangle");
ellipse(500,100,0,360,100,50);
outtextxy(480,170,"Ellipse");
line(100,250,540,250);
outtextxy(300,260,"Line");
sector(150,400,30,300,100,50);
outtextxy(120,460,"Sector");
drawpoly(6,poly);
outtextxy(300,460,"Polygon");
outtextxy(520,460,"Subbu Lakshmi");
getch();
closegraph();
}
Result:
Thus the C Program to display basic output primitives is written and executed.
3
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex No :1(b)
DRAWING STRUCTURES USING OUTPUT PRIMITIVES
Aim:
To write a C Program to draw the given shape using the output primitives.
Algorithm:
1.
2.
3.
4.
5.
6.
Initialize the variables.
Call the initgraph() function
Set color for the output primitives.
Using outtextxy() display the chosen particular primitives.
The various primitives are arc, line, circle, rectangle and ellipse.
Close the graph and run the program.
Program: Basic shapes using output primitives
#include<graphics.h>
#include<conio.h>
void main()
{
int gd,gm;
gd=DETECT;
initgraph(&gd,&gm,"C:\\TCC\\bgi");
circle(320,240,200);
ellipse(220,150,0,360,20,10);
ellipse(400,150,0,360,20,10);
line(320,240,360,280);
line(360,280,280,280);
line(280,280,320,240);
arc(320,300,220,320,100);
arc(120,240,90,280,30);
arc(520,240,280,90,30);
outtextxy(520,460,"JEGATHESAN");
getch();
closegraph();
}
4
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Output: Basic shapes using output primitives
Result:
Thus the C Program to draw given structures using basic output primitives is written and executed.
5
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex No: 2 (a)
IMPLEMENTATION OF DDA LINE DRAWING ALGORITHM
Aim
To write a C program to draw a line using DDA algorithm.
Description
The digital differential analyzer is a scan conversion algorithm based on calculation either ∆y or ∆x
using the following equations
∆y = m∆x , ∆x = ∆y / m
Sample the line at unit intervals in one coordinate and determine corresponding integer values nearest the
line path for the other coordinate.
Sample at X intervals (∆x = 1) and compute each successive Y value as Yk+1 = Yk + m
For lines with positive slope greater than 1, reverse the roles of X and Y. Sample at unit Y intervals (∆y =
1)and calculate each successive X value as Xk+1 = Xk + 1/m
Algorithm
Step 1: Input the line endpoints (x1, y1) and (x2, y2) . Store the left endpoint in (x,y)
Step 2: Calculate the values of ∆x and ∆y using ∆x = x2 – x1, ∆y = y2 – y1
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 plot the point(x,y)
Step 5: for k=1 to steps do
X = X + X increment
Y= Y + Y increment
putpixel(ceil(x), ceil(y),15)
6
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Program: DDA Line Drawing Algorithm
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int x1,y1,x2,y2,dx,dy,steps,gd,gm;
int xin,yin,x,y,xend,yend,k;
gd=DETECT;
initgraph(&gd,&gm,"D:\\tc\bgi");
printf("\t Line Drawing Algorithm - DDA");
printf("\n\nEnter the Coordinates: ");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
if(x1<x2)
{
x=x2;xend=x1;
y=y2;yend=y1;
}
else
{
x=x1;xend=x2;
y=y1;yend=y2;
}
xin=dx/steps;
yin=dy/steps;
putpixel(x,y,4);
for(k=0;k<steps;k++)
{
x=x+xin;
y=y+yin;
7
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
delay(50);
putpixel(x,y,4);
}
outtextxy(520,400,"JEGATHESAN");
getch();
closegraph();
}
Output: DDA Line Drawing Algorithm
8
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Output: DDA Line Drawing Algorithm
9
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Output: DDA Line Drawing Algorithm
10
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Output: DDA Line Drawing Algorithm
Result:
Thus the C Program to draw a line using DDA Line drawing Algorithm is written and executed.
11
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex.No :2(b ) IMPLEMENTATION OF BRESENHAM’S LINE DRAWING ALGORITHM
Aim
To write a C program to draw a line using Bresenham’s algorithm.
Description
In Bresenham’s approach the pixel position along a line path are determined by sampling unit X
intervals. Starting from the left end point(X0, Y0) of a given line we step to each successive columns and
plot the pixel whose scan line Y-value is closest to the line path. Assuming the Kth step in process,
determined that the pixel at(Xk, Yk)decide which pixel to plot in column Xk+1.The choices are (Xk+1, Yk)
and (Xk+1,Yk+1)
Algorithm
Step 1: Input the line endpoints (x1, y1) and (x2, y2) .Store the left endpoint in (x,y)
Step 2: Load (x,y) in to the frame buffer and plot it.
Step 3: Calculate constants ∆x, ∆y, 2∆y, and 2∆y -2∆x, and obtain the decision parameters as
P0 = 2 ∆y – ∆x
Step 4 : At each Xk along the line, starting at k = 0, perform the following test :
If Pk < 0, the next point to plot is (Xk+1, Yk) and
Pk+1 = Pk+2∆y
Otherwise, the next point to plot is (Xk+1, Yk+1) and
Pk+1 = Pk+2 ∆y - 2 ∆x
Step 5: Repeat step 4 ∆x times
12
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Program: Bresenham’s Line Drawing Algorithm
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int x1,y1,x2,y2,dx,dy,tdx,tdy,tdydx,gd,gm;
int xin,yin,x,y,xend,yend,p;
gd=DETECT;
initgraph(&gd,&gm,"D:\\tc\bgi");
printf("\t Line Drawing Algorithm - Bresenham's Algorithm");
printf("\n\nEnter the Coordinates: ");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
dx=x2-x1;
dy=y2-y1;
p=(2*dy)-dx;
tdy=2*dy;
tdx=2*dx;
tdydx=2*(dy-dx);
if(x1>x2)
{
x=x2;xend=x1;
y=y2;yend=y1;
}
else
{
x=x1;xend=x2;
y=y1;yend=y2;
}
putpixel(x,y,4);
while(x<xend)
{x++;
if(p<0)
p=p+tdy;
else
{y++;
p=p+tdydx;
}
13
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
putpixel(x,y,4);
}
outtextxy(520,400,"JEGATHESAN");
getch();
closegraph();
}
Output: Bresenham’s Line Drawing Algorithm
14
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Output: Bresenham’s Line Drawing Algorithm
Result
Thus the C Program to draw a line using Bresenham’s Line drawing Algorithm is written and
executed.
15
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex No: 3
IMPLEMENTATION OF MIDPOINT CIRCLE DRAWING ALGORITHM
Aim
To write a C program to draw a circle using Midpoint Circle algorithm.
Algorithm
Step 1:Input radius r and circle center(Xc, Yc)and obtain the first point on the circumference of a circle
centered on the origin as (X0, Y0) = (0, r)
Step 2: Calculate the initial values of the decision parameter as
P0 = 5/4 – r or P0 = 1 – r
Step 3: At each position starting at k perform the following test:
If Pk < 0, the next point to plot is (Xk+1, Yk) and Pk+1 = Pk+2 Xk+1 + 1
Otherwise the next point is (Xk+1, Yk-1) and Pk+1 = Pk+2 Xk+1 + 1- 2Yk+1
where 2Xk+1=2Xk+2 and 2Yk+1=2Yk-2
Step 4: Determine symmetry points in the other seven octants
Step 5: Move each pixel position(X, Y) onto the circular path centered on(Xc, Yc) and plot the coordinate
values as X = X + Xc Y = Y + Yc
Step 6: Repeat steps 3 through until X>=Y
16
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Program: Midpoint circle drawing algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void plotpoints(int,int,int,int);
void main()
{
int gd=DETECT,gm,x=0,y,p,r,xc,yc;
initgraph(&gd,&gm,"D:\\TCC\\BIN");
printf("\n Enter the Center Coordinates and Radius: ");
scanf("%d%d%d",&xc,&yc,&r);
y=r;
p=1-r;
plotpoints(xc,yc,x,y);
while(x<y)
{
x++;
if(p<0)
p=p+(2*x)+1;
else
{
y--;
p=p+(2*x)-(2*y)+1;
}
delay(50);
plotpoints(xc,yc,x,y);
}
outtextxy(520,460,"JEGATHESAN");
getch();
closegraph();
}
void plotpoints(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc+y,15);
17
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
putpixel(xc-x,yc-y,15);
putpixel(xc+y,yc+x,15);
putpixel(xc+y,yc-x,15);
putpixel(xc-y,yc+x,15);
putpixel(xc-y,yc-x,15);
}
Output: Midpoint circle drawing algorithm
Result:
Thus the C Program to draw a Circle using Midpoint Circle drawing Algorithm is written and
executed.
18
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex No : 4
IMPLEMENTATION OF MIDPOINT ELLIPSE DRAWING ALGORITHM
Aim
To write a C program to draw an Ellipse using midpoint ellipse algorithm.
Algorithm
Step 1: Input radius rx, ry and ellipse center (Xc, Yc) and obtain the first point on the circumference of a
circle centered on the origin as (X0, Y0) = (0, ry)
Step 2: Calculate the initial values of the decision parameter in region 1 as
P10 = r2y – r2x ry + 1/4 r2x
Step 3: At each Xk position in region 1, starting at k = 0, perform the following test:
If P1k < 0, the next point to plot is (Xk+1, Yk) and
P1k+1 = P1k+2 r2yXk+1 + r2y
Otherwise the next point is (Xk+1, Yk-1) and P1k+1 = P1k+2 r2yXk+1 - 2r2xYk+1 + r2y
With 2 r2yXk+1=2 r2yXk+ 2r2y
and
r2xYk+1=2r2xYk- 2r2x
Step 4: Calculate the initial values of the decision parameter in region 2 as
P20 = r2y(X0+1/2)2+ r2x(Y0 – 1)2- r2x r2y
Step 5: At each position starting at Yk position in region 2, starting at k = 0,
perform the following test:
If P2k > 0, the next point to plot is (Xk, Yk-1) and P2k+1 = P2k - 2 r2yYk+1 + r2x
Otherwise the next point is (Xk+1, Yk-1) and P2k+1 = P2k - 2 r2yXk+1 - 2r2xYk+1 + r2x
Step 6: Determine symmetry points in the other three octants
Step 7: Move each pixel position(X, Y) onto the circular path centered on
(Xc, Yc) and plot the coordinate values as
X = X + Xc Y = Y + Yc
Step 8: Repeat steps for region 1 until 2 r2yX>=2 r2xY
19
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Program: Midpoint ellipse drawing algorithm:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define Round(a) ((int) (a+0.5))
void plotpoint(int,int,int,int);
void main()
{
int gd,gm,rx2,ry2,trx2,try2,p,x=0,y,px=0,py,xc,yc,rx,ry;
gd=DETECT;
initgraph(&gd,&gm,"C:\\TCC\\BIN");
printf("\n Enter the Center coorinate: ");
scanf("%d%d",&xc,&yc);
printf("\n\n Enter the Minor & Major axis radius: ");
scanf("%d%d",&rx,&ry);
rx2=rx*rx;
ry2=ry*ry;
trx2=2*rx2;
try2=2*ry2;
y=ry;
py=trx2*y;
plotpoint(xc,yc,x,y);
p=round(ry2-(rx2*ry)+(0.25*rx2));
while(px<py)
{
x++;
px +=ry2+px;
if(p<0)
p +=ry2+px;
else
{
y--;
py -=ry2+px-py;
}
plotpoint(xc,yc,x,y);
}
p=round(ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2);
while(y>0)
{
y--;
20
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
py -=trx2;
if(p>0)
p +=try2-py;
else
{
x++;
px +=try2;
p +=rx2-py+px;
}
plotpoint(xc,yc,x,y);
}
outtextxy(520,460,"JEGATHESAN");
getch();
closegraph();
}
void plotpoint(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,15);
putpixel(xc-x,yc+y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc-y,15);
}
21
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Output: Midpoint Ellipse drawing algorithm:
Result:
Thus the C Program to draw an Ellipse using Midpoint Ellipse drawing Algorithm is written and
executed.
22
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex No: 5
IMPLEMENTATION OF LINE,CIRCLE & ELLIPSE ATTRIBUTES
Aim:
To write a C Program to display the various attributes of line, circle and ellipse.
Description:
Line()
line (x1,y1,x2,y2) :
Line function draws a line between two specified points (x,y) towards (x2,y2).This function comes handy if
you want to draw box like shapes or just plotting the graphs etc.
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.
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
FUNCTIONS USED:
Circle()
The function circle() is used to draw a circle using(x,y) as centre point.
Syntax:
circle (x,y,radius)
The other attributes with the command are as follows:
Setfillstyle(style,lc),setcolor(lc)
23
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
ELLIPSE DRAWING FUNCTIONS USED:
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:
1. Initialize the variables.
2. Call the initgraph() function
3. Set color for the output primitives.
4. Using Outtextxy() display the chosen particular primitives.
5. Include the various attributes of line, circle and ellipse.
6. Close the graph and run the program.
Program: Implementation of line, circle, ellipse attributes.
#include<graphics.h>
#include<conio.h>
void main()
{
int gd,gm;
gd=DETECT;
initgraph(&gd,&gm,"C:\\TCC\\bgi");
outtextxy(170,10,"Attributes_of_Output_Primitives");
setlinestyle(0,1,1);
line(50,50,200,50);
setlinestyle(1,1,1);
line(50,100,200,100);
setlinestyle(2,1,1);
line(250,50,400,50);
setlinestyle(3,1,1);
line(250,100,400,100);
setlinestyle(4,1,1);
line(450,50,600,50);
24
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
fillellipse(100,200,40,60);
setfillpattern("0XAA",15);
fillellipse(200,200,40,60);
setfillstyle(7,15);
fillellipse(300,200,40,60);
setfillpattern("0X77",15);
fillellipse(400,200,40,60);
setfillstyle(6,15);
fillellipse(500,200,40,60);
setcolor(YELLOW);
circle(300,350,50);
outtextxy(520,460,"JEGATHESAN");
getch();
closegraph();
}
Output: Implementation of line, circle, ellipse attributes.
25
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Result:
Thus the C Program for implementation of Line, Circle and Ellipse Attributes is written and
executed.
Ex No: 6(a)(i)
2 -D BASIC TRANFORMATIONS - TRANSLATION
Aim
To write a C Program to perform the various 2-dimensional basic transformation – Translation.
Algorithm
1. Input the object coordinates.
2. Translation: Enter the translation factors tx and ty.
3. Move the original coordinate position (x,y) to a new position
(x1,y1).ie. x=x+x1, y=y+y1.
4. Display the object after translation
Program: 2D Basic transformation-Translation
26
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
void input();
void output();
void translation();
int a[10][2],i,x,temp,tx,ty,k,y,n;
void input(){
printf("\t\t\tTranslation");
printf("\nEnter the number of vertices: " );
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the coordinates for the Line: ");
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);
}
}
void output()
{
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
}
void translation()
{
printf("\nEnter the tranformation vertex tx,ty: ");
scanf("%d%d",&tx,&ty);
cleardevice();
output();
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]+tx;
a[i][1]=a[i][1]+ty;
}
setlinestyle(2,1,1);
output();
27
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
delay(10);
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TCC\\bin");
input();
translation();
setlinestyle(0,1,1);
line(75,460,125,460);
outtextxy(130,460,"Before_Translation");
setlinestyle(2,1,1);
line(300,460,350,460);
outtextxy(355,460,"After_Translation");
outtextxy(520,460,"JEGATHESAN");
getch();
}
Output: 2D Basic transformation-Translation
28
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
29
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Result:
Thus the C Program to perform 2D basic Transformation – Translation is written and executed.
30
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex No: 6(a)(ii)
2 -D BASIC TRANFORMATIONS - ROTATION
Aim
To write a C Program to perform the various 2-dimensional basic transformation - Rotation.
Algorithm
Rotation
1. Input the object coordinates.
2. Enter the radian for rotation angle θ.
3. Rotate a point at position (x,y) through an angle θ about the origin
x1=xcos θ - ysin θ , y1=ycos θ + xsin θ.
4. Display the object after rotation
Program- 2D Basic transformation-Rotation
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
void input();
void output();
void rotation();
int a[10][2],i,x,option,temp,angle,fx,fy,sh,n,axis,y;
float k,tx,ty;
void input()
{
printf("\t\t\tRotation");
printf("\nEnter the number of vertices: " );
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the coordinates for the Line: ");
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);
}
}
void output()
{
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
31
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
}
}
void rotation()
{
printf("\nEnter the rotating angle: ");
scanf("%d",&y);
printf("\nEnter the pivot point:");
scanf("%d%d",&fx,&fy);
cleardevice();
output();
k=(y*3.14)/180;
for(i=0;i<=n;i++)
{
tx=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k);
ty=fy+(a[i][0]-fx)*sin(k)+(a[i][1]-fy)*cos(k);
a[i][0]=tx;
a[i][1]=ty;
}
setlinestyle(2,1,1);
output();
delay(10);
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TCC\\bin");
input();
rotation();
setlinestyle(0,1,1);
line(75,460,125,460);
outtextxy(130,460,"Before_Rotation");
setlinestyle(2,1,1);
line(300,460,350,460);
outtextxy(355,460,"After_Rotation");
outtextxy(520,460,"JEGATHESAN");
getch();
}
32
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Output: 2D Basic transformation-Rotation
33
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
34
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Result:
Thus the C Program to perform 2D basic Transformation – Rotation is written and executed.
Ex No: 6(a)(iii)
2 -D BASIC TRANFORMATIONS - SCALING
Aim
To write a C Program to perform the various 2-dimensional basic transformation - Scaling.
Algorithm
Scaling
1. Input the object coordinates.
a) Input the scaled factors sx and sy.
b) The transformed coordinates (x1,y1) , x1=x.sx and y1=y.sy.
c) Display the object after scaling
Program: 2D Basic transformation-Scaling
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
35
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
#include<stdlib.h>
void input();
void output();
void scaling();
int a[10][2],opt,i,x,fx,fy,option,temp,angle,k,n,axis,y;
float sx,sy;
void input()
{
printf("\t\t\tScaling");
printf("\nEnter the number of vertices: " );
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the coordinates for the Line: ");
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);
}
}
void output()
{
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
}
void scaling(){
printf("\nEnter the scaling factor: ");
scanf("%f%f",&sx,&sy);
printf("\nEnter 1 for fixed point Scaling otherwise enter 0: ");
scanf("%d",&opt);
if(opt == 1)
{
printf("\nEnter the fixed point: ");
scanf("%d%d",&fx,&fy);
cleardevice();
output();
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]*sx+fx*(1-sx);
a[i][1]=a[i][1]*sy+fy*(1-sy);
}
}
36
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
else
{
cleardevice();
output();
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]*sx;
a[i][1]=a[i][1]*sy;
}
}
setlinestyle(2,1,1);
output();
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TCC\\bin");
input();
scaling();
setlinestyle(0,1,1);
line(75,460,125,460);
outtextxy(130,460,"Before_Scaling");
setlinestyle(2,1,1);
line(300,460,350,460);
outtextxy(355,460,"After_Scaling");
outtextxy(520,460,"JEGATHESAN");
getch();
}
Output: 2D Basic transformation-Scaling
37
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
38
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Result:
Thus the C Program to perform other 2D basic Transformation – Scaling is written and executed.
39
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex No: 6(b)(i)
OTHER 2 -D TRANFORMATIONS - REFLECTION
Aim
To write a C Program to perform the various 2-dimensional basic transformation - Reflection.
Algorithm
Reflection
1. Input the Object Coordinates
2. Reflection can be performed about x axis and y axis.
a) Reflection about x axis : The transformed coordinates are x1=a and y1=-y.
b) Reflection about y axis : The transformed coordinates are x1=x and y1=y.
3. Display the object after reflection
Program : 2D Basic transformation-Reflection
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
void input();
void output();
void reflection();
int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y;
void input()
{
printf("\t\t\tReflection");
printf("\nEnter the number of vertices: " );
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the coordinates for the Line: ");
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);
}
}
void output()
{
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
40
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
}
}
void reflection()
{
input();
cleardevice();
output();
for(i=0;i<=n;i++)
{
temp=a[i][0];
a[i][0]=a[i][1];
a[i][1]=temp;
}
setlinestyle(2,1,1);
output();
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TCC\\bin");
reflection();
setlinestyle(0,1,1);
line(75,460,125,460);
outtextxy(130,460,"Before_Reflection");
setlinestyle(2,1,1);
line(300,460,350,460);
outtextxy(355,460,"After_Reflection");
outtextxy(520,460,"JEGATHESAN");
getch();
getch();
}
41
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Output: Other 2D transformation-Reflection
Result:
Thus the C Program to perform 2D basic Transformation – Reflection is written and executed.
42
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex No: 6(c)
OTHER 2 -D TRANFORMATIONS - SHEARING
Aim
To write a C Program to perform the various 2-dimensional basic transformation - Shearing.
Algorithm
Shearing
1. Input the object Coordinates
2. Input the shearing factors shx and shy.
3. (a)Shearing related to x axis : Transform coordinates x1=x+shx*y and y1=y.
(b) Shearing related to y axis : Transform coordinates x1=x and y1=y+shy*x.
4. Input the xref and yref values.
X axis shear related to the reference line y-yref is x1=x+shx(y-yref) and y1=y.
Y axis shear related to the reference line x=xref is x1=x andy1=y+shy(x-xref)
5. Display the object after shearing
Program: 2D Basic transformation-Shearing
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<stdlib.h>
void shearing();
void output();
int a[10][10],i,n,k=4,sh,fx,fy,axis;
void input()
{
printf("\t\t\tShearing");
printf("\nEnter the number of vertices: " );
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the coordinates for the Line: ");
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);
}
}
void output()
{
for(i=0;i<n;i++)
{
43
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
k=k+1;
}
void shearing()
{
printf("\nEnter the shear value: ");
scanf("%d",&sh);
printf("\nEnter the fixed point: ");
scanf("%d%d",&fx,&fy);
printf("\nEnter the axis for shearing if x-axis then 1 if y-axis the 0: ");
scanf("%d",&axis);
cleardevice();
output();
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();
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TCC\\bin");
input();
shearing();
setlinestyle(0,1,1);
line(75,460,125,460);
outtextxy(130,460,"Before_Shearing");
setlinestyle(2,1,1);
line(300,460,350,460);
outtextxy(355,460,"After_Shearing");
outtextxy(520,460,"JEGATHESAN");
getch();
44
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
}
Output: 2D Basic transformation-Shearing
45
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
46
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Result:
Thus the C Program to perform other 2D Transformation – Shearing is written and executed.
Ex No: 7
COMPOSITE 2D TRANSFORMATIONS
Aim
To write a C Program to perform the 2D composite transformation.
Description
Global, Local and Composite Transformations
Transformation can be divided into two categories based on their scope: global and local. In addition, there
are composite transformations. A global transformation is applicable to all items of a Graphics object. The
Transform property of the Graphics class is used to set global transformations. A composite transformation
is a sequence of transformations. For example, scaling followed by translation and rotation is a composite
translation. We can set up a matrix for any sequence of transformation matrix by calculating the matrix
product of individual transformations forming these products is called composition. Multiply matrices in
order from right to left. Each successive transformation matrix premultiplies the product of the preceding
transformation matrices.
Algorithm
47
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
1. Input the object coordinates.
2. Translation
a) Enter the translation factors tx and ty.
b) Move the original coordinate position (x,y) to a new position
(x1,y1).ie. x=x+x1, y=y+y1.
3. Rotation
a) Enter the radian for rotation angle θ.
b) Rotate a point at position (x,y) through an angle θ about the origin
x1=xcos θ - ysin θ , y1=ycos θ + xsin θ.
4. Scaling
a) Input the scaled factors sx and sy.
b) The transformed coordinates (x1,y1) , x1=x.sx and y1=y.sy.
5. Reflection
Reflection can be performed about x axis and y axis.
a) Reflection about x axis : The transformed coordinates are x1=a and y1=-y.
b) Reflection about y axis : The transformed coordinates are x1=x and y1=y.
6. Shearing
a) Input the shearing factors shx and shy.
b) Shearing related to x axis : Transform coordinates x1=x+shx*y and y1=y.
c) Shearing related to y axis : Transform coordinates x1=x and y1=y+shy*x.
d) Input the xref and yref values.
e) X axis shear related to the reference line y-yref is x1=x+shx(y-yref) and y1=y.
f) Y axis shear related to the reference line x=xref is x1=x and y1=y+shy(x-xref)
7. Finally display the transformed object after the successive transformations.
Program: Composite 2D transformation.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
void input();
void output();
void translation();
void rotation();
void scaling();
int a[10][2],i,x,temp,y,n,fx,fy,opt,ch,j=1;
int sx,sy;
int tx,ty;
48
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
float k;
void input()
{
printf("\t\t\tComposite transformation");
printf("\nEnter the sides of polygon: " );
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the coordinates for the Line: ");
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);}}
void output()
{
for(i=0;i<n;i++)
{line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);}}
void translation()
{
printf("\nEnter the tranformation vertex tx,ty: ");
scanf("%d%d",&tx,&ty);
cleardevice();
output();
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]+tx;
a[i][1]=a[i][1]+ty; }
setlinestyle(2,1,1);
output();
setlinestyle(0,1,1);
line(75,460,125,460);
outtextxy(130,460,"Before_Translation");
setlinestyle(2,1,1);
line(300,460,350,460);
outtextxy(355,460,"After_Translation");
outtextxy(520,460,"JEGATHESAN");
delay(100);}
void rotation()
{
printf("\nEnter the rotating angle: ");
scanf("%d",&y);
printf("\nEnter the pivot point:");
scanf("%d%d",&fx,&fy);
cleardevice();
49
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
output();
k=(y*3.14)/180;
for(i=0;i<=n;i++)
{
tx=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k);
ty=fy+(a[i][0]-fx)*sin(k)+(a[i][1]-fy)*cos(k);
a[i][0]=tx;
a[i][1]=ty;
}
setlinestyle(2,1,1);
output();
setlinestyle(0,1,1);
line(75,460,125,460);
outtextxy(130,460,"Before_Rotation");
setlinestyle(2,1,1);
line(300,460,350,460);
outtextxy(355,460,"After_Rotation");
outtextxy(520,460,"JEGATHESAN");
delay(100);
}
void scaling()
{
printf("\nEnter the scaling factor: ");
scanf("%d%d",&sx,&sy);
cleardevice();
output();
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]*sx;
a[i][1]=a[i][1]*sy;
}
setlinestyle(2,1,1);
output();
setlinestyle(0,1,1);
line(75,460,125,460);
outtextxy(130,460,"Before_Scaling");
setlinestyle(2,1,1);
line(300,460,350,460);
outtextxy(355,460,"After_Scaling");
outtextxy(520,460,"JEGATHESAN");
delay(100);
50
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TCC\\bin");
input();
do
{
printf("\nWhat Do you want to do with current object\n");
printf("\n1:Translation\n2:Rotation\n3:Scaling\n");
printf("\nEnter your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
translation();
break;
case 2:
rotation();
break;
case 3:
scaling();
break;
}
delay(6000);
closegraph();
initgraph(&gd,&gm,"C:\\TCC\\bin");
setlinestyle(0,1,1);
printf("To continue press 1 & to EXIT press 0: ");
scanf("%d",&j);
if(j==1)
{printf("\nThe Current object has the line Coordinates:");
for(i=0;i<n;i++)
{printf("\n");
printf("For line %d: ",i+1);
printf("%d %d %d %d",a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}}}while(j==1);
getch();
}
51
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Output: 2D Composite transformation.
52
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
53
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
54
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
55
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Result:
Thus the C Program to perform 2D composite transformation is written and executed.
56
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex No: 8
WINDOW TO VIEW PORT TRANSFORMATIONS
Aim
To write a C Program to perform the Window to Viewport Transformations.
Description:
Window - World coordinate area selected for display. It defines what is to be displayed.
Viewport – An area on display device where window is mapped. It defines where it is to be
displayed.
A point(xw,yw) in window is mapped to (xv,yv) in view port.
xv-xvmin/ xvmax-xvmin = xw-xwmin/xwmax-xwmin
yv-yvmin/ yvmax-yvmin = yw-ywmin/ywmax-ywmin
Program: Window to view port transformation:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
main()
{
float sx,sy;
int xwmin,ywmin,ywmax,xwmax,x1,y1,x2,y2,x3,y3,x4,y4,xvmin,xvmax,yvmax,yvmin;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TCC\\BGI");
printf("\t\t\tWindow to viewport transformation");
printf("\nEnter the co ordinates x1,y1,x2,y2,x3,y3: ");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
cleardevice();
xwmin=5;
ywmin=5;
xwmax=635;
ywmax=465;
rectangle(xwmin,ywmin,xwmax,ywmax);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
xvmin=425;
yvmin=75;
57
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
xvmax=550;
yvmax=250;
outtextxy(10,450,"Window");
outtextxy(428,78,"View_port");
sx=(float)(xvmax-xvmin)/(xwmax-xwmin);
sy=(float)(yvmax-yvmin)/(ywmax-ywmin);
rectangle(xvmin,yvmin,xvmax,yvmax);
x1=xvmin+floor(((float)(x1-xwmin)*sx)+0.5);
x2=xvmin+floor(((float)(x2-xwmin)*sx)+0.5);
x3=xvmin+floor(((float)(x3-xwmin)*sx)+0.5);
y1=yvmin+floor(((float)(y1-ywmin)*sy)+0.5);
y2=yvmin+floor(((float)(y2-ywmin)*sy)+0.5);
y3=yvmin+floor(((float)(y3-ywmin)*sy)+0.5);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
outtextxy(520,400,"JEGATHESAN");
getch();
return;
}
Output:
58
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
59
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Result:
Thus the C Program is written to perform Window to Viewport transformation and executed.
60
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex No: 9
IMPLEMENTATION OF COHEN-SUTHERLAND 2D LINE CLIPPING ALGORITHM
Aim
To write a C Program to implement Cohen-Sutherland 2D Line Clipping Algorithm.
Description:
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.
m  ( y  y ) (x  x )
2
1
1
7. The y coordinate
of the2 intersection
point at vertical line
y  y1  m( x  x1 )
8. The x coordinate of the intersection point at horizontal line
x  x1 
y  y1
m
y  yw
min
y  yw
max
61
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Program: Cohen Sutherland line clipping Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
float xwmin,xwmax,ywmax,ywmin;
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:\\TCC\\bgi");
settextstyle(1,0,1);
outtextxy(40,15,"Before_Clipping");
printf("\n\n\nEnter Left,Bottom,Right,Top Of Clip Window: ");
scanf("%f%f%f%f",&xwmin,&ywmin,&xwmax,&ywmax);
rect(xwmin,ywmin,xwmax,ywmax);
getch();
printf("\nEnter 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");
outtextxy(450,400,"JEGATHESAN_S");
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);
62
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
out:
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)*(xwmin-x1);
x=xwmin;
}
else if((c&2)==2)
{
y=y1+(y2-y1)*(xwmin-x1)/(x2-x1);
x=xwmax;
}
else if((c&8)==8)
{
x=x1+(x2-x1)*(ywmin-y1)/(y2-y1);
y=ywmin;
}
else if((c&4)==4)
{
x=x1+(x2-x1)*(ywmax-y1)/(y2-y1);
y=ywmax;
}
if(c==c1)
{
x1=x;
y1=y;
c1=code(x,y);
}
else
{
x2=x;
y2=y;
c2=code(x,y);
}}
rect(xwmin,ywmin,xwmax,ywmax);
line(x1,y1,x2,y2);
}
code(float x ,float y)
{
int c=0;
if(x<xwmin)
c=1;
63
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
else if(x>xwmax)
else if(y<ywmin)
else if(y>ywmax)
return c;
c=2;
c=c|8;
c=c|4;
}
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);
}
64
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Output
65
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
66
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Result:
Thus the C Program is written to implement Cohen-Sutherland 2D Line Clipping Algorithm and
executed.
Ex No: 10 IMPLEMENTATION OF SUTHERLAND HODGEMAN POLYGON CLIPPING ALGORITHM
Aim:
To Write a C Program to implement Sutherland Hodgeman Polygon Clipping Algorithm.
Description:
The Sutherland - Hodgman algorithm performs a clipping of a polygon against each window edge
in turn. It accepts an ordered sequence of verices v1, v2, v3, ..., vn and puts out a set of vertices defining
the clipped polygon.
This figure represents a polygon (the large, solid, upward pointing arrow) before clipping has occurred.
The following figures show how this algorithm works at each edge, clipping the polygon.
67
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
a. Clipping against the left side of the clip window.
b. Clipping against the top side of the clip window.
c. Clipping against the right side of the clip window.
d. Clipping against the bottom side of the clip window.
Four Types of Edges
As the algorithm goes around the edges of the window, clipping the polygon, it
encounters four types of edges. All four edge types are illustrated by the polygon in
the following figure. For each edge type, zero, one, or two vertices are added to the
output list of vertices that define the clipped polygon.
The four types of edges are:
1. Edges that are totally inside the clip window. - add the second inside vertex point
2. Edges that are leaving the clip window. - add the intersection point as a vertex
3. Edges that are entirely outside the clip window. - add nothing to the vertex output list
4. Edges that are entering the clip window. - save the intersection and inside
points as vertices
How To Calculate Intersections
Assume that we're clipping a polgon's edge with vertices at (x1,y1) and (x2,y2) against a clip window with
vertices at (xmin, ymin) and (xmax,ymax). The location (IX, IY) of the intersection of the edge with the
left side of the window is:
i. IX = xmin
ii. IY = slope*(xmin-x1) + y1, where the slope = (y2-y1)/(x2-x1)
The location of the intersection of the edge with the right side of the window is:
i. IX = xmax
ii. IY = slope*(xmax-x1) + y1, where the slope = (y2-y1)/(x2-x1)
The intersection of the polygon's edge with the top side of the window is:
i. IX = x1 + (ymax - y1) / slope
ii. IY = ymax
68
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Finally, the intersection of the edge with the bottom side of the window is:
i. IX = x1 + (ymin - y1) / slope
ii. IY = ymin
Program: Sutherland Hodgeman Polygon clipping algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int i,j=0,n;
int rx1,rx2,ry1,ry2;
void clip(float,float,float);
float x1[8],y1[8];
void main()
{
int gd=DETECT,gm;
int i,n;
float x[8],y[8],m;
69
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
clrscr();
initgraph(&gd,&gm,"d:\\tc\\bgi");
printf("Enter the Coordinates of rectangle: ");
scanf("%d%d%d%d",&rx1,&ry1,&rx2,&ry2);
printf("\nNo of sides for polygon: ");
scanf("%d",&n);
printf("\nEnter the Coordinates for Polygon: ");
for(i=0;i<n;i++)
{scanf("%f%f",&x[i],&y[i]);
}
cleardevice();
outtextxy(10,10,"Before_Clipping");
outtextxy(10,460,"Press any key");
outtextxy(520,460,"JEGATHESAN");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<n-1;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[i],y[i],x[0],y[0]);
getch();
cleardevice();
for(i=0;i<n-1;i++)
{
m=(y[i+1]-y[i])/(x[i+1]-x[i]);
clip(x[i],y[i],m);
clip(x[i+1],y[i+1],m);
}
m=(y[i]-y[0])/(x[i]-x[0]);
clip(x[i],y[i],m);
clip(x[0],y[0],m);
outtextxy(10,10,"After_Clipping");
outtextxy(10,460,"Prees_any_Key");
outtextxy(520,460,"JEGATHESAN");
rectangle(rx1,ry1,rx2,ry2);
getch();
for(i=0;i<j-1;i++)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
getch();
}
void clip(float e,float f,float m)
{
while(e<rx1||e>rx2||f<ry1||f>ry2)
70
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
{
if(e<rx1)
{
f+=m*(rx1-e);
e=rx1;
}
else if(e>rx2)
{
f+=m*(rx2-e);
e=rx2;
}
if(f<ry1)
{
e+=(ry1-f)/m;
f=ry1;
}
else if(f>ry2)
{
e+=(ry2-f)/m;
f=ry2;}}
x1[j]=e;
y1[j]=f;
j++;
}
Output:
71
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
72
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Result:
Thus the C Program is written to implement Sutherland Hodgeman Polygon Clipping Algorithm
and executed.
73
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex No: 11
3D BASIC TRANSFORMATIONS – TRANSLATION, ROTATION AND SCALING
Aim:
To write a C Program to perform 3D basic Transformations such as Translation, Rotation and
Scaling.
Description:
1. Translation
a) A point is transferred from position (x,y,z) to position (x1,y1,z1) with the matrix operation
X1
1 0 0 tx
x
Y1
=
0 1 0 ty + y
Z1
0 0 1 tz
z
1
000 1
1
b) The values are x1=x+tx ,y1=y+ty and z1=z+tz.
2. Rotation
a) Coordinate axes rotation
X1=x-cos θ-ysin θ
Y1=xsin θ+ycos θ
Z1=z
b) Y axis rotation
Z1=zcos θ-xsin θ
X1=zsin θ+xcos θ
Y1=y
c) X axis rotation
Y1=ycos θ-zsin θ
Z1=ysin θ+zcos θ
X1=x
3. Scaling
a) Translate the fixed point to the origin
b) Scale the object
X1
sx 0 0 0
x
Y1
=
0 sy 0 0 * y
Z1
0 0 sz 0
z
1
0 0 0 1
1
c) Translate the fixed point back to its original position
4. Finally display the object after the sequence of transformation
74
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Program: 3D Transformation
#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,y1,x2,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+50,midy-100,midx+60,midy-90,5,1);
printf("Enter the translation Factor: ");
scanf("%d%d%d",&x,&y,&z);
axis();
outtextxy(20,20,"After_the_translation");
bar3d(midx+(50+x),midy-(y+100),midx+(x+60),midy-(y+90),5,1);
outtextxy(520,460,"JEGATHESAN_S");
axis();
bar3d(midx+50,midy+100,midx+60,midy-90,5,1);
printf("Enter the Scaling factor: ");
scanf("%d%d%d",&x,&y,&z);
axis();
outtextxy(10,10,"After_Scaling");
bar3d(midx+(50*x),midy-(y*100),midx+(x*60),midy-(y*90),5,1);
75
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
outtextxy(520,460,"JEGATHESAN_S");
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter the 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();
outtextxy(10,10,"After_rotation_about_Z_axis");
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);
outtextxy(520,460,"JEGATHESAN_S");
axis();
outtextxy(10,10,"After_rotation_about_X_axis");
bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);
outtextxy(520,460,"JEGATHESAN_S");
axis();
outtextxy(10,10,"After_rotation_about_Y_axis");
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
outtextxy(520,460,"JEGATHESAN_S");
getch();
closegraph();
}
76
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Output: 3D Transformation
77
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
78
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
79
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
80
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Result:
Thus the C Program is written to perform 3D basic transformations such as translation, rotation and
scaling and executed.
Ex No: 12
3D COMPOSITE TRANSFORMATIONS
Aim:
To write a C Program to perform 3D basic Transformations such as Translation, Rotation and
Scaling.
Description:
1. Translation
a) A point is transferred from position (x,y,z) to position (x1,y1,z1) with the matrix operation
X1
1 0 0 tx
x
Y1
=
0 1 0 ty + y
Z1
0 0 1 tz
z
1
000 1
1
b) The values are x1=x+tx ,y1=y+ty and z1=z+tz.
2. Rotation
a) Coordinate axes rotation
X1=x-cos θ-ysin θ
Y1=xsin θ+ycos θ
Z1=z
b) Y axis rotation
81
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Z1=zcos θ-xsin θ
X1=zsin θ+xcos θ
Y1=y
c) X axis rotation
Y1=ycos θ-zsin θ
Z1=ysin θ+zcos θ
X1=x
3. Scaling
a) Translate the fixed point to the origin
b) Scale the object
X1
sx 0 0 0
x
Y1
=
0 sy 0 0 * y
Z1
0 0 sz 0
z
1
0 0 0 1
1
c) Translate the fixed point back to its original position
4. Finally display the object after the sequence of transformation
Program: 3D Composite Transformation
#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()
{
82
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
int gd,gm,x,y,z,o,x1,y1,x2,y2,c,no;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TCC\\bgi");
setfillstyle(0,getmaxcolor());
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
do
{
printf("\t\t\t\t3D-Composite Transformation");
printf("\n 1.TRANSLATION \n 2.SCALING \n 3.ROTATION \n 4.EXIT");
printf("\nEnter your choice");
scanf("%d",&c);
switch(c)
{
case 1:
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
outtextxy(50,50,"Enter_the_translation_Factor");
scanf("%d%d%d",&x,&y,&z);
axis();
outtextxy(50,50,"After_the_translation");
bar3d(midx+(50+x),midy-(y+100),midx+(x+60),midy-(y+90),5,1);
break;
case 2:
axis();
bar3d(midx+(50+x),midy-(y+100),midx+(x+60),midy-(y+90),5,1);
outtextxy(50,50,"Scaling_factor");
scanf("%d%d%d",&x,&y,&z);
axis();
outtextxy(50,50,"After_scaling");
bar3d(midx+(50*x),midy-(y*100),midx+(x*60),midy-(y*90),5,1);
break;
case 3:
axis();
bar3d(midx+(50*x),midy-(y*100),midx+(x*60),midy-(y*90),5,1);
printf("Enter the 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);
83
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
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();
outtextxy(50,50,"After_rotation_about_Z_axis");
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);
axis();
outtextxy(50,50,"After_rotation_about_X axis");
bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);
axis();
outtextxy(50,50,"After_rotation_about_Y_axis");
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
break;
}
delay(5000);
cleardevice();
printf("\n\nIf u want to continue enter 1 & to EXIT press 0:");
scanf("%d",&no);
}while(no==1);
getch();
closegraph();
}
Output:
84
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
85
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
86
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
87
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
88
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
89
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Result:
Thus the C Program is written to perform 3D Composite transformations and executed.
90
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex No: 13(a)
COLOR MODEL CONVERISON – RGB to HSV Model
Aim:
To write a C Program to perform Color model Conversion from RGB color model to HSV color
model.
Description:
The RGB color is used directly by most computer devices by expressing the color as combination
of red, green and blue. A commonly used colors space that corresponds more naturally to human
perception is HSV model. HSV color space corresponds to Hue, Saturation and Value. The formula for
conversion of RGB to HSV depends on which RGB component is largest and which is smallest. The
diagonal of the RGB hue corresponds to V axis of the HSV. The relative distance of a point from V axis
represent S.
Algorithm:
 Obtain the values for RGB component.
 Find the Min and Max value.
 Compute the value V which is equal to maximum of RGB.
 Normalize value V to 1.
 Compute saturation that is either 0.0 or {max-min/max}
 Normalize saturation S to 1.
 Compute Hue(H)
 Return H,S,V values.
91
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Program: RGB TO HSV Conversion
#include<math.h>
#define MIN(a,b) (a<b?a:b)
#define MAX(a,b) (a>b?a:b)
#define NO_HUE -1
void rgbtohsv(float r,float g,float b,float *h,float *s,float *v)
{
float max=MAX(r,MAX(g,b));
float min=MIN(r,MIN(g,b));
float delta=max-min;
*v=max;
if(max!=0.0)
*s=delta/max;
else
*s=0.0;
if(*s==0.0) *h=NO_HUE;
else
{
if(r==max)
*h=(g-b)/delta;
else if(g==max)
*h=2+(b-r)/delta;
else if(b==max)
*h=4+(r-g)/delta;
*h*=60.0;
if(*h<0)*h+=360.0;
*h/=360.0;
}
printf("\nH= %f S= %f V= %f",*h,*s,*v);
}
void main()
{
float a,b,c,d,e,f;
clrscr();
printf("\t\t\tColor Conversion - RGB to HSV");
printf("\n\nEnter the RGB and HSV Values: ");
scanf("%f%f%f%f%f%f",&a,&b,&c,&d,&e,&f);
rgbtohsv(a,b,c,&d,&e,&f);
92
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
printf("\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t\JEGATHESAN");
getch();
}
Output: RGB TO HSV Conversion
Result:
Thus the C Program is written to perform Color Model Conversion from HSV to RGB color model
and executed
93
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex No: 13(b)
COLOR MODEL CONVERISON – HSV to RGB Color Model
Aim:
To write a C Program to perform Color model Conversion from HSV color model to RGB color
model.
Description:
The RGB color is used directly by most computer devices by expressing the color as combination
of red, green and blue. A commonly used colors space that corresponds more naturally to human
perception is HSV model. HSV color space corresponds to Hue, Saturation and Value. The transformation
from HSV to RGB is obtained by the inverse of the equations in RGB to HSV procedure. These operations
are carried out for each position of the hexagon.
Program: HSV to RGB Conversion
#include"stdio.h"
#include"conio.h"
#include"math.h"
void hsvtorgb(float h,float s,float v,float *r,float *g,float *b)
{
float aa,bb,cc,f;
int i;
if(s==0)
*r=*g=*b;
else
{
if(h==1.0)
h=0;
h*=6.0;
i=floor(h);
f=h-i;
aa=v*(1-s);
bb=v*(1-(s*f));
cc=v*(1-(s*(1-f)));
switch(i)
{
case 0: *r=v; *g=cc; *b=aa;
printf("\nR= %f G= %f B= %f",*r,*g,*b);
break;
case 1: *r=bb; *g=v; *b=aa;
printf("\nR= %f G= %f B= %f",*r,*g,*b);
break;
94
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
case 2: *r=aa; *g=v; *b=cc;
printf("\nR= %f G= %f B= %f",*r,*g,*b);
break;
case 3: *r=aa; *g=bb; *b=v;
printf("\nR= %f G= %f B= %f",*r,*g,*b);
break;
case 4: *r=cc; *g=aa; *b=v;
printf("\nR= %f G= %f B= %f",*r,*g,*b);
break;
case 5:
*r=v;
*g=aa;
*b=bb;
printf("\nR= %f G= %f B= %f",*r,*g,*b);
break;
}
}
}
void main()
{
float a,b,c,d,e,f;
clrscr();
printf("\t\n\t\tColor Conversion - HSV to RGB");
printf("\n\nEnter HSV to RGB values: ");
scanf("%f%f%f%f%f%f",&a,&b,&c,&d,&e,&f);
hsvtorgb(a,b,c,&d,&e,&f);
printf("\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t\JEGATHESAN");
getch();
}
95
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Output: HSV to RGB Conversion
Result:
Thus the C Program is written to perform Color Model Conversion from HSV to RGB color model
and executed.
96
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex No: 14
ANIMATION USING C – GREETING CARD
Aim:
To write a C Program to animate a Greeting card.
Description:
Using the output primitives like rectangle(),circle() and line(), the shape of the object is defined.
With a loop the coordinates of the object are changed and it is displayed with a time delay using delay(t)
function. This gives an illusion of animation. The color of the font and textstyle are changed using
setfontstyle() and setcolor() function.
Program: Animation in C
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y;
int i,j,kk;
//detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TCC\\bgi");
setcolor(WHITE);
line(0,400,640,400);
rectangle(300,330,340,400);
rectangle(310,320,330,330);
setcolor(4);
line(319,280,319,398);
line(320,280,320,398);
rectangle(320,280,330,300);
outtextxy(340,280,"PRESS ANY KEY TO IGNITE THE ROCKET");
getch();
for(j=400;j<640;j++)
{
cleardevice();
setcolor(WHITE);
line(0,j,640,j);
rectangle(300,j-70,340,j);
97
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
rectangle(310,j-80,330,j-70);
setcolor(RED);
line(319,280,319,400);
line(320,280,320,400);
rectangle(320,280,330,300);
setcolor(YELLOW);
circle(325,300,2);
delay(5);
}
for(i=400;i>340;i--)
{
cleardevice();
setcolor(RED);
line(319,i,319,i-120);
line(320,i,320,i-120);
rectangle(320,i-120,330,i-100);
setcolor(YELLOW);
circle(325,i-100,2);
delay(25);
}
cleardevice();
kk=0;
for(j=100;j<350;j++)
{
if(j%20==0)
{
setcolor(kk);
kk=kk+3;
delay(50);
}
ellipse(320,30,0,360,j+100,j+0);
}
for(j=100;j<350;j++)
{
if(j%20==0)
{
setcolor(BLACK);
delay(2);
}
98
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
ellipse(320,30,0,360,j+100,j+0);
}
cleardevice();
for(i=0;i<70;i++)
{
setcolor(15);
settextstyle(2,0,6);
outtextxy(110,150,"HAPPY NEW YEAR");
delay(90);
}
getch();
}
Output: Animation in C
99
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
100
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Result: Thus the C Program is written to animate a Greeting Card , executed and Verified.
101
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex No: 15
PERSPECTIVE PROJECTION OF 3D OBJECT
Aim:
To write a C Program to perform perspective projection of 3D object.
Description:
1. To visualize 3D image on 2D monitor by using parallel projection , using the formula.
2. Input will be three co-ordinates(x,y,z)
3. Oblique projection can be obtained by knowing L values
L=
Z
=ZL1
tan α
4. To compute the persceptive projection the equation will be
Xp=x[(zprp-zvp)/(zprp-z)]=x[dp/zprp-z]
Yp=y[(zprp-zvp)/(zprp-z)]=y[dp/zprp-z]
Where zprp – projection reference point along X-axis.
5. Plot the Xp and Yp to get the perceptive projection.
Algorithm
Step1: Get the coordinates to draw the cube.
Step 2: Read the reference point.
Step 3: Read the view plane.
Step 4: For a perspective projection object positions are transformed to the view plane along lines that
converge to a point called the projection reference point.
Step 5: The projected view of an object is determined by calculating the intersection point of the projection
lines with the view plane.
102
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Program: Perspective Projection of 3D Object
#include<stdio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int x1,y1,x2,y2,gd,gm;
int ymax,a[4][8];
float par[4][4],b[4][8];
int i,j,k,m,n,p;
int xp,yp,zp,x,y,z;
a[0][0]=100;a[1][0]=100;a[2][0]=-100;
a[0][1]=200;a[1][1]=100;a[2][1]=-100;
a[0][2]=200;a[1][2]=200;a[2][2]=-100;
a[0][3]=100;a[1][3]=200;a[2][3]=-100;
a[0][4]=100;a[1][4]=100;a[2][4]=-200;
a[0][5]=200;a[1][5]=100;a[2][5]=-200;
a[0][6]=200;a[1][6]=200;a[2][6]=-200;
a[0][7]=100;a[1][7]=200;a[2][7]=-200;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TCC\\bgi");
ymax=getmaxy();
xp=300;yp=320;zp=100;
for(j=0;j<8;j++)
{
x=a[0][j];
y=a[1][j];
z=a[2][j];
b[0][j]=xp-((float)(x-xp)/(z-zp))*(zp);
b[1][j]=yp-((float)(y-yp)/(z-zp))*(zp);
}
for(j=0;j<3;j++)
{
x1=(int)b[0][j];
y1=(int)b[1][j];
x2=(int)b[0][j+1];
y2=(int)b[1][j+1];
line(x1,ymax-y1,x2,ymax-y2);
103
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
}
x1=(int)b[0][3];
y1=(int)b[1][3];
x2=(int)b[0][0];
y2=(int)b[1][0];
line(x1,ymax-y1,x2,ymax-y2);
setlinestyle(1,1,1);
for(j=4;j<7;j++)
{
x1=(int)b[0][j];
y1=(int)b[1][j];
x2=(int)b[0][j+1];
y2=(int)b[1][j+1];
line(x1,ymax-y1,x2,ymax-y2);
}
x1=(int)b[0][7];y1=(int)b[1][7];
x2=(int)b[0][4];y2=(int)b[1][4];
line(x1,ymax-y1,x2,ymax-y2);
setlinestyle(2,1,1);
for(i=0;i<4;i++)
{
x1=(int)b[0][i];y1=(int)b[1][i];
x2=(int)b[0][4+i];y2=(int)b[1][4+i];
line(x1,ymax-y1,x2,ymax-y2);
}
getch();
}
104
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Output:
Result:
Thus the C Program is written and executed for Perspective projection of a object and output is
verified.
105
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex.No:16
STUDY OF OPENGL PROGRAMMING
Aim:
To Study about OPENGL – features, Primitives and Programming basics.
Introduction:
OpenGL is a powerful software interface used to produce high-quality computer-generated images
and interactive graphics applications by rendering 2D and 3D geometric objects, bitmaps, and color
images.
OpenGL is designed as a streamlined, hardware-independent interface to be implemented on many
different hardware platforms. To achieve these qualities, no commands for performing windowing tasks or
obtaining user input are included in OpenGL; instead, you must work through whatever windowing system
controls the particular hardware you're using. Similarly, OpenGL doesn't provide high-level commands for
describing models of three-dimensional objects. Such commands might allow you to specify relatively
complicated shapes such as automobiles, parts of the body, airplanes, or molecules. With OpenGL, you
must build up your desired model from a small set of geometric primitives - points, lines, and polygons.
A sophisticated library that provides these features could certainly be built on top of OpenGL. The
OpenGL Utility Library (GLU) provides many of the modeling features, such as quadric surfaces and
NURBS curves and surfaces. GLU is a standard part of every OpenGL implementation. Also, there is a
higher-level, object-oriented toolkit, Open Inventor, which is built atop OpenGL, and is available
separately for many implementations of OpenGL.
Table 1-1 : Command Suffixes and Argument Data Types
Suffix
Data Type
Typical C-Language Type
OpenGL Type
b
8-bit integer
signed char
GLbyte
s
16-bit integer
short
GLshort
i
32-bit integer
int or long
GLint, GLsizei
f
32-bit
point
floating-
float
GLfloat, GLclampf
d
64-bit
point
floating-
double
GLdouble,
GLclampd
106
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
ub
8-bit
integer
us
ui
unsigned
unsigned char
GLubyte, GLboolean
16-bitunsigned
integer
unsigned short
GLushort
32-bit
integer
unsigned int or unsigned long
GLuint,GLenum,
GLbitfield
unsigned
glVertex2i(1, 3);
glVertex2f(1.0, 3.0);
are equivalent, except that the first specifies the vertex's coordinates as 32-bit integers
The following lines show how you might use a vector and a nonvector version of the command that sets the
current color:
glColor3f(1.0, 0.0, 0.0);
GLfloat color_array[] = {1.0, 0.0, 0.0};
glColor3fv(color_array);
GLUT, the OpenGL Utility Toolkit
GLUT to simplify opening windows, detecting input, and so on. If you have an implementation of OpenGL
and GLUT on your system, the examples in this book should run without change when linked with them.
Five routines perform tasks necessary to initialize a window.




glutInit(int *argc, char **argv) initializes GLUT and processes any command line arguments (for
X, this would be options like -display and -geometry). glutInit() should be called before any other
GLUT routine.
glutInitDisplayMode(unsigned int mode) specifies whether to use an RGBA or color-index color
model. You can also specify whether you want a single- or double-buffered window. (If you're
working in color-index mode, you'll want to load certain colors into the color map; use
glutSetColor() to do this.) Finally, you can use this routine to indicate that you want the window to
have an associated depth, stencil, and/or accumulation buffer. For example, if you want a window
with double buffering, the RGBA color model, and a depth buffer, you might call
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH).
glutInitWindowPosition(int x, int y) specifies the screen location for the upper-left corner of your
window.
glutInitWindowSize(int width, int size) specifies the size, in pixels, of your window.
107
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli

int glutCreateWindow(char *string) creates a window with an OpenGL context. It returns a unique
identifier for the new window. Be warned: Until glutMainLoop() is called (see next section), the
window is not yet displayed.
#include <GL/gl.h>
#include <GL/glut.h>
void display(void)
{/* clear all pixels */
glClear (GL_COLOR_BUFFER_BIT);
/* draw white polygon (rectangle) with corners at
* (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
*/
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
/* don't wait!
* start processing buffered OpenGL routines
*/
glFlush ();
}
void init (void)
{
/* select clearing (background) color
glClearColor (0.0, 0.0, 0.0, 0.0);
*/
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
/*
108
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
* Declare initial window size, position, and display mode * (single buffer and RGBA). Open window
with "hello" in its title bar. Call initialization routines.
* Register callback function to display graphics.
* Enter main loop and process events.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0; /* ISO C requires main to return int. */
}
Handling Input Events
You can use these routines to register callback commands that are invoked when specified events occur.



glutReshapeFunc(void (*func)(int w, int h)) indicates what action should be taken when the
window is resized.
glutKeyboardFunc(void (*func)(unsigned char key, int x, int y)) and glutMouseFunc(void
(*func)(int button, int state, int x, int y)) allow you to link a keyboard key or a mouse button with a
routine that's invoked when the key or mouse button is pressed or released.
glutMotionFunc(void (*func)(int x, int y)) registers a routine to call back when the mouse is moved
while a mouse button is also pressed.
Drawing Three-Dimensional Objects
GLUT includes several routines for drawing these three-dimensional objects:
cone
icosahedron
teapot
cube
octahedron
tetrahedron
dodecahedron
sphere
torus
109
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
You can draw these objects as wireframes or as solid shaded objects with surface normals defined. For
example, the routines for a cube and a sphere are as follows:
void glutWireCube(GLdouble size);
void glutSolidCube(GLdouble size);
void glutWireSphere(GLdouble radius, GLint slices, GLint stacks);
void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks);
Animation
Most OpenGL implementations provide double-buffering - hardware or software that supplies two
complete color buffers. One is displayed while the other is being drawn. When the drawing of a frame is
complete, the two buffers are swapped, so the one that was being viewed is now used for drawing, and vice
versa.
Procedure
open_window_in_double_buffer_mode();
for (i = 0; i < 1000000; i++) {
clear_the_window();
draw_frame(i);
swap_the_buffers();
}
Result:
The basics of OPENGL Programming is thus studied.
110
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex.No: 17
DRAWING 3D OBJECTS AND SCENES
Aim : To Write a OpenGL Program to display 3 D Objects and 3D Scenes.
Description:
The following primitives are used to display 3D objects.
void glutWireCube(GLdouble size);
void glutSolidCube(GLdouble size);
void glutWireSphere(GLdouble radius, GLint slices, GLint stacks);
void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks);
glPushMatrix() copies the matrix on the top of the stack. This would be like a Save function
glPopMatrix() would be the equivalent of load. This gets rid of the top matrix in the stack, and sets the
matrix underneath it as the new current matrix. So, a glPopMatrix() could undo immediately the result of
several or even hundreds of transformations
Program:
#include <gl/glut.h>
void init (void)
{
GLfloat light_ambient[] = { 1.0, 1.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv (GL_LIGHT0, GL_POSITION, light_position);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
111
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
glPushMatrix ();
glRotatef (20.0, 1.0, 0.0, 0.0);
glPushMatrix ();
glTranslatef (-0.75, 0.5, 0.0);
glRotatef (90.0, 1.0, 0.0, 0.0);
glutSolidTorus (0.275, 0.85, 15, 15);
glPopMatrix ();
glPushMatrix ();
glTranslatef (-0.75, -0.5, 0.0);
glRotatef (270.0, 1.0, 0.0, 0.0);
glutSolidCone (1.0, 2.0, 15, 15);
glPopMatrix ();
glPushMatrix ();
glTranslatef (0.75, 0.0, 1.0);
glutSolidSphere (1.0, 15, 15);
glPopMatrix (); glPopMatrix ();
glFlush ();
}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
if (w <= h)
glOrtho (-2.5, 2.5, -2.5*(GLfloat)h/(GLfloat)w,
2.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-2.5*(GLfloat)w/(GLfloat)h,
2.5*(GLfloat)w/(GLfloat)h, -2.5, 2.5, -10.0, 10.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutCreateWindow (argv[0]);
init ();
glutReshapeFunc (reshape);
glutDisplayFunc(display);
112
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
glutMainLoop();
return 0;
}
Output: 3D objects
Result :
Thus the 3D Objects and scenes are drawn using Opengl.
113
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Ex .No : 18
GENERATING FRACTAL IMAGES – SIERPINSKI GASKET
Aim :
To Write a Opengl Program for Fractal Generation. - Sierpinski Gasket
Description:
Fractal is "a rough or fragmented geometric shape that can be split into parts, each of which is (at least
approximately) a reduced-size copy of the whole,"[1] a property called self-similarity.
1.The sierpinski Triangle is created by infinite removals
2.Each triangle is divided into 4 smaller upside down triangles
3.The center of the 4 triangles is removed
4. 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 zero
5.After closer examination magnification factor is 2.
6.With each magnification there are 3 divisions of a triangle
Dimension D=ln(3)/ln(2) D=1.5850
Program:
#include <stdlib.h>
#include <GL/glut.h>
typedef float point[3];
/* initial tetrahedron */
point v[]={{0.0, 0.0, 1.0}, {0.0, 0.942809, -0.33333}, {-0.816497, -0.471405, -0.333333}, {0.816497, 0.471405,-0.333333}};
static GLfloat theta[] = {0.0,0.0,0.0};
int n;
void triangle( point a, point b, point c)
/* display one triangle using a line loop for wire frame, a single
114
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
normal for constant shading, or three normals for interpolative shading */
{
glBegin(GL_POLYGON);
glNormal3fv(a);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}
void divide_triangle(point a, point b, point c, int m){
/* triangle subdivision using vertex numbers
righthand rule applied to create outward pointing faces */
point v1, v2, v3;
int j;
if(m>0) {
for(j=0; j<3; j++) v1[j]=(a[j]+b[j])/2;
for(j=0; j<3; j++) v2[j]=(a[j]+c[j])/2;
for(j=0; j<3; j++) v3[j]=(b[j]+c[j])/2;
divide_triangle(a, v1, v2, m-1);
divide_triangle(c, v2, v3, m-1);
divide_triangle(b, v3, v1, m-1);
}
else(triangle(a,b,c)); /* draw triangle at end of recursion */
}
void tetrahedron( int m)
{
/* Apply triangle subdivision to faces of tetrahedron */
glColor3f(1.0,0.0,0.0);
divide_triangle(v[0], v[1], v[2], m);
glColor3f(0.0,1.0,0.0);
divide_triangle(v[3], v[2], v[1], m);
glColor3f(0.0,0.0,1.0);
divide_triangle(v[0], v[3], v[1], m);
glColor3f(0.0,0.0,0.0);
divide_triangle(v[0], v[2], v[3], m);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
tetrahedron(n);
115
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
glFlush();
}
void myReshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
else
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
void main(int argc, char **argv)
{
n=4;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("3D Gasket");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor (1.0, 1.0, 1.0, 1.0);
glutMainLoop();
}
116
http://www.francisxavier.ac.in
Francis Xavier Engineering College, Tirunelveli
Output: Sierpinski gasket
Result:
Thus an OpenGl Program for Fractal Generation. - Sierpinski Gasket is written, executed and
output is verified.
117
http://www.francisxavier.ac.in