Вы находитесь на странице: 1из 95

211711104001

1


ExNo:01
IMPLEMWENTATION OF
DDA LINE DRAWING ALGORITHM

Date:

AIM:
To write a C program to draw a line using DDA Algorithm.

ALGORITHM:
Step 1: Start.
Step 2: Declare variables x,y,x1,y1,x2,y2,k,dx,dy,s,xi,yi and also declare
gdriver=DETECT,gmode.
Step 3: Initialise the graphic mode with the path location in TC folder.
Step 4: Input the two line end-points and store the left end-points in (x1,y1).
Step 5: Load (x1,y1) into the frame buffer;that is,plot the first point.put x=x1,y=y1.
Step 6: Calculate dx=x2-x1 and dy=y2-y1.
Step 7: If abs(dx) > abs(dy), do s=abs(dx).
Step 8: Otherwise s= abs(dy).
Step 9 : Then xi=dx/s and yi=dy/s.
Step 10: Start from k=0 and continuing till k<s,the points will be i. x=x+xi.
ii. y=y+yi.
Step 11: Place pixels using putpixel at points (x,y) in specified colour.
Step 12: Close Graph.
Step 13: Stop.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2,dx,dy,steps,k;
float xincrement,yincrement,x,y;
initgraph(&gd, &gm, " ");
211711104001
2

printf("Enter the Starting Point of x axis : ");
scanf("%d", &x1);
printf("Enter the Starting of y axis : ");
scanf("%d", &y1);
printf("Enter the End Point of x axis : ");
scanf("%d", &x2);
printf("Enter the End Point of y axis : ");
scanf("%d", &y2);
dx = x2 - x1;
dy = y2 - y1;
x=x1;
y=y1;
if(abs(dx) > abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincrement=dx/(float)steps;
yincrement=dy/(float)steps;
putpixel(ceil(x), ceil(y), WHITE);
for(k=1;k<=steps;k++)
{
x=x+xincrement;
y=y+yincrement;
putpixel(ceil(x),ceil(y),15);
}
getch();
closegraph();
}






211711104001
3



OUTPUT:
Enter the co-ordinates of first point: 50 50
Enter the co-ordinates of second point: 200 200

















211711104001
4

ExNo:02
IMPLEMENTATION OF BRESENHAMS
LINE DRAWING ALGORITHM Date:

AIM:
To write a C program to draw a line using Bresenhams Algorithm.

ALGORITHM:
Step 1: Start the program.
Step 2: Input the two endpoints (x1,y1) and (x2,y2).
Step 3: Plot the pixel value (x1,y1) with a specified color.
Step 4: Calculate the value of dx and dy and find the starting value of decision parameter
as dp=2*dy-dx.
Step 5: Calculate the values of s1 and s2 depending on (x1,y1) and (x2,y2) values.
Step 6: If dp<0, the next point to plot is (x,y+s2) and dp=+2*dy.
Step 7: Otherwise the next point to plot is (x+s1,y+s2) and dp=dp+2*dx-2*dy.
Step 8: Repeat steps 5 and 6 dx times.
Step 9: Stop the program.
PROGRAM:
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
int main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
printf("\n\n\tEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("\n\n\tEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);
211711104001
5

dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
putpixel(x,y,15);
while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,15);
}
getch();
closegraph();
}









211711104001
6

OUTPUT:
Enter the co-ordinates of first point: 50 50
Enter the co-ordinates of second point: 200 200




















211711104001
7

ExNo:03

IMPLEMENTATION OF MIDPOINT
CIRCLE DRAWING ALGORITHM
Date:

AIM:
To write a C program to draw a Circle using Bresenhams Algorithm.

ALGORITHM:
Step 1: Start the program.
Step 2: Input radius r and the midpoint of the circle (x,y) and obtain the first point on the
circumference for the circle as (0,r).
Step 3: Calculate the initial value of the decision parameter as p=1-r.
Step 4: At each position check the following conditions.
a) If p<0 then x=x+1 and p+=2*x+1
b) Else y=y-1 and p+=2*(x-y)+1.
Step 5: Determine symmetry points at the other seven octants.
Step 6: Move each calculated pixel position (x,y) onto the circular path centered on (xc,yc)
and plot the coordinate value as x=x+xc and y=y+yc.
Step 7: Repeat step 3 until x<y.
Step 8: Stop the program.

PROGRAM:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
int gd=DETECT,gm;
int i,r,x,y,xc,yc;
float d;
initgraph(&gd,&gm,"");
211711104001
8

printf("\t Enter Radius: ");
scanf("%d",&r);
printf("\n\t Enter Center of circle x axis:");
scanf("%d",&xc);
printf("\n\tEnter Center of circle y axis: ");
scanf("%d",&yc);
d=1.25-r;
x=0;
y=r;
while(x<y)
{
if(d<0)
{
x++;
d=d+2*(x+1);
}
else
{
x=x+1;
y=y-1;
d=d+2*x-2*y;
}
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);
putpixel(xc-x,yc-y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc+y,15);
}
getch();
closegraph();
}
211711104001
9

OUTPUT:
Enter radius: 100
Enter Center of circle x axis: 250
Enter Center of circle y axis: 250



















211711104001
10

ExNo:04
IMPLEMENTATION OF MIDPOINT
ELLIPSE DRAWING ALGORITHM Date:
AIM:
To write a C program to draw a Ellipse using Bresenhams Algorithm

ALGORITHM:
Step 1: Start the program.
Step 2: Input r
x
, r
y
and the center of the ellipse (x
c
,y
c
)and obtain the first point on the ellipse
centered on the origin as (x
0
,y
0
) = (0,r
y
).
Step 3: Calculate the initial value of the decision parameter as P1
0
= r
y
2
r
x
2
r
y
+ r
x
2

Step 4: At each position k x in region 1, starting at k=0,perform the following test. If p1
k
< 0
the next pt along the ellipse centered on (0,0) is (x
k+1
,y
k
) and p1
k+1
= p1
k
+ 2r
y
2
xk+1 + r
y
2

Step 5: Otherwise the next point along the ellipse is (x
k
+1,y
k
-1) and
p1
k+1
= p1
k
+2r
y
2
x
k+1
2r
x
2
y
k+1
+r
y
2
with
2r
y
2
x
k+1
= 2r
y
2
x
k
+2r
y
2
, 2r
x
2
y
k+1
= 2r
x
2
y
k
2r
x
2
and continue until 2r
y
2
x 2r
x
2
y.
Step 6: Calculate the initial position of the decision parameter in region 2 as P20 = ry2
2
+
r
y
2
( y
0 1
)
2
- r
x
2
r
y
2
where (x
0
,y
0
)is the last position
Step 7: At each y
k
position in region 2, starting at k=0,perform the following test , if p2
k
> 0
the next point along the ellipse centered on (0,0) is ( x
k
, y
k+1
) and
p2
k+1
= p2
k
2r
x
2
y
k+1
+ r
y
2
.
Step 8: Otherwise the next point along the ellipse is ( x
k
+ 1 ,y
k
-1) and P2
k+1
= p2
k
2r
y
2
x
k+1

2r
x
2
y
k+1
+ r
x
2
.
Step 9: Using the same incremental values for x and y as in region 1 continue until y=0.
Step 10: For both regions determine symmetry points along the other three quadrants.
Step 11: Move each calculated pixel position (x,y) on to the elliptical path centered on (x
c
,
y
c
) and plot the co-ordinates values x = x + x
c ,
y = y + y
c
.
Step 12: Display the output points.
PROGRAM:

#include <stdio.h>
#include <dos.h>
#include <graphics.h>
void ellipseMidpoint(float, float, float, float);
void drawEllipse(float, float, float, float);
211711104001
11

int main()
{
float xc, yc, rx, ry;
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
printf("\nEnter the center coordinates of ellipse: ");
scanf("%f %f", &xc, &yc);

printf("\nEnter x-radius coordinate: ");
scanf("%f", &rx);
printf("\nEnter y-radius coordinate: ");
scanf("%f", &ry);
ellipseMidpoint(xc, yc, rx, ry);
getch();
}
void ellipseMidpoint(float xc, float yc, float rx, float ry)
{
float rxSq = rx * rx;
float rySq = ry * ry;
float x = 0, y = ry, p;
float px = 0, py = 2 * rxSq * y;
drawEllipse(xc, yc, x, y);
//Region 1
p = rySq - (rxSq * ry) + (0.25 * rxSq);
while (px < py)
{
x++;
px = px + 2 * rySq;
if (p < 0)
p = p + rySq + px;
else
{
y--;
py = py - 2 * rxSq;
211711104001
12

p = p + rySq + px - py;
}
drawEllipse(xc, yc, x, y);
}
// Region 2
p = rySq*(x+0.5)*(x+0.5) + rxSq*(y-1)*(y-1) - rxSq*rySq;
while (y > 0)
{
y--;
py = py - 2 * rxSq;
if (p > 0)
p = p + rxSq - py;
else
{
x++;
px = px + 2 * rySq;
p = p + rxSq - py + px;
}
drawEllipse(xc, yc, x, y);
}
}
void drawEllipse(float xc, float yc, float x, float 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);
}






211711104001
13

OUTPUT:
Enter the center coordinates of ellipse: 250 250
Enter x-radius coordinate: 100
Enter y-radius coordinate: 50





















211711104001
14

Ex No: 05
IMPLEMENTATION OF LINE ATTRIBUTES
Date:

AIM:
To write a C program to implement line attributes.

ALGORITHM:

Step 1: Start the program .
Step 2: Initialize the variables.
Step 3: Call the initgraph() function
Step 4: Set color for the output primitives.
Step 5: Using switch case mention the various line types using set line function.
Step 6: close the graph and run the program.
Step 7: stop the program.

PROGRAMS:
Line Width:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void drawline(int,int,int,int);
int main()
{
int gd=DETECT,gm;
int w,x1,x2,y1,y2,y3,y4,c,mid;
initgraph(&gd,&gm,"");
printf("\n Enter the x-coordinate of point 1: ");
scanf("%d",&x1);
printf("\n Enter the y-coordinate of point 1: ");
scanf("%d",&y1);
printf("\n Enter the x-coordinate of point 2: ");
scanf("%d",&x2);
printf("\n Enter the x-coordinate of point 2: ");
scanf("%d",&y2);
printf("\n Enter the width of the line: ");
211711104001
15

scanf("%d",&w);
drawline(x1,y1,x2,y2);
c=1;
y3=y1;
y4=y2;
if(w%2==0)
{
drawline(x1,--y1,x2,--y2);
c++;
}
mid=w/2;
while(mid>0)
{
drawline(x1,--y1,x2,--y2);
c++;
drawline(x1,++y3,x2,++y4);
c++;
mid--;
}
getch();
closegraph();
}
void drawline(int x1,int y1,int x2,int y2)

{
int x,y,i;
float dx,dy,pixel;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
pixel=dx;
else
pixel=dy;
dx=dx/pixel;
211711104001
16

dy=dy/pixel;
x=x1;
y=y1;
i=1;
while(i<=pixel)
{
putpixel(x,y,15);
x=x+dx;
y=y+dy;
i=i+1;
putpixel(x,y,15);
}
}
OUTPUT:
Enter the x-coordinate of point 1: 100
Enter the y-coordinate of point 1: 100
Enter the x-coordinate of point 2: 250
Enter the x-coordinate of point 2: 250
Enter the width of the line: 10

211711104001
17


Line Styles:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void drawline(int,int,int,int,int);
int main()
{
int gd=DETECT,gm;
int w,x1,x2,y1,y2,y3,y4,ch,mid;
initgraph(&gd,&gm,"");
printf("\n Enter the x-coordinate of point 1: ");
scanf("%d",&x1);
printf("\n Enter the y-coordinate of point 1: ");
scanf("%d",&y1);
printf("\n Enter the x-coordinate of point 2: ");
scanf("%d",&x2);
printf("\n Enter the x-coordinate of point 2 ");
scanf("%d",&y2);
do
{
printf("\n MENU ");
printf("\n 1.SOLID");
printf("\n 2.DOTTED");
printf("\n 3.DASHED");
printf("\n 4.DOT/DASHED");
printf("\n Enter your choice ");
scanf("%d",&ch);
if(ch>4)
exit(1);
drawline(x1,y1,x2,y2,ch);
delay(2000);
cleardevice();
}while(ch<5);
211711104001
18

getch();
closegraph();
}
void drawline(int x1,int y1,int x2,int y2,int ch)
{
int ans,x,y,i,c=0;
float dx,dy,pixel;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
pixel=dx;
else
pixel=dy;
dx=dx/pixel;
dy=dy/pixel;
x=x1;
y=y1;
i=1;
switch(ch)
{
case 1:
printf("\n You have chosen SOLID");
while(x<=x2)
{
x=x+dx;
y=y+dy;
i=i+1;
putpixel(x,y,15);
}
break;
case 2:
printf("\n You have chosen DOTTED");
while(x<=x2)
{
211711104001
19

x=x+5*dx;
y=y+dy;
i=i+1;
putpixel(x,y,15);
}
break;
case 3:
printf("\n You have chosen DASHED");
while(x<=x2)
{

c++;
x=x+dx;
y=y+dy;
i=i+1;
if(c<=5)
putpixel(x,y,15);
if(c%10==0)
{
c=1;
}
}
break;
case 4:
printf("\n You have chosen DOTTED/DASHED");
while(x<=x2)
{

c++;
x=x+dx;
y=y+dy;
i=i+1;
if(c<=5)
putpixel(x,y,15);
211711104001
20

if(c%10==0)
{
putpixel(x,y,15);
}
if(c%15==0)
{
c=1;
putpixel(x,y,15);
}
}
break;
}
}
OUTPUT:
Enter the x-coordinate of point 1: 100
Enter the y-coordinate of point 1: 100
Enter the x-coordinate of point 2: 250
Enter the y-coordinate of point 2 250
MENU
1.SOLID 2.DOTTED 3.DASHED 4.DOT/DASHED
Enter your choice 1
You have chosen SOLID


211711104001
21

MENU
1.SOLID 2.DOTTED 3.DASHED 4.DOT/DASHED
Enter your choice 2
You have chosen DOTTED


MENU
1.SOLID 2.DOTTED 3.DASHED 4.DOT/DASHED
Enter your choice 3
You have chosen DASHED



211711104001
22


MENU
1.SOLID 2.DOTTED 3.DASHED 4.DOT/DASHED
Enter your choice 4
You have chosen DOTTED/DASHED















211711104001
23


Ex No: 06
IMPLEMENTATION OF CIRCLE ATTRIBUTES
Date:

AIM:
To write a C program to implement circle attributes.

ALGORITHM:

Step 1: Start the program.
Step 2: Initialize the variables.
Step 3: Call the initgraph() function
Step 4: Set color for the output primitives.
Step 5: Using switch case mention the various types using circle, set fill style and floodfill
function.
Step 6: close the graph and run the program.
Step 7: stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>

int main()
{
int gd=DETECT,gm,choice;
int i=0;
initgraph(&gd,&gm,"");
while(i<=12)
{
setfillstyle(i,9);
circle(100,100,50);
floodfill(100,100,WHITE);
delay(1000);
i++;
211711104001
24

}
closegraph();
}
OUTPUT:
1. Empty Fill:



2. Solid fill



3. Line fill

4. LT Slash fill

211711104001
25



5. Slash fill



6. BK Slash fill

7. LT BK Slash fill












8. Hatch fill
211711104001
26


9. X Hatch fill

10. Interleave fill



11. Wide dot fill




211711104001
27





12. Close dot fill




















211711104001
28








Ex No: 07
IMPLEMENTATION OF ELLIPSE ATTRIBUTES
Date:

AIM:
To write a C program to implement ellipse attributes.

ALGORITHM:

Step 1: Start the program.
Step 2: Initialize the variables.
Step 3: Call the initgraph() function
Step 4: Set color for the output primitives.
Step 5: Using switch case mention the various types using circle, set fill style and floodfill
function.
Step 6: Close the graph and run the program.
Step 7: Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int gd=DETECT,gm,choice;
int i=0;
initgraph(&gd,&gm,"");
int j=0;
while(i<=12)
211711104001
29

{
setfillstyle(i,j++);
ellipse(250,250,0,360,100,50);
fillellipse(250,250,100,50);
delay(1000);
i++;
}
closegraph();
}
OUTPUT:
1. Empty Fill:





2. Solid fill



3. Line fill
211711104001
30


4. LT Slash fill



5. Slash fill




6. BK Slash fill

7. LT BK Slash fill

211711104001
31






8. Hatch fill

9. X Hatch fill

10. Interleave fill



11. Wide dot fill
211711104001
32




12. Close dot fill













211711104001
33









ExNo:8a
IMPLEMENTATION OF 2D
TRANSFORMATIONS- TRANSLATION Date:

AIM:
To write a C program to perform 2D transformations such as translation.

ALGORITHM:

Step 1: Start the program.
Step 2: Draw the image with default parameters.
Step 3: Get the translation vectors.
Step 4: Get the translation co-ordinates and calculate
x1=x1+tx
y1=y1+ty
x2=x2+tx
y2=y2+ty
Step 5: Perform the translation.
Step 6: Draw the image.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int x[10],y[10];
int n;
void translation();
211711104001
34

void drawline(int,int,int,int);
int main()
{
int gd=DETECT,gm;
int i,ch;
initgraph(&gd,&gm," ");
printf("\n Enter the no of points : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the point %d ",i+1);
scanf("%d %d",&x[i],&y[i]);
}
for(i=0;i<n-1;i++)
drawline(x[i],y[i],x[i+1],y[i+1]);
drawline(x[i],y[i],x[0],y[0]);
delay(6000);
cleardevice();
translation();
getch();
closegraph();
}
void translation()
{
int tx,ty,i;
printf("\n Enter the transformation values for x and y ");
scanf("%d %d",&tx,&ty);
for(i=0;i<n;i++)
{
x[i]+=tx;
y[i]+=ty;
}
for(i=0;i<n-1;i++)
drawline(x[i],y[i],x[i+1],y[i+1]);
211711104001
35

drawline(x[i],y[i],x[0],y[0]);
}
void drawline(int xa,int ya,int xb,int yb)
{
int Dx=xb-xa,Dy=yb-ya,steps,k;
float xin,yin,X=xa,Y=ya;
if(abs(Dx)>abs(Dy))
steps=abs(Dx);
else
steps=abs(Dy);
xin=Dx/(float)steps;
yin=Dy/(float)steps;
putpixel(round(X),round(Y),15);
for(k=0;k<steps;k++)
{
X=X+xin;
Y=Y+yin;
putpixel(round(X),round(Y),15);
}
}














211711104001
36

Output:
Enter the no of points : 5
Enter the point 1 50 50
Enter the point 2 50 100
Enter the point 3 75 125
Enter the point 4 100 100
Enter the point 5 100 50
Enter the transformation values for x and y: 300 300
Before Translation:


After Translation:









211711104001
37

ExNo:8b
IMPLEMENTATION OF 2D
TRANSFORMATIONS- SCALING Date:

AIM:
To write a C program to perform 2D transformations such as scaling.
ALGORITHM:
Step 1: Start the program.
Step 2: Draw the image with default parameters.
Step 3: Get the choice from the user.
Step 4: Get the Scaling vectors and calculate
x1=x1*sx
y1=y1*sy
x2=x2*sx
y2=y2*sy
Step 5: Perform the scaling.
Step 6: Draw the image.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int x[10],y[10];
int n;
void translation();
void scaling();
void rotation();
void drawline(int,int,int,int);
int main()
{
int gd=DETECT,gm;
int i,ch;
initgraph(&gd,&gm," ");
printf("\n Enter the no of points : ");
scanf("%d",&n);
for(i=0;i<n;i++)
211711104001
38

{
printf("\n Enter the point %d ",i+1);
scanf("%d %d",&x[i],&y[i]);
}
printf("\n N: %d ",n);
for(i=0;i<n-1;i++)
drawline(x[i],y[i],x[i+1],y[i+1]);
drawline(x[i],y[i],x[0],y[0]);
delay(6000);
cleardevice();
scaling();
getch();
closegraph();
}
void scaling()
{
int sx,sy,i;
printf("\n Enter the scaling factors for x and y ");
scanf("%d %d",&sx,&sy);
for(i=0;i<n;i++)
{
x[i]*=sx;
y[i]*=sy;
}
for(i=0;i<n-1;i++)
drawline(x[i],y[i],x[i+1],y[i+1]);
drawline(x[i],y[i],x[0],y[0]);
}
void drawline(int xa,int ya,int xb,int yb)
{
int Dx=xb-xa,Dy=yb-ya,steps,k;
float xin,yin,X=xa,Y=ya;
if(abs(Dx)>abs(Dy))
steps=abs(Dx);
211711104001
39

else
steps=abs(Dy);
xin=Dx/(float)steps;
yin=Dy/(float)steps;
putpixel(round(X),round(Y),15);
for(k=0;k<steps;k++)
{
X=X+xin;
Y=Y+yin;
putpixel(round(X),round(Y),15);
}
}
OUTPUT:
Enter the no of points: 6
Enter the point 1: 50 50
Enter the point 2: 75 25
Enter the point 3: 100 50
Enter the point 4: 100 100
Enter the point 5: 75 125
Enter the point 6: 50 100
Enter the scaling factors for x and y: 2 2
Before Scaling: After Scaling:




211711104001
40

ExNo:8C
IMPLEMENTATION OF 2D
TRANSFORMATIONS- REFLECTION Date:

AIM:
To write a C program to perform 2D transformations such as reflection.
ALGORITHM:
Step 1: Start the program.
Step 2: Draw the image with default parameters.
Step 3: Get the choice from the user.
Step 4: Get the parameters for Reflection.
a. About X axis
i. x= x , y = -y
ii. Plot (x,y)
b. About Y axis
i. x= -x , y=y
ii. plot (x,y)
Step 6: Draw the image.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void drawcoord(float,float);
float xc,yc;
void drawline(int,int,int,int);
void quad1();
void quad2();
void quad3();
void quad4();
int x[10],y[10],n;
int main()
{
float x1,y1,x2,y2;
int gd=DETECT,gm,i,q;
initgraph(&gd,&gm,"....//bgi");
211711104001
41

xc=getmaxx()/2;
yc=getmaxy()/2;
printf("Mid x %f",xc);
printf("\nMid y %f ",yc);
drawcoord(xc,yc);
printf("Enter the no of sides: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the x axis of point %d: ",i+1);
scanf("%d",&x[i]);
printf("Enter the y axis point %d : ",i+1);
scanf("%d",&y[i]);
}
quad1();
delay(2000);
quad2();
delay(2000);
quad3();
delay(2000);
quad4();
getch();
closegraph();
}
void drawline(int x1,int y1,int x2,int y2)
{
int steps,k;
float dx,dy,x,y;
dx=x2-x1;
dy=y2-y1;
x=x1;
y=y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
211711104001
42

else
steps=abs(dy);
dx=dx/float(steps);
dy=dy/float(steps);
putpixel(round(x),round(y),15);
for(k=1;k<=steps;k++)
{
x=x+dx;
y=y+dy;
putpixel(ceil(x),ceil(y),15);
}
}
void quad2()
{
int i;
for(i=0;i<n-1;i++)
drawline(xc-x[i],yc-y[i],xc-x[i+1],yc-y[i+1]);
drawline(xc-x[i],yc-y[i],xc-x[0],yc-y[0]);
}
void quad1()
{
int i;
for(i=0;i<n-1;i++)
line(xc+x[i],yc-y[i],xc+x[i+1],yc-y[i+1]);
line(xc+x[i],yc-y[i],xc+x[0],yc-y[0]);
}
void quad3()
{
int i;
for(i=0;i<n-1;i++)
line(xc-x[i],yc+y[i],xc-x[i+1],yc+y[i+1]);
line(xc-x[i],yc+y[i],xc-x[0],yc+y[0]);
}
void quad4()
211711104001
43

{
int i;
for(i=0;i<n-1;i++) line(xc+x[i],yc+y[i],xc+x[i+1],yc+y[i+1]);
line(xc+x[i],yc+y[i],xc+x[0],yc+y[0]);
}
void drawcoord(float xc,float yc)
{
drawline(xc,0,xc,getmaxy()); drawline(0,yc,getmaxx(),yc);
}
Output:
Mid x 319.000000
Mid y 239.000000
Enter the no of sides: 3
Enter the x axis of point 1: 50
Enter the y axis point 1: 50
Enter the x axis of point 2: 50
Enter the y axis point 2: 100
Enter the x axis of point 3: 100
Enter the y axis point 3: 100
All Coordinate Reflection:


211711104001
44

ExNo:8d
IMPLEMENTATION OF 2D
TRANSFORMATIONS- SHEARING Date:

AIM:
To write a C program to perform 2D transformations such as shearing.

ALGORITHM:
Step 1: Start the program.
Step 2: Draw the image with default parameters.
Step 3: Get the choice from the user.
Step 4: Get the parameters for shearing.
a).input the shearing value and reference point.
b) X-shear
i. Get the shearing value shx
ii. x=x + shx * y, y=y
iii. Plot (x,y)
Y-shear
i. Get the shearing value shy
ii. x=x , y=y+ shy * x
iii. Plot (x,y)
c). using the function line, display the object before and after shearing.
Step 5: Draw the image.
Step 6: Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void drawcoord(float,float);
float xc,yc;
void shearingx();
void shearingy();
void drawline(int,int,int,int);
int x[10],y[10];
int n;
int main()
{
float x1,y1,x2,y2;
211711104001
45

int gd=DETECT,gm,i,ch,q;
initgraph(&gd,&gm,"");
xc=getmaxx()/2;
yc=getmaxy()/2;
printf("Mid x %f",xc);
printf("\nMid y %f ",yc);
drawcoord(xc,yc);
printf("Enter the no of sides: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the x axis of point %d: ",i+1);
scanf("%d",&x[i]);
printf("Enter the y axis point %d : ",i+1);
scanf("%d",&y[i]);
}
for(i=0;i<n-1;i++)
drawline(xc+x[i],yc-y[i],xc+x[i+1],yc-y[i+1]);
drawline(xc+x[i],yc-y[i],xc+x[0],yc-y[0]);
printf("\n 1. Relative to X-axis");
printf("\n 2. Relative to Y-axis");
printf("\n Enter your choice\n");
scanf("%d",&ch);
if(ch==1)
shearingx();
else
shearingy();
getch();
closegraph();
}
void shearingx()
{
int s;
int i,j;
211711104001
46

int gd=DETECT,gm;
initgraph(&gd,&gm,"");
drawcoord(xc,yc);
printf("\n Enter the Shearing Factor value\n");
scanf("%d",&s);
for(i=0;i<n;i++)
{
x[i]=x[i]+y[i]*s;
}
for(j=0;j<n-1;j++)
{ printf("\t %d",x[i]);
line(xc+x[j],yc-y[j],xc+x[j+1],yc-y[j+1]);
}
line(xc+x[j],yc-y[j],xc+x[0],yc-y[0]);
}
void shearingy()
{
int s;
int i,j;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
drawcoord(xc,yc);
printf("\n Enter the Shearing Factor value\n");
scanf("%d",&s);
for(i=0;i<n;i++)
{
y[i]=y[i]+x[i]*s;
}
for(j=0;j<n-1;j++)
{ printf("\t %d",x[i]);
line(xc+x[j],yc-y[j],xc+x[j+1],yc-y[j+1]);
}
line(xc+x[j],yc-y[j],xc+x[0],yc-y[0]);
}
211711104001
47

void drawline(int x1,int y1,int x2,int y2)
{
int steps,k;
float dx,dy,x,y;
dx=x2-x1;
dy=y2-y1;
x=x1;
y=y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
dx=dx/float(steps);
dy=dy/float(steps);
putpixel(round(x),round(y),15);
for(k=1;k<=steps;k++)
{
x=x+dx;
y=y+dy;
putpixel(ceil(x),ceil(y),15);
}
}
void drawcoord(float xc,float yc)
{
drawline(xc,0,xc,getmaxy());
drawline(0,yc,getmaxx(),yc);
}
Output:
Enter the no of sides: 4
Enter the x axis of point 1: 50
Enter the y axis point 1: 50
Enter the x axis of point 2: 50
Enter the y axis point 2: 90
Enter the x axis of point 3: 90
211711104001
48

Enter the y axis point 3: 90
Enter the x axis of point 4: 90
Enter the y axis point 4: 50
1. Relative to X-axis
2. Relative to Y-axis
Enter your choice 1
Enter the Shearing Factor value 2
Enter your choice 2
Enter the Shearing Factor value 2
Before Shearing:

Shearing relative to X-axis: Shearing Relative to Y-axis:




211711104001
49


ExNo:8e
IMPLEMENTATION OF 2D
TRANSFORMATIONS- ROTATION Date:

AIM:
To study and Implement 2D Transformation Rotation

ALGORITHM:

Step 1: Start the program.
Step 2: Draw the image with default parameters.
Step 3: Get the choice from the user.
Step 4: Get the parameters for rotation.
a). input the rotation angle
b). using formula theta=(theta*3.14)/180
c).input the value of reference point
d). calculate new coordinate point using formula
xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta),
ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta),
e). using the function line,display the object before and after rotation.
Step 6: Draw the image.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3);
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3);
int main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
initgraph(&gd,&gm," ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
211711104001
50

printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
TriAngle(x1,y1,x2,y2,x3,y3);
getch();
cleardevice();
Rotate(x1,y1,x2,y2,x3,y3);
setcolor(1);
TriAngle(x1,y1,x2,y2,x3,y3);
getch();
}
void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3)
{
int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2;
float Angle;
printf("Enter the angle for rotation:");
scanf("%f",&Angle);
cleardevice();
Angle=(Angle*3.14)/180;
a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle);
b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle);
a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle);
b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle);
a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle);
b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle);
printf("Rotate");
TriAngle(a1,b1,a2,b2,a3,b3);
}

211711104001
51



OUTPUT:





211711104001
52


ExNo:9
IMPLEMENTATION OF
COMPOSITE 2D TRANSFORMATION Date:

AIM:
To write a C++ program to perform composite 2D transformations such as translation,
rotation, scaling, reflection and shearing.
ALGORITHM:
Step 1: Start the program.
Step 2: Input the object coordinates.
Step 3: 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.
Step 4: 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.
Step 5: Scaling
a) Input the scaled factors sx and sy.
b) The transformed coordinates (x1,y1) , x1=x.sx and y1=y.sy.
Step 6: Reflection
a) Reflection about x axis : The transformed coordinates are x1=a and y1=-y.
Step 7: Reflection about y axis : The transformed coordinates are x1=x and y1=y.
Step 8: 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)
Step 9: Finally display the transformed object after the successive transformations.
211711104001
53

PROGRAM:
#include <Stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
struct point
{
int x;
int y;
};
typedef float matrix[3][3];
matrix tm;
void Identity( matrix m)
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
m[i][j]=(i==j);
}
void multiply(matrix a, matrix b)
{
int r,c;
matrix tmp;
for(r=0;r<3;r++)
for(c=0;c<3;c++)
tmp[r][c]=a[r][0]*b[0][c]+a[r][1]*b[1][c]+a[r][2]*b[2][c];
for(r=0;r<3;r++)
for(c=0;c<3;c++)
b[r][c]=tmp[r][c];
}

void translate(int tx,int ty)
{
matrix m;
211711104001
54

Identity(m);
m[0][2]=tx;
m[1][2]=ty;
multiply(m,tm);
}
void scale(float sx,float sy, struct point refpt)
{
matrix m;
Identity(m);
m[0][0]=sx;
m[0][2]=(1-sx)*refpt.x;
m[1][1]=sy;
m[1][2]=(1-sy)*refpt.y;
multiply(m,tm);
}
void rotate(float a , struct point refpt)
{
matrix m;
Identity(m);
a=(a*3.14)/180.0;
m[0][0]=cos(a);
m[0][1]=-sin(a);
m[0][2]=refpt.x*(1-cos(a))+refpt.y*sin(a);
m[1][0]=sin(a);
m[1][1]=-cos(a);
m[1][2]=refpt.y*(1-cos(a))+refpt.x*sin(a);
multiply(m,tm);
}
void transform(int npts, struct point *pts)
{
int k;
float tmp;
for(k=0;k<npts;k++)
{
211711104001
55

tmp=tm[0][0]*pts[k].x+tm[0][1]*pts[k].y+tm[0][2];
pts[k].y=tm[1][0]*pts[k].x+tm[1][1]*pts[k].y+tm[1][2];
pts[k].x=tmp;
}
}
void main()
{
struct point pts[4]={220.0, 50.0, 320.0, 200.0, 150.0, 200.0, 220.0, 50.0};
struct point refpt={50.0,50.0};
int g=DETECT,d;
initgraph(&g,&d,"c:\tc\bgi");
setbkcolor(GREEN);
setcolor(RED);
drawpoly(4,pts);
getch();
Identity(tm);
scale(0.5,0.5,refpt);
rotate(90.0,refpt);
translate(100,100);
transform(4,pts);
setcolor(WHITE);
drawpoly(4,pts);
getch();
closegraph();
}









211711104001
56


OUTPUT:













211711104001
57




ExNo:10
IMPLEMENTATION OF
COHEN SUTHERLAND LINE CLIPPING Date:

AIM:
To write a C program to clip a line using Cohen-Sutherland clipping algorithm.

ALGORITHM:
Step 1: Start the program.
Step 2: Enter the line end points and the window coordinates.
Step 3: Every line end point is assigned a code that identified the location of the point
relative to the boundaries of the clipping rectangle.
Step 4: Check whether the line lies inside the window then it is entirely drawn.
Step 5: Check whether the line lies outside the window then it is entirely clipped.
Step 6: Otherwise check whether the line intersects the window:
a) Calculate differences between end points and clip boundaries.
b) Determine the intersection point and how much of the line is to be discarded.
Step 7: Display the Output.
Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
struct coords
{
int x,y;
};
211711104001
58

int xmin=100,ymin=100,xmax=500,ymax=350;
int calreg(int x,int y)
{
int reg=10000;
if(x<xmin)
{
reg+=10;
}
else if(x>xmax)
{
reg+=1;
}
if(y<ymin)
{
reg+=1000;
}
else if(y>ymax)
{
reg+=100;
}
return reg;
}
int inout(int rc1,int rc2)
{
while(rc1!=1)
{
if((rc1%10)*(rc2%10))
{
return 1;
}
rc1=rc1/10;
rc2=rc2/10;
}
return 0;
211711104001
59

}
int check(struct coords c)
{
if((c.x<xmin)||(c.x>xmax))
return 0;
else if((c.y<ymin)||(c.y>ymax))
return 0;
return 1;
}
struct coords clipX(int x1,int y1, float m)
{
int x;
float y;
struct coords c;
if((x1<xmin)||(x1>xmax))
{
if(x1<xmin)
x=xmin;
else
x=xmax;
y=y1+m*(x-x1);
}
else
{
x=x1;
y=y1;
}
c.x=x;
c.y=(int)y;
return c;
}
struct coords clipY(int x1,int y1, float m)
{
int y;
211711104001
60

float x;
struct coords c;
if((y1<ymin)||(y1>ymax))
{
if(y1<ymin)
y=ymin;
else
y=ymax;
x=x1+(y-y1)/m;
}
else
{
x=x1;
y=y1;
}
c.x=(int)x;
c.y=y;
return c;
}
void lineSolid(int x1,int y1,int x2,int y2,int color)
{
int dx,dy,steps,k;
float xinc,yinc,x,y;
dx=x2-x1;
dy=y2-y1;
x=x1;
y=y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=dx/(float)steps;
yinc=dy/(float)steps;
putpixel((int)ceil(x),(int)ceil(y),color);
211711104001
61

for(k=1;k<=steps;k++)
{
x=x+xinc;
y=y+yinc;
putpixel((int)ceil(x),(int)ceil(y),color);
}
}

int main()
{
int gd=DETECT,gm;
int i,xs[10],ys[10],xe[10],ye[10],res[10],ree[10],n,x2,y2,x1,y1;
float m,garb;
struct coords c1,c2;
initgraph(&gd,&gm,".....");
lineSolid(xmin,ymin,xmin,ymax,12);
lineSolid(xmin,ymin,xmax,ymin,12);
lineSolid(xmin,ymax,xmax,ymax,12);
lineSolid(xmax,ymin,xmax,ymax,12);
printf("\nEnter Number of Lines: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Start Coordinates of Line %d: ",i+1);
scanf("%d%d",&xs[i],&ys[i]);
res[i]=calreg(xs[i],ys[i]);
printf("Region Code: %04d",res[i]%10000);
printf("\nEnter End Coordinates of Line %d: ",i+1);
scanf("%d%d",&xe[i],&ye[i]);
ree[i]=calreg(xe[i],ye[i]);
printf("Region Code: %04d",ree[i]%10000);
lineSolid(xs[i],ys[i],xe[i],ye[i],10);
if(inout(res[i],ree[i]))
printf("\nLine is completely outside");
211711104001
62

else
printf("\nLine is inside");
}
printf("\nEnter Any Key To Proceed to After Clipping........ ");
scanf("%f",&garb);
initgraph(&gd,&gm,".....");
lineSolid(xmin,ymin,xmin,ymax,12);
lineSolid(xmin,ymin,xmax,ymin,12);
lineSolid(xmin,ymax,xmax,ymax,12);
lineSolid(xmax,ymin,xmax,ymax,12);
for(i=0;i<n;i++)
{
m=((float)ye[i]-(float)ys[i])/((float)xe[i]-(float)xs[i]);
c1=clipX(xs[i],ys[i],m);
if(!check(c1))
c1=clipY(c1.x,c1.y,m);
c2=clipX(xe[i],ye[i],m);
if(!check(c2))
c2=clipY(c2.x,c2.y,m);
if(check(c1)&&check(c2))
{
printf("\nLine%d:(%d,%d)(%d,%d)ClippedTo:(%d,%d),(%d,%d)",
i+1,xs[i],ys[i],xe[i],ye[i],c1.x,c1.y,c2.x,c2.y);
lineSolid(c1.x,c1.y,c2.x,c2.y,10);
}
else
printf("\nLine %d: (%d,%d)(%d,%d) Out of Window Area & Can't be
Clipped",i+1,xs[i],ys[i],xe[i],ye[i]);
}
getch();
closegraph();
}
OUTPUT:
Enter Number of Lines: 4
211711104001
63

Enter Start Coordinates of Line 1: 150 150
Region Code: 0000
Enter End Coordinates of Line 1: 300 300
Region Code: 0000
Line is inside
Enter Start Coordinates of Line 2: 10 20
Region Code: 1010
Enter End Coordinates of Line 2: 30 40
Region Code: 1010
Line is completely outside
Enter Start Coordinates of Line 3: 50 60
Region Code: 1010
Enter End Coordinates of Line 3: 250 400
Region Code: 0100
Line is inside
Enter Start Coordinates of Line 4: 80 60
Region Code: 1010
Enter End Coordinates of Line 4: 110 20
Region Code: 1000
Line is completely outside
Enter Any Key To Proceed to After Clipping........
h
Line 1: (150,150)(300,300) Clipped To: (150,150),(300,300)
Line 2: (10,20)(30,40) Clipped To: (100,110),(100,110)
Line 3: (50,60)(250,400) Clipped To: (100,145),(220,350)
Line 4: (80,60)(110,20) Out of Window Area & Can't be Clipped
Before Clipping

211711104001
64



After Clipping:








211711104001
65


ExNo:11
IMPLEMENTATION OF
SUTHER HODGEMAN POLYGON CLIPPING Date:

AIM:
To write a C program to implement SUTHERLAND HODGEMANN polygon clipping
algorithm.

ALGORITHM:

Step 1: Start the program.
Step 2: Input Coordinates of all vertices of the polygon
Step 3: Input coordinates of the clipping window
Step 4: Consider the left edge of the window
Step 5: Compare the vertices of each edge of the polygon , ndividually with the clipping
plane
Step 6: Save the resulting intersections and vertices in the new list of vertices according to
four possible relationships between the edge and the clipping boundary discussed earlier
Step 7: Repeat the steps 4 and 5 for remaining edges of the clipping window. Each time the
resultant list of vertices is successively passed to process the next edge of the clipping
window
Step 8: Stop the Program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
struct coords
{
int x,y;
};
211711104001
66

int xmin=100,ymin=100,xmax=500,ymax=350;
int i=0,xs[100],ys[100],xe[100],ye[100];
int check(struct coords c)
{
if((c.x<xmin)||(c.x>xmax))
return 0;
else if((c.y<ymin)||(c.y>ymax))
return 0;
return 1;
}
struct coords clipX(int xd,int yd, float m)
{
int x;
float y;
struct coords c;
if((xd<xmin)||(xd>xmax))
{
if(xd<xmin)
x=xmin;
else
x=xmax;
y=yd+m*(x-xd);
}
else
{
x=xd;
y=yd;
}
c.x=x;
c.y=(int)y;
return c;
}
struct coords clipY(int xd,int yd, float m)
{
211711104001
67

int y;
float x;
struct coords c;
if((yd<ymin)||(yd>ymax))
{
if(yd<ymin)
y=ymin;
else
y=ymax;
x=xd+(y-yd)/m;
}
else
{
x=xd;
y=yd;
}
c.x=(int)x;
c.y=y;
return c;
}
void lineSolid(int x1,int y1,int x2,int y2,int color)
{
int dx,dy,steps,k;
float xinc,yinc,x,y;
dx=x2-x1;
dy=y2-y1;
x=x1;
y=y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=dx/(float)steps;
yinc=dy/(float)steps;
211711104001
68

putpixel((int)ceil(x),(int)ceil(y),color);
for(k=1;k<=steps;k++)
{
x=x+xinc;
y=y+yinc;
putpixel((int)ceil(x),(int)ceil(y),color);
}
}
void drawpoly(int npts)
{
int ctr;
for(ctr=0;ctr<npts;ctr++)
{
if(ctr==npts-1)
{
lineSolid(xs[i],ys[i],xs[i-ctr],ys[i-ctr],10);
xe[i]=xs[i-ctr];
ye[i]=ys[i-ctr];
i++;
}
else
{
lineSolid(xs[i],ys[i],xs[i+1],ys[i+1],10);
xe[i]=xs[i+1];
ye[i]=ys[i+1];
i++;
}
}
}
int main()
{
int gd=DETECT,gm,j,npts,ctr,x2,y2,x1,y1,n;
float m,garb;
struct coords c1,c2;
211711104001
69

initgraph(&gd,&gm,".....");
lineSolid(xmin,ymin,xmin,ymax,12);
lineSolid(xmin,ymin,xmax,ymin,12);
lineSolid(xmin,ymax,xmax,ymax,12);
lineSolid(xmax,ymin,xmax,ymax,12);
printf("\nEnter Number of Polygons: ");
scanf("%d",&n);
for(j=0;j<n;j++)
{
printf("\nEnter Number of points in Poly %d: ",j+1);
scanf("%d",&npts);
ctr=0;
while(ctr<npts)
{ printf("\nEnter End Coordinates of Poly %d Point %d: ",j+1,ctr+1);
scanf("%d%d",&xs[i],&ys[i]);
i++;
ctr++;
}
i=i-ctr;
drawpoly(npts);
}
n=i;
printf("\nEnter Any Key To Proceed to After Clipping........ ");
initgraph(&gd,&gm,".....");
lineSolid(xmin,ymin,xmin,ymax,12);
lineSolid(xmin,ymin,xmax,ymin,12);
lineSolid(xmin,ymax,xmax,ymax,12);
lineSolid(xmax,ymin,xmax,ymax,12);
for(i=0;i<n;i++)
{ m=((float)ye[i]-(float)ys[i])/((float)xe[i]-(float)xs[i]);
c1=clipX(xs[i],ys[i],m);
if(!check(c1))
c1=clipY(c1.x,c1.y,m);
c2=clipX(xe[i],ye[i],m);
211711104001
70

if(!check(c2))
c2=clipY(c2.x,c2.y,m);
if(check(c1)&&check(c2))
{
printf("\nLine%d:(%d,%d)(%d,%d)ClippedTo:(%d,%d)
,(%d,%d)",i+1,xs[i],ys[i],xe[i],ye[i],c1.x,c1.y,c2.x,c2.y);
lineSolid(c1.x,c1.y,c2.x,c2.y,10);
}
else
printf("\nLine %d: (%d,%d)(%d,%d) Out of Window Area & Can't be
Clipped",i+1,xs[i],ys[i],xe[i],ye[i]);
}
getch();
closegraph();
}
OUTPUT:
Enter Number of Polygons: 2
Enter Number of points in Poly 1: 3
Enter End Coordinates of Poly 1 Point 1: 50 50
Enter End Coordinates of Poly 1 Point 2: 50 90
Enter End Coordinates of Poly 1 Point 3: 90 90
Enter Number of points in Poly 2: 4
Enter End Coordinates of Poly 2 Point 1: 250 250
Enter End Coordinates of Poly 2 Point 2: 290 250
Enter End Coordinates of Poly 2 Point 3: 290 290
Enter End Coordinates of Poly 2 Point 4: 250 290
Enter Any Key To Proceed to After Clipping........
Line 1: (50,50)(50,90) Clipped To: (100,100),(100,100)
Line 2: (50,90)(90,90) Out of Window Area & Can't be Clipped
Line 3: (90,90)(50,50) Clipped To: (100,100),(100,100)
Line 4: (250,250)(290,250) Clipped To: (250,250),(290,250)
Line 5: (290,250)(290,290) Clipped To: (290,250),(290,290)
Line 6: (290,290)(250,290) Clipped To: (290,290),(250,290)
Line 7: (250,290)(250,250) Clipped To: (250,290),(250,250)
211711104001
71

Before Clipping:

After Clipping:






211711104001
72



ExNo:12
IMPLEMENTATION OF
3D TRANSFORMATION TECHNIQUES Date:

AIM:
To perform 3D transformations such as Translation, Rotation and Scaling on 3D object

ALGORITHM:

Step 1: Create a class cube with function draw cube.
Step 2: Use the function draw cube to draw a cube using eight points by means of
functions line.
Step 3: Translation
a).input the translation vectortx,ty,tz.
b).calculate points using formula
x3[i]=x1[i]+tx.
y3[i]=y1[i]+ty
z3[i]=z1[i]+tz.
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.
Step 4: Rotation:
a). input the rotation angle
b). using formula theta=(theta*3.14)/180
c).input the direction in x,y,z axis
d). if the direction is along x axis,
x3[i]=x1[i].
y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta),
y3[i]=y1[i]*sin(theta)-z1[i]*cos(theta),
if the direction is along yaxis,
y3[i]=y1[i].
z3[i]=z1[i]*cos(theta)-x1[i]*sin(theta),
x3[i]=z1[i]*sin(theta)-x1[i]*cos(theta),
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),
e).calculate the points using the formula
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
f). using the function line,display the object before and after rotation.
Step 5: Scaling:
a).input the scaling factor and reference point
b).calculate coordinates point using formula
x3[i]=xf+(x1[i]*sx+xf*(1-sx),
y3 [i] =yf+ (y1[i]*sy+yf*(1-sy)
211711104001
73

z3 [i] =zf+ (z1[i]*sz+zf*(1-sz)
c). calculate the points using the formula
x4[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.
Step 6: Stop.
PROGRAM:
Translation:
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main(void)
{
int gd=DETECT,gm,errorcode;
int l,t,r,b,tf,x,y,z,dp;
initgraph(&gd,&gm,"c:\\tc\\bgi");
clrscr();
setcolor(9);
setfillstyle(CLOSE_DOT_FILL,3);
settextstyle(GOTHIC_FONT,HORIZ_DIR,3);
outtextxy(200,10,"3D TRANSFORMATION");
bar3d(l=10,t=50,r=100,b=150,dp=15,tf=1);
getch();
cleardevice();
printf("\nEnter the x and y values : ");
scanf("%d%d",&x,&y);
setcolor(14);
outtextxy(150,150,"After Transformation");
setcolor(9);
bar3d(l=x+10,t=y+50,r+x+100,b+y+150,15,1);
getch();
setcolor(9);
setfillstyle(CLOSE_DOT_FILL,3);
bar3d(l=10,t=50,r=100,b=150,dp=15,tf=1);
211711104001
74

getch();
}

OUTPUT:


Rotation:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int mx,my,mix,miy;
void axis()
{
getch();
cleardevice();
line(mix,0,mix,my);
line(0,miy,mx,miy);
}
void main()
{
int gd=DETECT,gm,x,y,z,ang,x1,x2,y1,y2,o;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setfillstyle(0,getmaxcolor());
mx=getmaxx();
my=getmaxy();
mix=mx/2;
211711104001
75

miy=my/2;
axis();
bar3d(mix+50,miy-100,mix+60,miy-90,5,1);
printf("\nEnter the rotation angle : ");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
x2=60*sin(o*3.14/180)-90*cos(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
axis();
printf("\nAfter rotation about z-axis");
bar3d(mix+x1,miy-y1,mix+x2,miy-y2,5,1);
axis();
printf("\nAfter rotation about y-axis");
bar3d(mix+50,miy-x1,mix+60,miy-x2,5,1);
axis();
printf("\nAftet rotation about x-axis");
bar3d(mix+x1,miy-100,mix+x2,miy-90,5,1);
getch();
closegraph();
}
OUTPUT

Scaling
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
211711104001
76

void main(void)
{
int gd=DETECT,gm,errorcode, l,t,r,b,tf,x,y,z,dp;
initgraph(&gd,&gm,"c:\\tc\\bgi");
clrscr();
setcolor(9);
setfillstyle(CLOSE_DOT_FILL,3);
settextstyle(GOTHIC_FONT,HORIZ_DIR,3);
outtextxy(200,10,"3D TRANSFORMATION");
bar3d(l=10,t=50,r=100,b=150,dp=15,tf=1);
getch();
cleardevice();
printf("\n Enter the x ,y and z values : ");
scanf("%d%d%d",&x,&y,&z);
cleardevice();
setcolor(14);
outtextxy(200,10,"AFTER SCALING");
setcolor(9);
setfillstyle(CLOSE_DOT_FILL,3);
bar3d(l=10,t=50,r=x+100,b=y+150,dp=z+15,tf=1);
getch(); }





211711104001
77



ExNo:13
IMPLEMENTATION OF
3D COMPOSITE TRANSFORMATION Date:
AIM:
To write a C++ program to perform composite 3D transformations such as
translation, rotation, scaling, reflection and shearing.

ALGORITHM:

Step 1: Start the program.
Step 2: input the points for the cude and size of the Cube.
Step 3: Display the menu as 1.Translation 2.Scaling 3.Rotation 4.Exit
Step 4: Get the choice from the user.
Step 5: If the choice is 1 a point or an object is translated from position P to position P' with
the operation P'=T.P where t
x
,t
y
and t
z
specifying translation distances.
x'=x+ t
x ,
y'=y+ t
y,
z'=z+ t
z

Step 6: If the choice is 2 the scaling transformation of a position P can be written as P'=S.P
where scaling parameters s
x
,s
y
and s
z
are assigned any positive values.
x'=x.s
x ,
y'=y.s
y ,
z'=z.s
z

Step 7: If the choice is 3 get the rotation angle. Rotate the figure with respect to the axis of
rotation.
Step 8: About z axis rotation x'=xcos-ysin ,y'=xsin+ycos
z'=z Rotation can be expressed as P'=R
z
().P
Step 9: About x axis rotation y'=ycos-zsin z'=ysin+zcos x'=x
Rotation can be expressed as P'=R
x
().P
Step 10:About y axis rotation z'=zcos-xsin x'=zsin+xcos y'=y
Rotation can be expressed as P'=R
y
().P
Step 11:If choice is 4 exit the program.
Step 12:Stop the program.

PROGRAM:
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
class cube
{
public:
void drawcube(int x1[],int y1[])
{
211711104001
78

int i;
for(i=0;i<4;i++)
{
if(i<3)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
line(x1[0],y1[0],x1[3],y1[3]);
}
for(i=4;i<8;i++)
{
if(i<7)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
line(x1[4],y1[4],x1[7],y1[7]);
}
for(i=0;i<4;i++)
{
line(x1[i],y1[i],x1[i+4],y1[i+4]);
}
}
};
void main()
{
int
i,x1[8],y1[8],x2[8],y2[8],z1[8],x3[8],y3[8],z3[8],x4[8],y4[8],theta,op,ch,tx,ty,tz,sx,sy,sz,xf,yf,zf,x,y,z,si
ze;
int driver=DETECT;
int mode;
initgraph(&driver,&mode,"");
cout<<"enter the points on the cube:";
cin>>x>>y>>z;
cout<<"enter the size of the edge:";
cin>>size;
x1[0]=x1[3]=x;
x1[1]=x1[2]=x+size;
x1[4]=x1[7]=x;
211711104001
79

x1[5]=x1[6]=x+size;
y1[0]=y1[1]=y;
y1[2]=y1[3]=y+size;
y1[4]=y1[5]=y;
y1[6]=y1[7]=y+size;
z1[1]=z1[2]=z1[3]=z1[0]=z ;
z1[4]=z1[5]=z1[6]=z1[7]=z-size;
for(i=0;i<8;i++)
{
x2[i]=x1[i]+z1[i]/2;
y2[i]=y1[i]+z1[i]/2;
}
cube c;
getch();
cleardevice();
do
{
cout<<"menu"<<endl;
cout<<"\n1.translation\n2.rotation\n3.scaling\n4.exit\n";
cout<<"enter the choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the translation vector:";
cin>>tx>>ty>>tz;
for(i=0;i<8;i++)
{
x3[i]=x1[i]+tx;
y3[i]=y1[i]+ty;
z3[i]=z1[i]+tz;
}
for(i=0;i<8;i++)
{
211711104001
80

x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before translation";
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<"after translation";
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 2:
cout<<"enter the rotation angle:";
cin>>theta;
theta=(theta*3.14)/180;
cout<<"enter the direction"<<endl;
cout<<"1.rotation about x axis"<<endl<<"2.rotation about y axis"<<endl<<"3.rotation about z axis";
cin>>op;
if(op==1)
{
for(i=0;i<8;i++)
{
x3[i]=x1[i];
y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta);
z3[i]=y1[i]*sin(theta)+z1[i]*cos(theta);
}
}
else
if(op==2)
{
for(i=0;i<8;i++)
{
211711104001
81

y3[i]=y1[i];
x3[i]=z1[i]*cos(theta)-x1[i]*sin(theta);
x3[i]=z1[i]*sin(theta)+x1[i]*cos(theta);
}
}
else
if(op==3)
{
for(i=0;i<8;i++)
{
z3[i]=z1[i];
x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta);
y3[i]=x1[i]*sin(theta)+y1[i]*cos(theta);
}
}
else
cout<<"enter correct option";
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before rotation";
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<"after rotation";
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 3:
cout<<"enter the scaling factor:";
211711104001
82

cin>>sx>>sy>>sz;
cout<<"enter the reference point:";
cin>>xf>>yf>>zf;
for(i=0;i<8;i++)
{
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);
}
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before scaling";
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<"after scaling";
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 4:
exit(0);
break;
}
}
while(op!=4);
getch();
}


211711104001
83


OUTPUT:

AFTER TRANSLATION













211711104001
84

AFTER SCALING





















211711104001
85

OPENGL TO WORK WITH VISUAL C++
Installation
1. Install Visual C++ 2008 Express Edition (To support OpenGL).
2. Copy all the .h files into the C:\Program Files\Microsoft SDKs\Windows\v6.1\Include\GL
folder.
Header Files (.h files) : Gl.h, glu.h, glut.h, freeglut.h, freeglut_ext.h , freeglut_std.h
3. Copy all the .lib files into the C:\Program Files\Microsoft SDKs\Windows\v6.1\Lib folder.
Library files (.lib files) : opengl32.lib, glut32.lib, glu32.lib
4. Copy all the .dll files into the C:\Windows\system32 folder. Dynamic Link Library Files
(.dll) : freeglut.dll , glut32.dll
Working with Console Application Program in OpenGL

1. Creating a Project
1. Start Visual C++ and Under the File menu select New Project (Ctrl+Shift+N).
2. Select project types as Win32 and Win32 Console Application.
3. Give a User name for the Project.
4. Add a GLUT program to the project in the window.

2. Linking OpenGL Libraries
1. Under the Project menu select Project Properties (Alt+F7) at the bottom.
2. Select Configuration Properties Select C/C++ Preprocessor In preprocessor
definitions additionally include the path where gl/glut.h is present.
Example : C:\Program Files\Microsoft \ SDKs \Windows \v6.0A \Include
3. Select "Linker" folder and click "Input" .
4. Select All Configurations from the Configuration drop-down box at the top of the dialog.
This ensures you are changing the settings for both the Debug and Release configurations.
5. Select "Additional Dependencies" and type the following contents:
opengl32.lib glut32.lib glu32.lib

3. Compiling the Application
Choose "Build" from the menu options.
Select "Build filename".

4. Run the Application
Choose "Debug" from the menu options.
Select "Start without Debugging".

6. Save the Project
Select File Menu select Save all (Ctrl+Shift+S).
Save all the documents before quitting.

Working with Windows Application Program in OpenGL

1. Creating a Project
1. Start Visual C++ and Under the File menu select New Project (Ctrl+Shift+N).
2. Select Win32 Project, enter a Username, and click OK.
3. In the Wizard click Next, then in Additional options check the box of Empty Project, and
click Finish.
211711104001
86

4. Under the Project menu select Add New Item (Ctrl+Shift+A).
5. Select C++ File (.cpp), enter a Name, and click OK.
6. Add a GLUT program to the project in the window.

2. Link to the OpenGL libraries:
1. Under the Project menu select Project Properties (Alt+F7) at the bottom.
2. Select Configuration Properties Linker Input from the navigation panel on the left.
3. Select All Configurations from the Configuration drop-down box at the top of the dialog.
4. Type opengl32.lib glut32.lib glu32.lib in Additional Dependencies and click OK.

3. Compiling the Application
Choose "Build" from the menu options.
Select "Build filename".

4. Run the Application
Choose "Debug" from the menu options.
Select "Start Without Debugging".

5. Save the Project
Select File Menu select Save all (Ctrl+Shift+S).
Save all the documents before quitting.



























211711104001
87

Ex No:14
IMPLEMENTATION OF 3D OBJECTS AND SCENES
USING OPENGL

Date:


AIM:
To draw three dimensional Objects using a grey material characteristic.

ALGORITHM:

Step 1: Start the program.
Step 2: Include the glut header file and initialize the variables.
Step 3: Call the necessary glut functions as and when required.
Step 4: Set the required output primitives.
Step 5: Stop the program

PROGRAM:
#include "stdafx.h"
#include <gl/glut.h>
#include <stdlib.h>
void init (void)
{
GLfloat light_ambient[] = { 0.0, 0.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);
glPushMatrix ();
211711104001
88

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);
211711104001
89

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutCreateWindow (argv[0]);
init ();
glutReshapeFunc (reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

OUTPUT:















211711104001
90

Ex No:15
IMPLEMENTATION OF FRACTAL IMAGES
USING OPENGL

Date:

AIM:
To generate fractal images using OPENGL

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:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
PictureBox picFractal=new PictureBox();
public Form1()
{
picFractal.BackColor = Color.White;
211711104001
91

picFractal.Dock = DockStyle.Fill;
picFractal.Location = new Point(0, 0);
picFractal.Name = "picFractal";
picFractal.Size = new Size(241, 223);
picFractal.Click += new EventHandler(picFractal_Click);
Controls.Add(picFractal);
Name = "tsquare";
Text="Fractal T-Square";
Size = new Size(241, 223);
WindowState = FormWindowState.Maximized;
}
public void GenerateTSquare(Graphics g, int iIterations, double iLeft, double iTop, double iWidth,
double iHeight, Color oColor)
{
g.FillRectangle(new SolidBrush(oColor), (float)iLeft, (float)iTop, (float)iWidth, (float)iHeight);
if (iIterations > 1)
{
double dNewWidth = iWidth / 2.0;
double dNewHeight = iHeight / 2.0;
GenerateTSquare(g, iIterations - 1, iLeft - (dNewWidth / 2.0), iTop - (dNewHeight / 2.0), dNewWidth,
dNewHeight, oColor);
GenerateTSquare(g, iIterations - 1, iLeft + iWidth - (dNewWidth / 2.0), iTop - (dNewHeight / 2.0),
dNewWidth, dNewHeight, oColor);
GenerateTSquare(g, iIterations - 1, iLeft - (dNewWidth / 2.0), iTop + iHeight - (dNewHeight / 2.0),
dNewWidth, dNewHeight, oColor);
GenerateTSquare(g, iIterations - 1, iLeft + iWidth - (dNewWidth / 2.0), iTop + iHeight - (dNewHeight /
2.0), dNewWidth, dNewHeight, oColor);
}
}
public void DrawTSquare(int iIterations, Color oColor)
{
picFractal.Image = DrawTSquare(iIterations, oColor, picFractal.Width, picFractal.Height);
}
public Bitmap DrawTSquare(int iIterations, Color oColor, int iWidth, int iHeight)
211711104001
92

{
Bitmap oImage = new Bitmap(iWidth, iHeight);
Graphics g = Graphics.FromImage(oImage);
GenerateTSquare(g, iIterations, (double)((iWidth - 2.0) / 4.0) + 1, ((iHeight - 2.0) / 4.0) + 1,
(double)(iWidth - 2.0) / 2.0, (double)(iHeight - 2.0) / 2.0, oColor);
return oImage;
}
public Image GetImage()
{
return picFractal.Image;
}
private void picFractal_Click(object sender, EventArgs e)
{
DrawTSquare(5, Color.Black);
}
public static void Main()
{
Application.Run(new Form1());
}
}
OUTPUT:


s

211711104001
93

Ex No:16
IMPLEMENTATION OF SIMPLE ANIMATION USING C

Date:

AIM:
To wite a C program that implements a simple animation.
ALGORITHM:
Step 1: Start the program.
Step 2: Draw a car using circles and lines.
Step 3: Use for loop to move it by changing centre and coordinate value by any constant factor.
Step 4: Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void house(int,int);
void car(int,int);
void main()
{
int gd=DETECT,gm;
int a=50,b=200,i,p=350,q=350;
initgraph(&gd,&gm,"");
house(a,b);
car(p,q);
for(i=1;i<=75;i++)
{
cleardevice();
house(a,b);
p=p-5;
car(p,q);
delay(50);
}
211711104001
94

getch();
}
void house(int x,int y)
{
rectangle(x,y,x+50,y+50);
line(x,y,x+25,y-25);
line(x+25,y-25,x+50,y);
line(x+25,y-25,x+75,y-50);
line(x+50,y,x+100,y-25);
line(x+75,y-50,x+100,y-25);
line(x+100,y-25,x+100,y+25);
line(x+100,y+25,x+50,y+50);
}
void car(int x,int y)
{
rectangle(x,y,x+50,y+20);
line(x,y,x+15,y-15);
line(x+15,y-15,x+35,y-15);
line(x+35,y-15,x+50,y);
line(x+25,y,x+25,y-15);
circle(x+15,y+20,5);
circle(x+35,y+20,5);
}











211711104001
95



OUTPUT:

Вам также может понравиться