Graphics Manual 12
-
Upload
rajaraman-govindasamy -
Category
Documents
-
view
218 -
download
0
Transcript of Graphics Manual 12
-
8/4/2019 Graphics Manual 12
1/72
REG NO: 40508104067
LINE DRAWING USING DDA ALGORITHM
AIM:
To write a C program to draw a line using DDA algorithm.
ALGORITHM:
1. Start the program.2. Read the starting and ending coordinates xa, ya, xb, and yb.3. Find the x-coordinate difference and y-coordinate difference.
dx=xb-xa & dy=yb-ya.
4.Compare the difference and decide the step value.if(dx>dy)
step=dx
else
step=dy
5. Find the increment values of coordinates.xi=dx/step
yi=dy/step
6. Display the starting point using the function putpixel(xa,ya,4).7. Find adjacent pixels using the formula xa=xa+xi & ya=ya+yi.8.
Repeat the steps till reaching the endpoints i.e, ya=yb & xa=xb.
9. Stop the program.
-
8/4/2019 Graphics Manual 12
2/72
REG NO: 40508104067
PROGRAM:
#include#include
#include
#include
void main()
{
int gd=DETECT,gm;
float xadj,yadj,xa,ya,xb,yb,dx,dy,step,xi,yi,d,t1,t2;
initgraph(&gd,&gm,"");
d=1;printf("Enter the starting coordinates:");
scanf("%f %f",&xa,&ya);
printf("Enter the ending coordinates:");
scanf("%f %f",&xb,&yb);
dx=xb-xa;
dy=yb-ya;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xi=dx/step;
yi=dy/step;
while(d==1)
{
putpixel(xa,ya,4);
xadj=xa+xi;
yadj=ya+yi;
xa=xadj;
ya=yadj;if((xa==xb)&&(ya==yb))
d=0;
}
getch();
closegraph();
}
-
8/4/2019 Graphics Manual 12
3/72
REG NO: 40508104067
OUTPUT:
Enter the starting coordinates: 100 100
Enter the ending coordinates: 200 200
RESULT:
Thus a line is drawn successfully using DDA algorithm in C.
-
8/4/2019 Graphics Manual 12
4/72
REG NO: 40508104067
LINE DRAWING USING BRESENHAMS ALGORITHM
AIM:
To write a program in C to draw a line using Bresenhams
algorithm.
ALGORITHM:
1. Start the program.2. Read the starting and ending coordinates xa, ya, xb, yb.3. Find the x-coordinate difference and y-difference.
dx=xb-xa & dy=yb-ya
4. Calculate decision parameter p value.p= 2dy-dx
5. Fix the starting and ending coordinates.xstart=xa
if(xa
-
8/4/2019 Graphics Manual 12
5/72
REG NO: 40508104067
PROGRAM:
#include#include
#include
void main()
{
int gd=DETECT,gm;
int xa, xb, ya, yb, dx, dy, x, y, xstart, ystart, xend, yend, p;
initgraph(&gd,&gm,""); clrscr();
printf("Enter the xa & ya value:"); scanf("%d %d",&xa,&ya);
printf("Enter the xb & yb value:"); scanf("%d %d",&xb,&yb);dx=abs(xa-xb);
dy=abs(ya-yb);
p=2*dy-dx;
if(xa
-
8/4/2019 Graphics Manual 12
6/72
REG NO: 40508104067
OUTPUT:
Enter the xa & ya value: 200 200Enter the xb & yb value: 350 450
RESULT:
Thus a line is drawn successfully using Bresenhams algorithm in C.
-
8/4/2019 Graphics Manual 12
7/72
REG NO: 40508104067
CIRCLE DRAWING USING BRESENHAMS CIRCLE ALGORITHM
AIM:
To draw a circle using Bresenhams circle drawing algorithm in C.
ALGORITHM:
1. Start the program.2. Get the radius and center of the circle r, xc, yc.3. Obtain the first point on the circumference of a circle centered on the origin
as (X0,Y0)=(0,r)
4. Calculate the initial value of the decision parameter as p =5/4-r5. At each xk position, starting at k=0, perform the following test
if(pk=y.9.
Stop the program.
-
8/4/2019 Graphics Manual 12
8/72
REG NO: 40508104067
PROGRAM:
#include#include
#include
float xc, yc, r, x, y, p;
void plotpoint();
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
printf("Enter the xc value:");scanf("%f",&xc);
printf("Enter the yc value:");
scanf("%f",&yc);
printf("Enter the radius:");
scanf("%f",&r);
x=0;
y=r;
plotpoint();
p=1-r;
while(x
-
8/4/2019 Graphics Manual 12
9/72
REG NO: 40508104067
void plotpoint()
{
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,1);
putpixel(xc+x,yc-y,1);
put pixel(xc-x,yc-y,1);
putpixel(xc+y,yc+x,1);
putpixel(xc-y,yc+x,1);
putpixel(xc+y,yc-x,1);
putpixel(xc-y,yc-x,1);
}
-
8/4/2019 Graphics Manual 12
10/72
REG NO: 40508104067
OUTPUT:
Enter the xa value: 200Enter the ya value: 200
Enter the radius: 50
RESULT:
Thus a circle is drawn successfully using Bresenhams circle drawing
algorithm in C.
-
8/4/2019 Graphics Manual 12
11/72
REG NO: 40508104067
ELLIPSE DRAWING ALGORITHM
AIM:
To write a program in C to draw an ellipse using midpoint ellipse
drawing algorithm.
ALGORITHM:
1. Start the program.2. Get the radius rx, ry and center of the ellipse xc, yc.3. Obtain the first point on the ellipse centered on the origin as (x0,y0)=(0,ry)4. Calculate the initial value of the decision parameter in region1 as
p10=ry2-rx
2ry+1/4rx
2.
5. At each xk position in region1,starting at k=0,perform the following testIf(p1k0), the next point along the ellipse centered on (0,0) is (xk, yk+1) and
p2k+1=p2k-2rx2yk+1+rx
2
Otherwise the next point along the ellipse is (xk+1,yk+1) andP2k+1=p2k+2ry
2xk+1+rx
2-2rx
2yk+1, using the same incremental
calculations for x & y as in region2.
8. Determine the symmetry points in other three quadrants.9. Move each calculated pixel position (x,y) onto the elliptical path centered on
(xc,yc) and plot the coordinate values, x=x+xc & y=y+yc.
10.Repeat the steps for region1 until 2ry2x>2rx2y.11.Stop the program.
-
8/4/2019 Graphics Manual 12
12/72
REG NO: 40508104067
PROGRAM:
#include#include
#include
#define round(a) (int)(a+0.5)
float xc, yc, x, y;
void plotpoint();
void main()
{
int gd=DETECT,gm;
float rx, ry, p, px, py, rx2, ry2, tworx2, twory2;initgraph(&gd,&gm,"");
clrscr();
printf("Enter the xc value:");
scanf("%f",&xc);
printf("Enter the yc value:");
scanf("%f",&yc);
printf("Enter the x radius:");
scanf("%f",&rx);
printf("Enter the y radius:");
scanf("%f",&ry);
ry2=ry*ry;
rx2=rx*rx;
twory2=2*ry2;
tworx2=2*rx2;
x=0;
y=ry;
plotpoint();
p=round(ry2 - rx2 * ry + (0.25 * rx2));
px=0;py=tworx2*y;
while(px=0){
y=y-1;
py=py-tworx2; }
-
8/4/2019 Graphics Manual 12
13/72
REG NO: 40508104067
if(p0)
{
y=y-1;
py=py-tworx2;
if(p0)
p=p+rx2-py;
else
p=p+rx2-py+px;
plotpoint();
}
getch();
closegraph();
}
void plotpoint()
{
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,1);
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,1);
}
-
8/4/2019 Graphics Manual 12
14/72
REG NO: 40508104067
OUTPUT:
Enter the xa value: 200Enter the ya value: 200
Enter the x radius: 100
Enter the y radius: 50
RESULT:
Thus an ellipse is drawn successfully using midpoint ellipse drawing
algorithm in C.
-
8/4/2019 Graphics Manual 12
15/72
REG NO: 40508104067
IMPLEMENTATION OF LINE, CIRCLE AND ELLIPSE ATTRIBUTES
AIM:
To write a C Program to display the various output primitives with its
attributes.
ALGORITHM:
1. Start the program.2. Initialize the variables.3. Call the initgraph() function4. Set color for the output primitives.5. Using outtextxy() display the chosen particular primitives.6. Using switch cases 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.
-
8/4/2019 Graphics Manual 12
16/72
REG NO: 40508104067
PROGRAM:
#include#include
#include
#include
void main()
{
char ch;
int gd=DETECT,gm,x1,y1,x2,y2,rad,sa,ea,xrad,yrad,i;
initgraph(&gd,&gm,"");
printf("enter the choice:"); scanf("%c",ch);
do
{
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':
setlinestyle(SOLID_LINE,0,1);
line(100,200,300,400);
break;
case '2':setfillstyle(SOLID_FILL,8);
circle(200,200,75);
break;
case '3':
setfillstyle(7,4);
bar(100,300,200,100);
break;
-
8/4/2019 Graphics Manual 12
17/72
REG NO: 40508104067
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,"Graphics");
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;
}
}
while(ch=='y'|| ch=='Y');
getch();
}
-
8/4/2019 Graphics Manual 12
18/72
REG NO: 40508104067
OUTPUT:
Enter Choice: yEnter 1 to get line
2. Circle
3. Box
4. Arc
5. Ellipse
6. Rectangle
7. Exit
1 2
-
8/4/2019 Graphics Manual 12
19/72
REG NO: 40508104067
RESULT:
Thus the program for implementing line, circle and ellipse attributes was
executed successfully.
Graphics
4
6
-
8/4/2019 Graphics Manual 12
20/72
REG NO: 40508104067
-
8/4/2019 Graphics Manual 12
21/72
REG NO: 40508104067
BASIC 2D TRANSFORMATIONS
AIM:
To write a program to perform the basic 2D transformations like
translation, rotation and scaling using transformation equation in C.
ALGORITHM:
1. Start the program.2. Obtain the coordinates of the line xa,ya,xb,yb.3. Get the translation factors tx, ty, rotation angle a & scaling factors sx, sy.4. Find the translated coordinates by applying the angle as
x1=x+tx & y
1=y+ty.
5. Get the rotation coordinates by applying the angle asX
1=abs(xa-xb)cosa-abs(ya-yb)sina
Y1
=abs(xa-xb)sina+abs(ya-yb)cosa
6. Scaling is applied asX
1=x*sx
Y1=y*sy
7. Draw the transformed line with the new coordinates (x1,y1).8. Stop the program.
-
8/4/2019 Graphics Manual 12
22/72
REG NO: 40508104067
PROGRAM:
#include#include
#include
#include
void getdata();
int x1, y1, x2, y2;
void main()
{
int gd=DETECT,gm;
int x3, x4, y3, y4, tx, ty, ch;float sx, sy, a, t;
initgraph(&gd,&gm,"");
clrscr(); setcolor(1);
do
{
printf("1.Translation\t2.Scaling\t3.Rotation\t4.Exit\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
getdata();
printf("Enter the translation factor in x-axis:");
scanf("%d",&tx);
printf("Enter the translation factor in y-axis:");
scanf("%d",&ty);
x3=x1+tx; x4=x2+tx;
y3=y1+ty; y4=y2+ty;
line(x3,y3,x4,y4);
getch(); cleardevice();break;
case 2:
getdata();
printf("Enter the scaling factor in x-axis:");
scanf("%f",&sx);
printf("Enter the scaling factor in y-axis:");
scanf("%f",&sy);
x3=x1*sx;
-
8/4/2019 Graphics Manual 12
23/72
REG NO: 40508104067
x4=x2*sx;
y3=y1*sy;
y4=y2*sy;
line(x3,y3,x4,y4);
getch();
cleardevice();
break;
case 3:
getdata();
printf("Enter the rotation angle:");
scanf("%f",&a);
t=a*(3.14159/180);x4=x1+abs(x2-x1)*cos(t)-abs(y2-y1)*sin(t);
y4=y1+abs(x2-x1)*sin(t)+abs(y2-y1)*cos(t);
line(x1,y1,x4,y4);
getch();
cleardevice();
break;
case 4:
exit(0);
break;
}
}while(ch
-
8/4/2019 Graphics Manual 12
24/72
REG NO: 40508104067
OUTPUT:
1.Translation 2.Scaling 3.Rotation 4.ExitEnter the choice: 1
Enter the x1 value: 100
Enter the y1 value: 100
Enter the x2 value: 200
Enter the y2 value: 200
Enter the translation factor in x-axis: 50
Enter the translation factor in y-axis: 0
1.Translation 2.Scaling 3.Rotation 4.Exit
Enter the choice: 2
Enter the x1 value: 100
Enter the y1 value: 100
Enter the x2 value: 200
Enter the y2 value: 200
Enter the scaling factor in x-axis: 1
Enter the scaling factor in y-axis: 2
-
8/4/2019 Graphics Manual 12
25/72
REG NO: 40508104067
1.Translation 2.Scaling 3.Rotation 4.Exit
Enter the choice: 3
Enter the x1 value: 100Enter the y1 value: 100
Enter the x2 value: 200
Enter the y2 value: 200
Enter the rotation angle: 45
RESULT:
Thus the program for performing basic 2D transformations using
transformation equation is successfully executed in C.
-
8/4/2019 Graphics Manual 12
26/72
REG NO: 40508104067
2D TRANSFORMATIONS (REFLECTION AND SHEARING)
AIM:
To write a program to perform the other 2D transformations like
reflection and shearing in C.
ALGORITHM:
1. Start the program.2. For reflection obtain the coordinates of the triangle xa, ya, xb, yb, xc, yc.3. Calculate the reflection as, x`=x+2*(320-x) y`=y+2*(240-y)4. For shearing obtain the coordinates of the square xa, ya, xb, yb, xc, yc, xd, yd.5. Shearing points can be calculated as x`=x+shx & y`=y+shy, where shx, shy
are the shearing factors.
6. Draw the transformed objects with the new coordinates (x`, y`).7. Stop the program.
-
8/4/2019 Graphics Manual 12
27/72
REG NO: 40508104067
PROGRAM:
#include#include
#include
void reflection();
void shearing();
void main()
{
int gd=DETECT,gm,ch;
initgraph(&gd,&gm,"");
do{
cleardevice();
printf("1.Reflection\t2.Shearing\t3.Exit\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
cleardevice();
reflection();
break;
case 2:
cleardevice();
shearing();
break;
case 3:
exit(0);
break;
}
}while(ch
-
8/4/2019 Graphics Manual 12
28/72
REG NO: 40508104067
printf("Enter the xb&yb values:");
scanf("%d\n%d",&xb,&yb);
printf("Enter the xc&yc values:");
scanf("%d\n%d",&xc,&yc);
line(xa,ya,xb,yb);
line(xb,yb,xc,yc);
line(xc,yc,xa,ya);
do
{
printf("1.About x-axis\t2.About y-axis\t3.About both\t4.exit\nEnter the
choice:");
scanf("%d",&ch);switch(ch)
{
case 1:
cleardevice();
line(xa,ya,xb,yb);
line(xb,yb,xc,yc);
line(xc,yc,xa,ya);
y1=ya+2*(getmaxy()/2-ya);
y2=yb+2*(getmaxy()/2-yb);
y3=yc+2*(getmaxy()/2-yc);
line(xa,y1,xb,y2);
line(xb,y2,xc,y3);
line(xc,y3,xa,y1);
break;
case 2:
cleardevice();
line(xa,ya,xb,yb);
line(xb,yb,xc,yc);
line(xc,yc,xa,ya);
x1=xa+2*(getmaxy()/2-xa);
x2=xb+2*(getmaxy()/2-xb);
x3=xc+2*(getmaxy()/2-xc);
line(x1,ya,x2,yb);
line(x2,yb,x3,yc);
line(x3,yc,x1,ya);
break;
-
8/4/2019 Graphics Manual 12
29/72
REG NO: 40508104067
case 3:
cleardevice();
line(xa,ya,xb,yb);
line(xb,yb,xc,yc);
line(xc,yc,xa,ya);
x1=xa+2*(getmaxy()/2-xa);
x2=xb+2*(getmaxy()/2-xb);
x3=xc+2*(getmaxy()/2-xc);
y1=ya+2*(getmaxy()/2-ya);
y2=yb+2*(getmaxy()/2-yb);
y3=yc+2*(getmaxy()/2-yc);
line(x1,y1,x2,y2);line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
break;
case 4:
break;
}
}
while(ch
-
8/4/2019 Graphics Manual 12
30/72
REG NO: 40508104067
switch(ch)
{
case 1:
cleardevice();
line(xa,ya,xb,yb);
line(xb,yb,xc,yc);
line(xc,yc,xd,yd);
line(xd,yd,xa,ya);
printf("Enter the shearing factor for x:");
scanf("%f",&shx);
xe=xc+shx;
xf=xd+shx;line(xa,ya,xb,yb);
line(xb,yb,xe,yc);
line(xe,yc,xf,yd);
line(xf,yd,xa,ya);
break;
case 2:
cleardevice();
printf("Enter the shearing factor for y:");
scanf("%f",­);
ye=yb+shy;
yf=yc+shy;
line(xa,ya,xb,ye);
line(xb,ye,xc,yf);
line(xc,yf,xd,yd);
line(xd,yd,xa,ya);
break;
case 3:
cleardevice();
printf("Enter the shearing factor for x:");
scanf("%f",&shx);
printf("Enter the shearing factor for y:");
scanf("%f",­);
xe=xc+shx;
xf=xd+shx;
ye=yb+shy;
yf=yc+shy;
-
8/4/2019 Graphics Manual 12
31/72
REG NO: 40508104067
line(xa,ya,xb,ye);
line(xb,ye,xe,yf);
line(xe,yf,xf,yd);
line(xf,yd,xa,ya);
break;
case 4:
break;
}
}
while(ch
-
8/4/2019 Graphics Manual 12
32/72
REG NO: 40508104067
OUTPUT:
1.Reflection 2.Shearing 3.ExitEnter the choice: 1
Enter the xa&ya value: 200 100
Enter the xb&yb value: 200 200
Enter the xc&yc value: 100 200
1.About x-axis 2.About y-axis 3.About both 4.Exit
Enter the choice: 1
1.About x-axis 2.About y-axis 3.About both 4.Exit
Enter the choice: 2
1.About x-axis 2.About y-axis 3.About both 4.ExitEnter the choice: 3
-
8/4/2019 Graphics Manual 12
33/72
REG NO: 40508104067
1.About x-axis 2.About y-axis 3.About both 4.Exit
Enter the choice: 4
1.Reflection 2.Shearing 3.Exit
Enter the choice: 2
Enter the xa&ya value: 200 200
Enter the xb&yb value: 300 200
Enter the xc&yc value: 300 300
Enter the xd&yd value: 200 300
1.About x-axis 2.About y-axis 3.About both 4.Exit
Enter the choice: 1
Enter the shearing factor for x:50
1.About x-axis 2.About y-axis 3.About both 4.Exit
Enter the choice: 2
Enter the shearing factor for y:50
1.About x-axis 2.About y-axis 3.About both 4.Exit
Enter the choice: 3
Enter the shearing factor for x: 50
Enter the shearing factor for y: 50
-
8/4/2019 Graphics Manual 12
34/72
REG NO: 40508104067
Enter the choice: 4
Enter the choice: 3
RESULT:
Thus the program for performing 2D transformations reflection and
shearing is successfully executed in C.
-
8/4/2019 Graphics Manual 12
35/72
REG NO: 40508104067
COMPOSITE 2D TRANSFORMATIONS
AIM:
To write a program to perform the composite 2D transformations like
successive translation, rotation and scaling using transformation equation in
C.
ALGORITHM:
1. Start the program.2.
Obtain the coordinates of the line xa,ya,xb,yb.
3. Get the translation factors tx1, ty1, tx2, ty2 rotation angles a1, a2 & scalingfactors sx1, sy1, sx2, sy2.
4. Find the translated coordinates by applying the formula as below.x
1=x+(tx1+tx2) & y
1=y+(ty1+ty2).
It possesses associative property and successive translations are additive.
5. Get the rotation coordinates by applying the formula asX
1=abs(xa-xb)cos(a1+a2)-abs(ya-yb)sin(a1+a2)
Y1=abs(xa-xb)sin(a1+a2)+abs(ya-yb)cos(a1+a2)
It possesses associative property and successive rotations are additive.
6. Scaling is applied asX
1=x*(sx1*sx2)
Y1=y*(sy1*sy2)
It possesses associative property and successive scaling is multiplicative.
7. Draw the transformed line with the new coordinates (x1,y1).8. Stop the program.
-
8/4/2019 Graphics Manual 12
36/72
REG NO: 40508104067
PROGRAM:
#include#include
#include
#include
void getdata();
int x1, y1, x2, y2;
void main()
{
int gd=DETECT, gm;
int x3, x4, y3, y4, tx1, ty1, tx2, ty2, ch;float sx1, sy1, sx2, sy2, a1, a2, t1, t2;
initgraph(&gd,&gm,"");
clrscr();
setcolor(1);
do
{
printf("1.Successive Translation\t2.Successive Scaling\t3.Successive
Rotation\t4.Exit\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
getdata();
printf("Enter the translation factors in x-axis:");
scanf("%d%d",&tx1,&tx2);
printf("Enter the translation factors in y-axis:");
scanf("%d%d",&ty1,&ty2);
x3=x1+tx1+tx2;
x4=x2+tx1+tx2;y3=y1+ty1+ty2;
y4=y2+ty1+ty2;
line(x3,y3,x4,y4);
getch();
cleardevice();
break;
-
8/4/2019 Graphics Manual 12
37/72
REG NO: 40508104067
case 2:
getdata();
printf("Enter the scaling factors in x-axis:");
scanf("%f%f",&sx1,&sx2);
printf("Enter the scaling factors in y-axis:");
scanf("%f%f",&sy1,&sy2);
x3=x1*sx1*sx2;
x4=x2*sx1*sx2;
y3=y1*sy1*sy2;
y4=y2*sy1*sy2;
line(x3,y3,x4,y4);
getch();cleardevice();
break;
case 3:
getdata();
printf("Enter the rotation angle:");
scanf("%f%f",&a1,&a2);
t1=a1*(3.14159/180);
t2= a2*(3.14159/180);
x4=x1+abs(x2-x1)*cos(t1+t2)-abs(y2-y1)*sin(t1+t2);
y4=y1+abs(x2-x1)*sin(t1+t2)+abs(y2-y1)*cos(t1+t2);
line(x1,y1,x4,y4);
getch();
cleardevice();
break;
case 4:
exit(0);
break;
}
}
while(ch
-
8/4/2019 Graphics Manual 12
38/72
REG NO: 40508104067
printf("Enter the x1 value:");
scanf("%d",&x1);
printf("Enter the y1 value:");
scanf("%d",&y1);
printf("Enter the x2 value:");
scanf("%d",&x2);
printf("Enter the y2 value:");
scanf("%d",&y2);
line(x1,y1,x2,y2);
}
-
8/4/2019 Graphics Manual 12
39/72
REG NO: 40508104067
OUTPUT:
1.Successive Translation 2. Successive Scaling 3. Successive Rotation4.Exit
Enter the choice: 1
Enter the x1 value: 100
Enter the y1 value: 100
Enter the x2 value: 200
Enter the y2 value: 200
Enter the translation factor in x-axis: 25 25
Enter the translation factor in y-axis: 0 0
1.Successive Translation 2. Successive Scaling 3. Successive Rotation
4.Exit
Enter the choice: 2
Enter the x1 value: 100
Enter the y1 value: 100
Enter the x2 value: 200
Enter the y2 value: 200
Enter the scaling factor in x-axis: 0.5 0.5
Enter the scaling factor in y-axis: 1 1
-
8/4/2019 Graphics Manual 12
40/72
REG NO: 40508104067
1.Successive Translation 2. Successive Scaling 3. Successive Rotation
4.Exit
Enter the choice: 3Enter the x1 value: 100
Enter the y1 value: 100
Enter the x2 value: 200
Enter the y2 value: 200
Enter the rotation angle: 20 25
RESULT:
Thus the program for performing composite 2D transformations using
transformation equation is successfully executed in C.
-
8/4/2019 Graphics Manual 12
41/72
REG NO: 40508104067
LINE CLIPPING USING COHEN-SUTHERLAND ALGORITHM
AIM:
To write a program in C to clip a line using Cohen-Sutherland line
clipping algorithm.
ALGORITHM:
1.
Start the program.2. Obtain the coordinates of the clipping window xmin,ymin,xmax,ymax.3. Get the coordinates of the line to be clipped xa,ya,xb,yb.4. Region codes are assigned to the line end points according to relative position
with respect to the clipping rectangle.
5. The two region codes of the line end points which passes through the clippingrectangle are ANDed.
6. If the result is one (1 1 1 1),then the line is entirely outside the clippingwindow.
7. If the result is zero (0 0 0 0), then the line is completely inside the windowhence we can save it for display.
8. For the intermediate results we have to find intersection point using lineequation. The slope of the line is given by the equation m=(yb-ya)/(xb-xa).
9. The point at which the line intersects the clipping window can be obtainedusing the equation x
-
8/4/2019 Graphics Manual 12
42/72
REG NO: 40508104067
PROGRAM:
#include#include
#include
#include
void main()
{
int gd=DETECT,gm;
float xmin,ymin,xmax,ymax,x1,y1,x2,y2,x,y,slope,yl1,yr1,xa1,xb1;
float xa2, xr2;
float yl2,xb2,xl2,xe,ye,yr2;int l1,r1,b1,a1,l2,r2,b2,a2,l,r,b,a;
initgraph(&gd,&gm,"");
clrscr();
printf("Enter the coordinates for rectangle:");
scanf("%f\n%f\n%f\n%f",&xmin,&ymin,&xmax,&ymax);
printf("Enter the coordinates for line:");
scanf("%f\n%f\n%f\n%f",&x1,&y1,&x2,&y2);
setcolor(3);
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
if((x1-xmin)
-
8/4/2019 Graphics Manual 12
43/72
REG NO: 40508104067
if((x2-xmin)
-
8/4/2019 Graphics Manual 12
44/72
REG NO: 40508104067
else
xa1=x1+(ymin-y1)/slope;
getch(); cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(xa1,ymin,x2,y2);
xe=xa1; ye=ymin;
}
if(x1xmax)
{
yr1=y1+slope*(xmax-x1);
getch();
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(xmax,yr1,x2,y2);
xe=xmax;
ye=yr1;
}
if(y1>ymax)
{
xb1=x1+(ymax-y1)/slope;
getch();
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(xb1,ymax,x2,y2);
xe=xb1;
ye=ymax;
}
if(y2
-
8/4/2019 Graphics Manual 12
45/72
REG NO: 40508104067
else
xa2=x2+(ymin-y2)/slope;
getch();
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(xa2,ymin,xe,ye);
}
if(x2>xmax)
{
yr2=y2+slope*(xmax-x2);
getch();
cleardevice();rectangle(xmin,ymin,xmax,ymax);
line(xmax,yr2,xe,ye);
}
if(x2ymax)
{
if((x2-x1)==0)
xb2=x2;
else
xb2=x2+(ymax-y2)/slope;
getch();
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(xb2,ymax,xe,ye);
}
getch();
closegraph();
}
-
8/4/2019 Graphics Manual 12
46/72
REG NO: 40508104067
OUTPUT:Enter the coordinates for rectangle: 200 200 300 300
Enter the coordinates for line:150 200 350 450
*
*
*
RESULT:
Thus the program to clip a line using Cohen-Sutherland algorithm has
written and successfully executed in C.
-
8/4/2019 Graphics Manual 12
47/72
REG NO: 40508104067
IMPLEMENTATION OF SUTHERLAND HODGEMAN POLYGONCLIPPING ALGORITHM
AIM:
To write a C program to implement Sutherland Hodgeman polygon
clipping algorithm.
ALGORITHM:
1. Start the program.2. Declare the variables for defining clip window & polygon.3. While clipping a polygon following four possible cases to be considered:
If the first vertex is outside the window boundary and the secondvertex inside.
If the first vertex is inside the window boundary and the second vertexoutside
If both are outside If both are inside
4. Clip the polygon against the window boundary in the order left, right, bottom& above using intersection calculation.
5. Check the vertices that formed the polygon lies inside or on the boundary. Ifthat is inside or on the boundary save that point otherwise discard it.
6. Stop the program.
-
8/4/2019 Graphics Manual 12
48/72
REG NO: 40508104067
PROGRAM:
#include #include
#include
#include
#include
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);
enum {
TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
float xmin,xmax,ymin,ymax;
void clip(float x0,float y0,float x1,float y1)
{
outcode outcode0,outcode1,outcodeOut;
int accept = FALSE,done = FALSE;outcode0 = CompOutCode(x0,y0);
outcode1 = CompOutCode(x1,y1);
do
{
if(!(outcode0|outcode1))
{
accept = TRUE;
done = TRUE;
}else if(outcode0 & outcode1)
done = TRUE;
else
{
float x,y;
outcodeOut = outcode0?outcode0:outcode1;
-
8/4/2019 Graphics Manual 12
49/72
REG NO: 40508104067
if(outcodeOut & TOP)
{
x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
}
else if(outcodeOut & BOTTOM)
{
x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
}
else if(outcodeOut & RIGHT)
{y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else
{
y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
}
if(outcodeOut==outcode0)
{
x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);
}
}
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(150,20,"POLYGON AFTER CLIPPING");
rectangle(xmin,ymin,xmax,ymax);
}
-
8/4/2019 Graphics Manual 12
50/72
REG NO: 40508104067
outcode CompOutCode(float x,float y)
{
outcode code = 0;
if(y>ymax)
code|=TOP;
else if(yxmax)
code|=RIGHT;
else if(x
-
8/4/2019 Graphics Manual 12
51/72
REG NO: 40508104067
OUTPUT:Enter the no of sides of polygon:5
Enter the coordinates of polygon50
50
200
100
350
350
80
200
40
80
Enter the rectangular coordinates of clipping window150
150
300
300
POLYGON BEFORE CLIPPING
POLYGON AFTER CLIPPING
RESULT:Thus the C program to implement Sutherland Hodgeman polygon clipping was
executed and verified.
-
8/4/2019 Graphics Manual 12
52/72
REG NO: 40508104067
3D TRANSFORMATION(TRANSLATION,ROTATION AND SCALING)
AIM:
To write a C program to perform translation,rotation and scaling on
3D objects.
ALGORITHM:
1. Start the program.2. Store the coordinate values in a homogeneous matrix.3. Draw a 3D object with a specified coordinate value stored in a homogeneous
matrix.
4. Perform Translation with the use of translation matrix given below1 0 0 tx
0 1 0 ty
0 0 1 tz
0 0 0 1
where tx,ty,tz are the translation factors.
5. Perform rotation with the rotation matrix given by1 0 0 0
0 cost -sint 0
0 sint cost 0
0 0 0 0
6. Perform scaling with the scaling matrix given bysx 0 0 0
0 sy 0 0
0 0 sz 00 0 0 1
where sx,sy,sz are scaling factors.
7. Stop the program.
-
8/4/2019 Graphics Manual 12
53/72
REG NO: 40508104067
PROGRAM:
#include#include
#include
#include
void main()
{
int tx,ty,z[4][4],t1,i,j,p,k,l,shx;
float a,zr[4][4],yf,xf,zf,m,n,sx,sy,sz;
int pts[10],pts1[10],ch,x[10],y[10];
int gd=DETECT,gm;void scaling();
initgraph(&gd,&gm,"");
do
{
cleardevice();
printf("3D-TRANSFORMATION\n");
printf("1.Translation\t2.Rotation\t3.Scaling\t4.Exit\n");
printf("Enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
cleardevice();
pts[0]=50;pts[1]=50;pts[2]=250;pts[3]=50;pts[4]=250;
pts[5]=150;pts[6]=50;pts[7]=150;pts[8]=50;pts[9]=50;
printf("Enter the vector:");
scanf("%d %d",&tx,&ty);
for(i=0;i
-
8/4/2019 Graphics Manual 12
54/72
REG NO: 40508104067
for(k=0;k
-
8/4/2019 Graphics Manual 12
55/72
REG NO: 40508104067
for(l=0;l
-
8/4/2019 Graphics Manual 12
56/72
REG NO: 40508104067
case 3:
scaling();
break;
case 4:
exit(0);
}
}
while(ch!=5);
closegraph();
}
void scaling()
{int xx1,yy1,xx2,yy2,xx3,yy3,xx4,yy4,tx,ty, x1,y1,x2,y2,x3,y3,x4,y4;
x1=100;y1=100;x2=50;y2=150;x3=100;y3=125;x4=150;y4=150;
printf("Enter the scaling factor:"); scanf("%d %d",&tx,&ty);
cleardevice(); settextstyle(0,0,2);
outtextxy(200,50,"scaling");
line(x1,y1,x2,y2);
line(x1,y1,x4,y4);
line(x2,y2,x4,y4);
setlinestyle(1,1,1);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
line(x3,y3,x4,y4);
xx1=x1*tx; yy1=y1*ty;
xx2=x2*tx; yy2=y2*ty;
xx3=x3*tx; yy3=y3*ty;
xx4=x4*tx; yy4=y4*ty;
setlinestyle(0,1,1);
line(xx1,yy1,xx2,yy2);
line(xx1,yy1,xx4,yy4);
line(xx2,yy2,xx4,yy4);
setlinestyle(1,1,1);
line(xx2,yy2,xx3,yy3);
line(xx3,yy3,xx1,yy1);
line(xx3,yy3,xx4,yy4);
getch();
}
-
8/4/2019 Graphics Manual 12
57/72
REG NO: 40508104067
OUTPUT:
3D-TRANSFORMATIONS
1.Translation 2.Rotation 3.Scaling 4.ExitEnter the choice: 1
1.Translation 2.Rotation 3.Scaling 4.Exit
Enter the choice: 2
-
8/4/2019 Graphics Manual 12
58/72
REG NO: 40508104067
1.Translation 2.Rotation 3.Scaling 4.Exit
Enter the choice: 3
1.Translation 2.Rotation 3.Scaling 4.Exit
Enter the choice: 4
RESULT:
Thus the program to perform translation, rotation and scaling on 3D
objects were written and executed successfully in C.
-
8/4/2019 Graphics Manual 12
59/72
REG NO: 40508104067
COMPOSITE 3D- TRANSFORMATIONS
AIM:
To perform Composite 3D transformations such as successive translation &
rotation, fixed point scaling.
ALGORITHM:
1. Start the program.2. Use the function draw cube to draw a cube using eight points by means of
line functions.
3. Declare the variables x1,y1,x2,y2,x3,y3, in array type which of data type int.4. Declare the necessary variables for performing transformations.5. Initialize graphics functions.6. Input the first point in the cube.7. Input the size of the edge.8. Using switch operation selects the operation to perform successive translation,
successive rotation, and fixed point scaling.
9. Successive Translationa) input the translation vector tx1, ty1, tz1 & tx2, ty2, tz2.b) calculate points using formula
x3[i]=x1[i]+tx1+tx2.
y3[i]=y1[i]+ty1+ty2z3[i]=z1[i]+tz1+tz2.
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
c) using the function line, display the object before and after translation.
-
8/4/2019 Graphics Manual 12
60/72
REG NO: 40508104067
10.Successive Rotation:
a) input the rotation anglesb) Using formula theta=((theta1+theta2)*3.14)/180c) If the direction is along z axis,
z3[i]=z1[i].
x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta).
y3[i]=x1[i]*sin(theta)-y1[i]*cos(theta).
Apply cyclic permutation and compute for the rest of the directions.
d) Calculate the points using the formulax4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
e) Using the function line, display the object before and after rotation.12.Fixed Point Scaling:
a) Input the scaling factor and reference pointb) Calculate coordinates point using formula
x3[i]=xf+(x1[i]*sx+xf*(1-sx),
y3 [i] =yf+ (y1[i]*sy+yf*(1-sy)
z3 [i] =zf+ (z1[i]*sz+zf*(1-sz)
c) Calculate the points using the formulax4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
d) Using the function line, display the object before and after scaling.13.Stop the program.
-
8/4/2019 Graphics Manual 12
61/72
REG NO: 40508104067
PROGRAM:
#include#include
#include
#include
void drawcube(int x1[],int y1[]);
void main()
{
int i,x1[8],y1[8],x2[8],y2[8],z1[8],x3[8],y3[8],z3[8],x4[8],y4[8],t,t1,t2,op;
int ch,tx1,ty1,tz1,tx2,ty2,tz2,sx,sy,sz,xf,yf,zf,x,y,z,s;
int driver=DETECT;int mode;
initgraph(&driver,&mode,"");
printf("Enter the points on the cube:");
scanf("%d%d%d",&x,&y,&z);
printf("Enter the size of the edge:");
scanf("%d",&s);
x1[0]=x1[3]=x;
x1[1]=x1[2]=x+s;
x1[4]=x1[7]=x;
x1[5]=x1[6]=x+s;
y1[0]=y1[1]=y;
y1[2]=y1[3]=y+s;
y1[4]=y1[5]=y;
y1[6]=y1[7]=y+s;
z1[1]=z1[2]=z1[3]=z1[0]=z;
z1[4]=z1[5]=z1[6]=z1[7]=z-s;
for(i=0;i
-
8/4/2019 Graphics Manual 12
62/72
REG NO: 40508104067
do
{
printf("menu");
printf("\n1.Successive Transformation\n2.Successive Rotation \n
3.Fixed Point Scaling\n4.exit\n");
printf("Enter thr choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter thr translation vectors:");
scanf("%d%d%d%d%d%d",&tx1, &ty1, &tz1, &tx2,&ty2, &tz2);
for(i=0;i
-
8/4/2019 Graphics Manual 12
63/72
REG NO: 40508104067
t=((t1+t2)*3.14)/180;
printf("Enter the dirrection");
printf("\n1.Rotation about x-axis\n2.Rotation about y-
axis\n3.Rotation about z axis");
scanf("%d",&op);
if(op==1)
{
for(i=0;i
-
8/4/2019 Graphics Manual 12
64/72
REG NO: 40508104067
drawcube(x2,y2);
getch();
cleardevice();
printf("After Rotation");
drawcube(x4,y4);
getch();
cleardevice();
break;
case 3:
printf("Enter the scaling factor");
scanf("%d%d%d",&sx,&sy,&sz);
printf("Enter the reference point");scanf("%d%d%d",&xf,&yf,&zf);
for(i=0;i
-
8/4/2019 Graphics Manual 12
65/72
REG NO: 40508104067
}
while(op!=4);
getch();
}
void drawcube(int x1[],int y1[])
{
int i;
for(i=0;i
-
8/4/2019 Graphics Manual 12
66/72
REG NO: 40508104067
DRAWING 3D- OBJECTS
AIM:
To write a C program to compose a scene by using three dimensional
objects.
ALGORITHM:
1. Start the program.2. Initialize graphics functions.3. Use the function bar3d to depict three dimensional bars on the screen.4. Call sphere in the program by specifying necessary variables.5. Compose a scene in which the bars and the sphere move on the screen by using
the relevant control structure.
6. Stop the program.
-
8/4/2019 Graphics Manual 12
67/72
REG NO: 40508104067
PROGRAM:
#include#include
#include
void main()
{
int gd=DETECT,gm,i,j;
initgraph(&gd,&gm,"");
bar3d(200,200,325,250,31,1);
getch();for(i=1;i
-
8/4/2019 Graphics Manual 12
68/72
REG NO: 40508104067
bar3d(i+300,i-100,i+125,i-150,31,1);
setcolor(8);
setfillstyle(LINE_FILL,8);
bar3d(i-400,i+200,i-525,i+250,31,1);
setcolor(5);
setfillstyle(BKSLASH_FILL,5);
bar3d(i+100,i-200,i+250,i-250,31,1);
setcolor(3);
setfillstyle(SOLID_FILL,3);
fillellipse(i+100,100,50,50);
fillellipse(450-i,400,50,50);
setcolor(6);setfillstyle(SOLID_FILL,6);
fillellipse(100,i+100,50,50);
fillellipse(450,i-500,50,50);
setcolor(2);
setfillstyle(SOLID_FILL,2);
fillellipse(i+100,i+100,50,50);
fillellipse(450-i,i-400,50,50);
delay(25);
}
getch();
closegraph();
}
-
8/4/2019 Graphics Manual 12
69/72
REG NO: 40508104067
OUTPUT:
RESULT:
Thus a C program for composing a scene using three dimensional
objects was executed and verified.
-
8/4/2019 Graphics Manual 12
70/72
REG NO: 40508104067
GENERATING FRACTALS
AIM:
To write a C program to generate a fractal image.
ALGORITHM:
1. Start the program.2. Initialize graphics functions.3. In this program a sierpinski gasket is chosen as a fractal image and is drawn
using the function drawsierpinski().
4. Sierpinski triangle is created by infinite removals. Each triangle is divided intofour smaller triangles, each of them half the length (height, etc.) of the original.
5.
Each smaller triangle is reduced iteratively in linear dimension from the originalby a factor of n=2, and keep k=3 smaller triangles at each step. Hence the
Sierpinski Triangle has a dimension of 1.5850
6. Stop the program.
-
8/4/2019 Graphics Manual 12
71/72
REG NO: 40508104067
PROGRAM:
#include#include
#include
#include
#include
void DrawSierpinski(void);
void main()
{
int gd=VGA;
int gm=VGAHI;initgraph(&gd,&gm,"\\tc\\bgi");
DrawSierpinski();
getch();
}
void DrawSierpinski(void)
{
char Direct;
int iterate;
unsigned int x1,y1,x2,y2;
x1=x2=320;
y1=y2=0;
for(iterate=0;iterate
-
8/4/2019 Graphics Manual 12
72/72
REG NO: 40508104067
OUTPUT: