Beruflich Dokumente
Kultur Dokumente
EX.NO: 1A
DATE:
AIM:
To write a C program for Line Drawing Using Bresenham Algorithm.
ALGORITHM:
Step 1 : Start the program.
Step 2 : Declare the necessary variables.
Step 3 : Initialize the graph using dx, dy, gd, gm.
Step 4 : Assign the values of x1, y1 to x,y respectively.
Step 5 : Similarly, absolute values to dx, dy.
Step 6 : put pixel and set 15 to pixel position.
Step 7 : Using do-while loop, put e,x,y,I values.
Step 8 : Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
#include<graphics.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,e;
int i,gd=DETECT,gm;
clrscr();
printf("\n ENTER THE VALUE OF X1:");
scanf("%f",&x1);
printf("\n ENTER THE VALUES OF Y1:");
scanf("%f",&y1);
printf("\n ENTER THE VALUES OF X2:");
scanf("%f",&x2);
printf("\n ENTER THE VALUES OF Y2:");
scanf("%f",&y2);
initgraph(&gd,&gm," ");
dx=abs(x2-x1);
dy=abs(y2-y1);
x=x1;
y=y1;
e=2*(dy-dx); i=1;
do
{
putpixel(x,y,15);
while(e>=0)
{
y=y+1;
e=e-(2*dx);
}
x=x+1;
e=e+(2*dy);
i=i+1;
delay(100);
}
while(i<=dx);
closegraph();
getch();
}
OUTPUT:
ENTER THE VALUE OF X1: 600
ENTER THE VALUE OF Y1: 400
ENTER THE VALUE OF X2: 300
ENTER THE VALUE OF Y2: 400
AIM:
To write a C program for Circle Drawing Using Bresenham Algorithm.
ALGORITHM:
Step 1 : Start the program.
Step 2 : Declare the necessary variables.
Step 3 : Create the function for Circle.
Step 4 : Enter the radius and center values.
Step 5 : Initialize the graph with gd, gm and assign y<-radius.
Step 6 : Start the circle function and p<- 1- radius.
Step 7 : Check the while loop until the condition is satisfied.
Step 8 : Check the if else condition until the condition is satisfied.
Step 9 : Assign all operation for circle function and the values.
Step 10 : Stop the Program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void circlefun(int xcenter,int ycenter,int x,int y);
void main()
{
int x=0,radius,xcenter,ycenter;
int y,p,gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm," ");
printf("\n ENTER THE XCENTER & YCENTER:");
scanf("%d%d",&xcenter,&ycenter);
printf("\n ENTER THE RADIUS:");
scanf("%d",&radius);
y=radius;
circlefun(xcenter,ycenter,x,y);
p=1-radius;
while(x<y)
{
if(p<0)
x=x+1;
else
{
x=x+1;
y=y-1;
}
if(p<0)
p=p+2*x+1;
else
p=p+2*(x-y)+1;
circlefun(xcenter,ycenter,x,y);
}
getch();
}
void circlefun(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter+x,ycenter+y,1);
putpixel(xcenter-x,ycenter+y,1);
putpixel(xcenter+x,ycenter-y,1);
putpixel(xcenter-x,ycenter-y,1);
putpixel(xcenter+y,ycenter+x,1);
putpixel(xcenter-y,ycenter+x,1);
putpixel(xcenter+y,ycenter-x,1);
putpixel(xcenter-y,ycenter-x,1);
}
OUTPUT:
ENTER THE X CENTER AND Y CENTER : 500 250
ENTER THE RADIUS : 70
RESULT:
Thus the above program CIRCLE DRAWING USING BREASENHAM is executed
successfully.
AIM:
To write a C program for Ellipse Drawing Using Bresenham Algorithm
ALGORITHM:
Step 1: Start the program.
Step 2: Initialize the variables.
Step 3: Call the initgraph() function.
Step 4: Get the initialize points P1,P2.
Step 5: Get the values of Co-Ordinates of the ellipse (x,y).
Step 6: Enter the coordinates a,b of the ellipse .
Step 7: Display the output.
Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm,a,b;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("*** Ellipse Generating Algorithm ***\n");
printf("Enter the value of Xc\t");
scanf("%d",&xc);
printf("Enter the value of Yc\t");
scanf("%d",&yc);
printf("Enter X axis length\t");
scanf("%d",&a);
printf("Enter Y axis length\t");
scanf("%d",&b);
x=0;y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
delay(50);
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;
delay(50);
}
getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,7);
putpixel(xc-x,yc+y,7);
putpixel(xc+x,yc-y,7);
putpixel(xc+x,yc-y,7);
}
OUTPUT:
RESULT:
Thus using C++ program implementation of Bresenhams algorithm for
line, circle and ellipse drawing is done.
ATTRIBUTES OF OUTPUT PRIMITIVES
AIM:
To write a C program to display the attributes of output primitives.
FUNCTIONS USED:
LINE DRAWING
line(x1,y1,x2,y2);
Line function draws a line between two specified points (x1,y1) towards
(x2,y2). This function comes handy if you want to draw box like shapes or just
plotting the graphsetc.
e.g. line(100,50,100,400);
You can set the line style using setlinestyle functions. This function
specifies the type of line, pattern & the thickness that is going to appear on the
screen. You have options like solid, dotted, centered, dashed etc.
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
Blue
Green
Cyan
Red
Magenta
Brown
Lightgray
Darkgray
Lightblue
Lightgreen
10
Lightcyan
11
Lightred
12
Lightmagenta
13
Yellow
14
White
15
CIRCLE DRAWING
Circle()
The function circle() is used to draw a circle using (x,y) as center point.
Syntax:
circle(x,y,radius)
The other attributes with the command are as follows:
setfillstyle(style,lc),setcolor(lc).
ELLIPSE DRAWING
Ellipse(x,y,stangle,endangle.xrad,yrad)
This function draws an elliptical arc. Here (x,y) are the co-ordinates of
center of the ellipse. (stangle,endangle) are the starting and ending angles.
If stangle=0 and endangle=360 then this function draws complete ellipse.
Eg.ellipse(100,150,0,360,100,50);
The other attributes with the command are as follows:
setfillcolor(lc),setfillstyle(ls),setlinecolor(lc),setlinewidth(lw)
ALGORITHM
Step 1: Start the program.
Step 2: Draw the line. Set the attributes like style, type, width etc.
Step 3: Draw the circle. Set the attributes using commands.
Step 4: Draw the ellipse. Set the attributes like fillcolor, fillstyle, linecolor,
linewidth.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
void main()
{
char ch=y;
int gd=DETECT,gm,x1,y1,x2,y2,red,sa,ea,xrad,yrad,I;
initgraph(&gd,&gm,);
while(ch==y)
{
cleardevice();
setbkcolor(9);
outtextxy(100,130,Enter your choice:);
outtextxy(100,150,1.Line);
outtextxy(100,170,2.Circle);
outtextxy(100,190,3.Box);
outtextxy(100,210,4.Arc);
outtextxy(100,230,5.Ellipse);
outtextxy(100,250,6.Rectangle);
outtextxy(100,270,7.Exit);
ch=getch();
cleardevice();
switch(ch)
{
case 1:
line(100,200,300,400);
break;
case 2:
circle(200,200,100);
break;
case 3:
setfillstyle(5,4);
bar(100,300,200,100);
break;
case 4:
setfillstyle(5,4);
arc(200,200,100,300,100);
break;
case 5:
setfillstyle(5,4);
fillellipse(100,100,50,100);
break;
case 6:
settextstyle(DEFAULT_FONT,0,2);
outtextxy(120,140,BEC);
line(100,100,100,300);
line(300,300,100,300);
line(100,100,300,100);
line(300,100,300,300);
break;
case 7:
closegraph();
return;
}
ch=y;
getch();
}
}
OUTPUT:
Enter your choice:
1.Line
2.Circle
3.Box
4.Arc
5.Ellipse
6.Rectangle
7.Exit
BIE
RESULT:
Thus the program was executed successfully.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the necessary variables and initialize the graph, gd, gm.
Step 3: Use do-while loop and declare the function clear device.
Step 4: Create six cases translation, rotation, scaling , reflection,
shearing and exit.
Step 5: In case 1 enter the translation values and print the translation
object.
Step 6: In case 2 enter the rotation values and print rotation object.
Step 7: In case 3 enter the scaling values and print the scaling object.
Step 8: In case 4 enter the reflection values and print the reflection
object.
Step 9: In case 5 enter the shearing values and print the shearing
object.
Step 9: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int ch,x,y,az,i,w,ch1,ch2,xa,ya,ra,a[10],b[10],da,db;
float x1,y1,az1,w1,dx,dy,theta,x1s,y1s,sx,sy,a1[10],b1[10];
void main()
{
int gm ,gr;
clrscr();
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
printf("Enter the upper left corner of the rectangle:\n");
scanf("%d%d",&x,&y);
printf("Enter the lower right corner of the rectangle:\n");
scanf("%d%d",&az,&w);
rectangle(x,y,az,w);
da=az-x;
db=w-y;
a[0]=x;
b[0]=y;
a[1]=x+da;
b[1]=y;
a[2]=x+da;
b[2]=y+db;
a[3]=x;b[3]=y+db;
while(1)
{
printf("******2D Transformations*******\n");
printf("1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Shearing\n
6.Exit\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Translation*******\n\n");
printf("Enter the value of shift vector:\n");
scanf("%f%f",&dx,&dy);
x1=x+dx;
y1=y+dy;
az1=az+dx;
w1=w+dy;
rectangle(x1,y1,az1,w1);
break;
case 2:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Rotation*******\n\n");
printf("Enter the value of fixed point and angle of rotation:\n");
scanf("%d%d%d",&xa,&ya,&ra);
theta=(float)(ra*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((a[i]-xa)*cos(theta)-(b[i]-ya)*sin(theta)));
b1[i]=(ya+((a[i]-xa)*sin(theta)+(b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
case 3:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("********Scaling*******\n\n");
printf("Enter the value of scaling factor:\n");
scanf("%f%f",&sx,&sy);
x1=x*sx;
y1=y*sy;
az1=az*sx;
w1=w*sy;
rectangle(x1,y1,az1,w1);
break;
case 4:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Reflection*********\n");
printf("1.About x-axis\n2.About y-axis\n3.About both axis\nEnter your
choice:\n");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("Enter the fixed point\n");
scanf("%d%d",&xa,&ya);
theta=(float)(90*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((a[i]-xa)*cos(theta)-(-b[i]-ya)*sin(theta)));
b1[i]=(ya+((a[i]-xa)*sin(theta)+(-b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
case 2:
printf("Enter the fixed point\n");
scanf("%d%d",&xa,&ya);
theta=(float)(270*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((-a[i]-xa)*cos(theta)-(b[i]-ya)*sin(theta)));
b1[i]=(ya+((-a[i]-xa)*sin(theta)+(b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
case 3:
printf("Enter the fixed point\n");
scanf("%d%d",&xa,&ya);
theta=(float)(180*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((-a[i]-xa)*cos(theta)-(-b[i]-ya)*sin(theta)));
b1[i]=(ya+((-a[i]-xa)*sin(theta)+(-b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
}
break;
case 5:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Shearing******\n\n");
printf("1.x-direction shear\n2.y-direction shear\nEnter your choice:\n");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
printf("Enter the value of shear:\n");
scanf("%f",&x1s);
x1=x+(y*x1s);
y1=y;
az1=az+(w*x1s);
w1=w;
rectangle(x1,y1,az1,w1);
break;
case 2:
printf("Enter the value of shear:\n");
scanf("%f",&y1s);
x1=x;
y1=y+(x*y1s);
az1=az;
w1=w+(az*y1s);
rectangle(x1,y1,az1,w1);
break;
}
break;
case 6:
exit(0);
}
}
getch();
}
OUTPUT:
******2DTransformations*******
1.Translation
2.Rotation
3.Scaling
4.Reflection
5.Shearing
6.Exit
Enter your choice: 1
*******Translation*******
Enter the value of shift vector:
150
150
******2DTransformations*******
1.Translation
2.Rotation
3.Scaling
4.Reflection
5.Shearing
6.Exit
Enter your choice: 2
*******Rotation*******
Enter the value of fixed point and angle of rotation:
300
300
70
******2DTransformations*******
1.Translation
2.Rotation
3.Scaling
4.Reflection
5.Shearing
6.Exit
Enter your choice: 3
********Scaling*******
Enter the value of scaling factor:
2
2
******2DTransformations*******
1.Translation
2.Rotation
3.Scaling
4.Reflection
5.Shearing
6.Exit
Enter your choice: 4
*******Reflection*********
1.About x-axis
2.About y-axis
3.About both axis
Enter your choice:1
Enter the fixed point
150
150
******2DTransformations*******
1.Translation
2.Rotation
3.Scaling
4.Reflection
5.Shearing
6.Exit
Enter your choice: 5
*******Shearing*********
1.x-direction shear
2.y-direction shear
Enter your choice: 1
Enter the value of shear : 2
RESULT:
Thus the above programTWO DIMENSIONAL TRANSFORMATIONS Is
executed successfully.
Ex.No:3(d)
#include<graphics.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm,midx,midy,midw,n;
int xs1,xs2,xs3,ys1,ys2,ys3,ch,x1,y1,x2,y2,x3,y3,abs,c,b,w,z,sh;
initgraph(&gd,&gm,"c:/tc/bgi");
printf("\nEnter the I co-ordinate\n");
scanf("%d%d",&x1,&y1);
printf("\nEnter the II coordinate\n");
scanf("%d%d",&x2,&y2);
midx=abs((x2-x1)/2);
midy=abs((y2-y1)/2);
cleardevice();
do
{
printf("1.Shearing\n2.Reflection\n3.Quit\n");
scanf("%d",&n);
switch(n)
{
case 1:
cleardevice();
line(x1,y1,x2,y2);
moveto(x2,y2);
lineto(x1+midx,y1+midy);
lineto(x1,y1);
printf("Enter the shearing factor\n");
scanf("%d",&sh);
printf("Enter the choice 1 with respect to x,2 with respect to y\n");
scanf("%d",&ch);
printf("%d",ch);
if(ch==1)
{
xs1=x1+sh*y1;
xs2=x2+sh*y2;
line(x1,y1,xs2,y2);
moveto(xs2,y2);
lineto(xs1+abs((xs2-xs1)/2),y1+abs((y2-y1)/2));
lineto(x1,y1);
}
else
{
ys1=y1+sh*x1;
ys2=y2+sh*x2;
line(x1,y1,x2,ys2);
moveto(x2,ys2);
lineto(x1+abs((x2-x1)/2),ys1+abs((ys2-ys1)/2));
lineto(x1,y1);
}
getch();
break;
case 2:
cleardevice();
line(x1,y1,x2,y2);
moveto(x2,y2);
lineto(x1+midx,y1+midy);
lineto(x1,y1);
if(y1>y2)
z=y1;
else
z=y2;
if(x1<x2)
w=x2;
else
w=x1;
line(0,z+10,w+50,z+10);
line(x1,z+20+(z-y1),x2,z+20+(z-y2));
moveto(x2,z+20+(z-y2));
lineto(x1+midx,z+20+(z-(y1+midy)));
lineto(x1,z+20+(z-y1));
getch();
break;
case 3:
exit(0);
break;
}}
while(n<=3);
getch(); }
OUTPUT:
Enter the I co-ordinate
100 200
Enter the II co-ordinate
300 150
1.Shearing
2.Reflection
3.Quit
1
1.Shearing
2.Reflection
3.Quit
1
Enter the shearing factor
1
Enter the choice 1 with respect to x,2 with respect to y
2
1.Shearing
2.Reflection
3.Quit
2
1.Shearing
2.Reflection
3.Quit
3
AIM:
To write a C program to implement cohen-sutherland 2d line clipping.
ALGORITHM:
Step 1: Enter the line end points and the window coordinates.
Step 2: Every line end point is assigned a code that identified the
location of the point relative to the boundaries of the clipping
rectangle.
Step 3: Check whether the line lies inside the window then it is entirely
drawn.
Step 4: Check whether the line lies outside the window then it is
entirely clipped.
Step 5: Otherwise check whether the line intersects the window:
a) Calculate differences between endpoints and clip boundaries.
b) Determine the intersection point and how much of the line is to
be discarded
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#define MAX 20
if (y > ymax)
oc |= TOP;
else if (y < ymin)
oc |= BOTTOM;
if (x > xmax)
oc |= RIGHT;
else if (x < xmin)
oc |= LEFT;
return oc;
}
void cohen_sutherland (double x1, double y1, double x2, double y2,
double xmin, double ymin, double xmax, double ymax)
{
int accept;
int done;
outcode outcode1, outcode2;
accept = FALSE;
done = FALSE;
}
else
{
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
}
if (outcode_ex == outcode1)
{
x1 = x;
y1 = y;
outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax,
ymax);
}
else
{
x2 = x;
y2 = y;
outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax,
ymax);
}
}
} while (done == FALSE);
if (accept == TRUE)
line (x1, y1, x2, y2);
}
void main()
{
int n;
int i, j;
int ln[MAX][4];
int clip[4];
int gd = DETECT, gm;
printf ("Enter the x- and y-coordinates of the bottom-left and righttop corners of the clip window:\n");
for (i=0; i<4; i++)
scanf ("%d", &clip[i]);
OUTPUT:
Enter the number of lines to be clipped: 1
Result:
Thus the C program to implement the line clipping was written, executed and
the output was
verified successfully.
EX NO: 8
DATE:
WINDOWING TO VIEWPORT MAPPING
Aim :
To write a C program to clip a Window to Viewport Mapping.
Algorithm:
Step 1:Start the program.
Step 2:Get the maximum and minimum co-ordinates of the Window
Step 3:Get the maximum and minimum co-ordinates of the ViewPort
Step 4:Get the co-ordinates of a point by fitting window in viewport.
Step 5:Display the output.
Step 6:Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
main()
{
float sx,sy;
int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
printf("Enter The Coordinate x1,y1,x2,y2,x3,y3\n");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
cleardevice();
w1=5;
w2=5;
w3=635;
w4=465;
rectangle(w1,w2,w3,w4);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
v1=425;
v2=75;
v3=550;
v4=250;
sx=(float)(v3-v1)/(w3-w1);
sy=(float)(v4-v2)/(w4-w2);
rectangle(v1,v2,v3,v4);
x1=v1+floor(((float)(x1-w1)*sx)+.5);
x2=v1+floor(((float)(x2-w1)*sx)+.5);
x3=v1+floor(((float)(x3-w1)*sx)+.5);
y1=v2+floor(((float)(y1-w2)*sy)+.5);
y2=v2+floor(((float)(y2-w2)*sy)+.5);
y3=v2+floor(((float)(y3-w2)*sy)+.5);
line(x1,y1,x2,y2);line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
return 0;
}
OUTPUT:
Enter The Coordinate x1,y1,x2,y2,x3,y3
100
200
300
400
500
350
RESULT:
Thus the above program has been executed and output is verified.
Ex. No : 6
Date
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1>=xmin && x2<xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
}
}
}
if(y1<=ymax && y2>ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
}
}
void clipr(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
if(x1<=xmax && x2<=xmax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1>xmax && x2<=xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1<=xmax && x2>xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
}
void main()
{
int gdriver=DETECT,gmode,n,poly[20],i;
float xi,yi,xf,yf,polyy[20];
clrscr();
printf("Coordinates of rectangular clip window :\nxmin,ymin:");
scanf("%f%f",&xmin,&ymin);
printf("Coordinates of rectangular clip window :\nxmax,ymax:");
scanf("%f%f",&xmax,&ymax);
printf("\n\nPolygon to be clipped :\nNumber of sides
scanf("%d",&n);
printf("Enter the coordinates :");
for (i=0;i<2*n;i++)
scanf("%f",&polyy[i]);
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
for(i=0;i<2*n+2;i++)
poly[i]=round(polyy[i]);
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
rectangle(xmin,ymax,xmax,ymin);
printf("\tUNCLIPPED POLYGON");
fillpoly(n,poly);
getch();
cleardevice();
k=0;
for(i=0;i<2*n;i+=2)
clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
:");
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
for(i=0;i<k;i++)
poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
rectangle(xmin,ymax,xmax,ymin);
printf("\tCLIPPED POLYGON");
getch();
closegraph();
}
OUTPUT:
Coordinates of rectangular clip window:
xmin,ymin: 150
150
300
Polygon to be clipped :
Number of sides : 5
Enter the coordinates :
CLIPPED POLYGON
Result:
Thus the C program to implement the polygon clipping was written, executed
and the output was verified successfully.
Ex.No:7
3D transformations - Translation, Rotation, Scaling
Aim:
To write a C program to implement three dimensional transformations such as
Translation,
Rotation and Scaling.
Algorithm:
1. Start the program.
2. Draw the 3D object and also display the choices.
3. Get the choice and perform the corresponding actions.
4. The choices is 1 then get the translation values and move the 3D-object.
5. The choices is 2 then get the scaling vectors and change the size of 3Dobject.
6. The choices is 3 then get the rotation angle to rotate the 3D-object.
7. The choices is 4 then exit from the output window.
8. Terminate the program.
PROGRAM:
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<process.h>
int points[50];
int points1[50];
int x=25;
int n=5;
void construct(int[]);
void construct1(int[],int[]);
void main()
{
int gd=DETECT;
int gm=DETECT;
char z;
initgraph(&gm,&gd,"D:/tc/bgi");
do
{
int points[50]={100,100,100,150,150,150,150,100,100,100};
int a;
cleardevice();
printf("\n\n\n\t\t\t\t1.Translation");
printf("\n\n\n\t\t\t\t2.Scaling");
printf("\n\n\n\t\t\t\t3.Rotation");
printf("\n\n\n\t\t\t\t4.Exit");
printf("\n\n\n\tEnter the choice:");
scanf("%d",&a);
switch(a)
{
case 1:
{
int tx,ty,i,point[50],point1[50];
construct(points);
printf("Enter the Translation Values:\n");
scanf("%d%d",&tx,&ty);
for(i=0;i<=2*n;i=i+2)
{
point[i]=points[i]+tx;
point1[i]=points1[i]+tx;
}
for(i=1;i<2*n;i=i+2)
{
point[i]=points[i]+ty;
point1[i]=points1[i]+ty;
}
construct1(point,point1);
getch();
break;
}
case 2:
{
int sx,sy,sz,point[50],point1[50],i;
construct(points);
case 4:
{
exit(0);
break;
}
}
printf("Do you want to continue say(Y/N)?:");
scanf("%s",&z);
}while(z=='Y');
getch();
closegraph();
}
void construct(int points[50])
{
int x=25,n=5,i;
cleardevice();
for(i=0;i<2*n;i++)
{
points1[i]=points[i]+x;
}
construct1(points,points1);
}
void construct1(int x[50],int y[50])
{
drawpoly(n,x);
drawpoly(n,y);
line(x[0],x[1],y[0],y[1]);
line(x[2],x[3],y[2],y[3]);
line(x[4],x[5],y[4],y[5]);
line(x[6],x[7],y[6],y[7]);
}
OUTPUT:
axis();
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
printf("Enter the rotation angle");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*sin(o*3.14/180)+100*cos(o*3.14/180);
x2=60*cos(o*3.14/180)-90*sin(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
axis();
printf("After rotating about Z-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,10,1);
axis();
printf("After rotating about x-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+100,midy-x1,midx+60,midy-x2,10,1);
axis();
printf("After rotating about Y-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+x1,midy-150,midx+x2,midy-100,10,1);
getch();
closegraph();
}
OUTPUT:
Result:
Thus the C program to implement the Composite 3D-Transformations was
written, executed and the output was verified successfully.
EX NO : 10 GENERATING FRACTAL IMAGESDATE :
Aim:
To generate fractal images.
Algorithm:
Step1: The Sierpinski Triangle is created by infinite removals
Step2: Each triangle is divided into 4 smaller upside down triangles
Step3: The center of the 4 triangles is removed
Step4: 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
Step5: After closer examination magnification factor is 2.
Step6: With each magnification there are 3 divisions of a triangle
Dimension D=ln(3)/ln(2)
D=1.5850
PROGRAM
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <graphics.h>
void DrawSierpinski(void);
void main(void)
{
int gd=VGA;
int gm=VGAHI;
initgraph(&gd, &gm, "\\tc\\bgi");
DrawSierpinski();
getch();
}
void DrawSierpinski(void)
{
char Direct;
int iterate;
unsigned int x1, y1, x2, y2;
x1 = x2 = 320;
y1 = y2 = 0;
for(iterate = 0; iterate < 10000; iterate++)
{
Direct = random(3);
if(Direct == 0)
{
x1 = (x2 + 320) / 2;
y1 = (y2 + 0) / 2;
}
else if(Direct == 1)
{
x1 = (x2 + 0) / 2;
y1 = (y2 + 480) / 2;
}else if(Direct == 2)
{
x1 = (x2 + 640) / 2;
y1 = (y2 + 480) / 2;
}
putpixel(x1, y1, WHITE);
x2 = x1;
y2 = y1;
}
}
OUTPUT :
RESULT:
Thus generation of Sierpinski triangle using the above program has
been executed and outputis verified.