Sei sulla pagina 1di 95

Exno:1

Fundamental Shapes using Built in functions

__________________________________________________________________________

Aim:
To write a c graphics program and to display fundamental shapes using built in functions.

Algorithm:
1. Detect graphics device and graphic mode, and initialize graphic system using Initgraph().
2. Declare the required variables and display the options in switch case statement.
3. The options 1.circle 2.rectangle 3.ellipse 4.line 5.Sector 6.polygon 7.Filled
ellipse 8.arc 9.square 10.exit
4.If the choice is 1, drawing circle using circle(int x,int y,int radius)function where x,y are
coordinate values.
5. If the choice is 2, drawing rectangle using rectangle(int left,int top,int right,int bottom)function.
6. If the choice is 3, drawing ellipse using ellipse(int x, int y, int stangle, int end angle, int x radius,
int y radius)function where x,y are coordinate values.
7. If the choice is 4, drawing filled ellipse using fillellipse(int x, int y, int x radius, int y
radius)function where x,y are coordinate values.
8. If the choice is 5, drawing line using line(int x1, int y1, int x2, int y2)function where x1,y1,x2,y2
are line point coordinate values.
9. If the choice is 6, drawing sector using sector(int x, int y, int stangle, int end angle, int x radius, int
y radius)function where x,y are coordinate values.
10. If the choice is 7, drawing polygon using draypoly(int number of points, int polypoints)function.
11. If the choice is 8, drawing arc using arc(int x, int y, int stangle, int end angle, int radius)function.
12.Display the images and closegraph() which is used to close current opened graphic device.

PROGRAM:

#include<graphics.h>
#include<conio.h>
void main()
{
int ch, gd=DETECT, gm;
int poly[12]={350,450, 350,410, 430,400, 350,350, 300,430, 350,450 };
initgraph(&gd, &gm, "");
printf("\n1.circle\n2.rectangle\n3.ellipse\n4.line\n5.Sector\n6.polygon\n7.Filled
ellipse\n8.arc\n9.square\n10.exit");
do
{
printf("\nEnter ur choice::");
scanf("%d",&ch);
switch(ch)
{
case 1:
circle(200,50,20);
outtextxy(175,100, "Circle");
break;
case 2:
rectangle(250,50,300,100);
outtextxy(240,170, "Rectangle");
break;
case 3:
ellipse(400,100,0,360, 50,25);
outtextxy(480, 170, "Ellipse");
break;
case 4:
line(200,250,300,250);
outtextxy(300,260,"Line");
break;
case 5:
sector(150, 400, 30, 300, 100,50);
outtextxy(120, 460, "Sector");
break;
case 6:
drawpoly(6, poly);
outtextxy(340, 400, "Polygon");
break;
case 7:
outtextxy(340, 460, "Filled elipse");
fillellipse(400,190,50,20);
break;
case 8:
outtextxy(360, 480, "arc");
arc(375,375,200,-20,70);
break;

case 9:
outtextxy(270,370, "Square");
rectangle(450,450,400,400);
break;
case 10:
exit(0);
break;
}
}while(1);
getch();
closegraph();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:2

Displaying doll image

__________________________________________________________________________

Aim:
To write a c graphics program and to display doll image using built in functions.

Algorithm:

1. Detect graphics device and graphic mode, and initialize graphic system using Initgraph().
2. Drawing the doll using some built in functions, such as,
3.Drawing circle using circle(int x,int y,int radius)function where x,y are coordinate values.
4. Drawing ellipse using ellipse(int x, int y, int stangle, int end angle, int x radius, int y
radius)function where x,y are coordinate values.
5. Drawing filled ellipse using fillellipse(int x, int y, int x radius, int y radius)function where x,y are
coordinate values.
6. Drawing arc using arc(int x, int y, int stangle, int end angle, int radius)function.
7.Display the images and closegraph() which is used to close current opened graphic device.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"");
setcolor(YELLOW);
circle(200,200,100);
ellipse(150,180,0,360,20,30);
ellipse(150,190,0,360,10,20);
fillellipse(150,190,10,20);
ellipse(240,180,0,360,20,30);
ellipse(240,190,0,360,10,20);
fillellipse(240,190,10,20);
arc(200,200,200,-20,70);
setcolor(RED);
ellipse(230,250,200,-10,10,60);
getch();
closegraph();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:3

Display English characters using Built in functions

__________________________________________________________________________

Aim:
To write a c graphics program and to display English characters using built in functions.

Algorithm:

1. Detect graphics device and graphic mode, and initialize graphic system using Initgraph().
2. Display English characters using built in functions such as,
3.Drawing circle using circle(int x,int y,int radius)function where x,y are coordinate values.
4. Drawing line using line(int x1, int y1, int x2, int y2)function where x1,y1,x2,y2 are line point
coordinate values.
5.Display the images and closegraph() which is used to close current opened graphic device.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"");
//p
setcolor(5);
line(40,150,40,200);
line(40,150,60,150);
line(40,175,60,175);
line(60,150,60,175);
setcolor(WHITE);
setfillstyle(1,WHITE);
fillellipse(65,195,3,3);
//j
setcolor(11);
line(90,150,110,150);
line(100,150,100,200);
line(100,200,80,190);
//e
setcolor(2);
line(120,150,120,200);
line(120,150,140,150);
line(120,200,140,200);
line(120,175,130,175);
//g
setcolor(3);
line(150,150,150,200);
line(150,150,170,150);
line(150,200,170,200);
line(170,200,170,180);
//a
setcolor(4);
line(200,150,180,200);
line(200,150,220,200);
line(190,175,210,175);
//t
setcolor(5);
line(240,150,240,200);
line(225,150,255,150);
//h
setcolor(6);
line(270,150,270,200);
line(290,150,290,200);
line(270,175,290,175);
//e

setcolor(7);
line(310,150,310,200);
line(310,150,330,150);
line(310,200,330,200);
line(310,175,320,175);
//e
setcolor(8);
line(340,150,340,200);
line(340,150,360,150);
line(340,200,360,200);
line(340,175,350,175);
//s
setcolor(9);
line(380,150,400,150);
line(380,150,380,175);
line(380,175,400,175);
line(380,200,400,200);
line(400,175,400,200);
//w
setcolor(10);
line(420,150,420,200);
line(450,150,450,200);
line(435,175,420,200);
line(435,175,450,200);
//a
setcolor(11);
line(470,150,460,200);
line(470,150,490,200);
line(465,175,480,175);
//r
setcolor(12);
line(510,150,510,200);
line(510,150,530,150);
line(510,175,530,200);
line(510,175,530,175);
line(530,150,530,175);
//i
setcolor(1);
line(550,150,550,200);
line(540,150,560,150);
line(540,200,560,200);
getch();
closegraph();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:4

Displaying Tamil characters

__________________________________________________________________________

Aim:
To write a c graphics program and to display tamil characters using built in functions.

Algorithm:

1. Detect graphics device and graphic mode, and initialize graphic system using Initgraph().
2. Display Tamil characters using built in functions such as,
3.Drawing circle using circle(int x,int y,int radius)function where x,y are coordinate values.
4. Drawing ellipse using ellipse(int x, int y, int stangle, int end angle, int x radius, int y
radius)function where x,y are coordinate values.
5.Drawing line using line(int x1, int y1, int x2, int y2)function where x1,y1,x2,y2 are line point
coordinate values.
6.Display the images and closegraph() which is used to close current opened graphic device.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"");
setcolor(11);
line(120,160,120,200);
line(140,160,140,200);
line(120,200,140,200);
line(140,200,150,200);
line(150,200,150,210);
line(150,210,150,220);
line(150,220,140,220);
setcolor(12);
line(160,160,190,160);
line(160,160,160,200);
line(180,160,180,200);
line(180,200,160,220);
setcolor(5);
line(210,160,270,160);
ellipse(210,180,0,360,8,20);
ellipse(230,180,0,360,8,20);
ellipse(250,180,0,360,8,20);
line(265,140,265,200);
ellipse(273,140,0,180,8,20);
setcolor(4);
line(280,160,280,200);
line(280,200,320,200);
ellipse(305,200,0,180,15,25);
setcolor(3);
line(330,160,360,160);
line(330,160,330,200);
line(350,160,350,200);
getch();
closegraph();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:5

Display Date of birth using Built in functions

__________________________________________________________________________

Aim:
To write a c graphics program and to display date of birth using built in functions.

Algorithm:

1. Detect graphics device and graphic mode, and initialize graphic system using Initgraph().
2. Display date of birth using built in functions such as,
3.Drawing circle using circle(int x,int y,int radius)function where x,y are coordinate values.
4. Drawing line using line(int x1, int y1, int x2, int y2)function where x1,y1,x2,y2 are line point
coordinate values.
5.Display the images and closegraph() which is used to close current opened graphic device.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"");
//2
line(80,200,110,200);
line(110,200,110,230);
line(110,230,80,230);
line(80,230,80,260);
line(80,260,110,260);
//0
line(120,200,120,260);
line(120,200,150,200);
line(150,200,150,260);
line(120,260,150,260);
//0
line(170,200,170,260);
line(170,200,200,200);
line(200,200,200,260);
line(200,260,170,260);
//5
line(210,200,240,200);
line(210,200,210,230);
line(210,230,240,230);
line(240,230,240,260);
line(240,260,210,260);
//1
line(260,200,260,260);
//9
line(270,200,300,200);
line(270,200,270,230);
line(270,230,300,230);
line(300,200,300,260);
//9
line(310,200,340,200);
line(310,200,310,230);
line(310,230,340,230);
line(340,200,340,260);
//1
line(350,200,350,260);
getch();
closegraph();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:6

Dream house using Built in functions

__________________________________________________________________________

Aim:
To write a c graphics program and to display dream house using built in functions.

Algorithm:

1. Detect graphics device and graphic mode, and initialize graphic system using Initgraph().
2. Display dream house using built in functions such as,
3.Drawing circle using circle(int x,int y,int radius)function where x,y are coordinate values.
4. Drawing line using line(int x1, int y1, int x2, int y2)function where x1,y1,x2,y2 are line point
coordinate values.
5. Drawing polygon using draypoly(int number of points, int polypoints)function.
6.Display the images and closegraph() which is used to close current opened graphic device.

PROGRAM:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main(){
int gdriver, gmode;
gdriver = DETECT;
int col=BROWN;
initgraph(&gdriver, &gmode, "c:\\Turboc3\\BGI\\Disk\\Turboc3\\BGI");
int arr[106] = {
25,400,52,100,25,100,60,50,
95,100,68,100,95,400,165,400,
165,175,150,175,150,150,160,150,
160,165,170,165,170,150,180,150,
180,165,190,165,190,150,200,150,
200,175,185,175,185,270,270,270,
270,75,240,75,310,25,380,75,
350,75,350,270,420,270,420,175,
405,175,405,150,415,150,415,165,
425,165,425,150,435,150,435,165,
445,165,445,150,455,150,455,175,
440,175,440,400,510,400,537,100,
510,100,545,50,580,100,555,100,
580,400
};
setcolor(col);
rectangle(0,0,getmaxx(),getmaxy());
drawpoly(53,arr);
line(0,400,getmaxx(),400);
line(290,400,290,310);
line(330,400,330,310);
arc(310,310,0,180,20);
circle(310,150,18);
setfillstyle(1,col);
floodfill(30,395,col);
floodfill(520,395,col);
floodfill(310,55,col);
setfillstyle(1,GREEN);
floodfill(100,440,col);
getch();
closegraph();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:7

Display National flag using Built in functions

__________________________________________________________________________

Aim:
To write a c graphics program and to display national flag using built in functions.

Algorithm:

1. Detect graphics device and graphic mode, and initialize graphic system using Initgraph().
2. Display national flag using built in functions such as,

3.Drawing circle using circle(int x,int y,int radius)function where x,y are coordinate values.
4. Drawing line using line(int x1, int y1, int x2, int y2)function where x1,y1,x2,y2 are line point
coordinate values.
5. Drawing rectangle using rectangle(int left,int top,int right,int bottom)function.
6. To fill color using,
setcolor(WHITE);
setfillstyle(int pattern,WHITE);
floodfill(int x,int y,WHITE);
7.Display the images and closegraph() which is used to close current opened graphic device.

PROGRAM:

#include <graphics.h>
#include <stdlib.h>
void main()
{
int gdriver, gmode;
gdriver = DETECT;
initgraph(&gdriver, &gmode, "c:\\Turboc3\\BGI\\Disk\\Turboc3\\BGI");setcolor(RED);
setcolor(RED);
rectangle(150,100,300,140);
setfillstyle(1,RED);
floodfill(200,120,RED);
setcolor(WHITE);
rectangle(150,140,300,180);
setfillstyle(1,WHITE);
floodfill(200,150,WHITE);
setcolor(GREEN);
rectangle(150,180,300,220);
setfillstyle(1,GREEN);
floodfill(200,200,GREEN);
setcolor(BROWN);
rectangle(150,100,140,400);
setfillstyle(1,BROWN);
floodfill(145,140,BROWN);
setcolor(BLUE);
circle(225,160,20);
line(225,140,225,180);
line(230,141,220,179);
line(235,142,215,178);
line(238,145,212,176);
line(242,150,208,169);
line(244,155,206,165);
line(205,160,245,160);
line(244,166,205,156);
line(245,171,207,150);
line(239,175,211,146);
line(236,176,214,143);
line(232,179,219,142);
getch();
closegraph();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:8

Display Ship on the river using Built in functions

__________________________________________________________________________

Aim:
To write a c graphics program and to ship on the river using built in functions.

Algorithm:

1. Detect graphics device and graphic mode, and initialize graphic system using Initgraph().
2. Display ship on the river using built in functions such as,
3.Drawing circle using circle(int x,int y,int radius)function where x,y are coordinate values.
4. Drawing line using line(int x1, int y1, int x2, int y2)function where x1,y1,x2,y2 are line point
coordinate values.
5. Drawing rectangle using rectangle(int left,int top,int right,int bottom)function.
6. To fill color using,
setcolor(WHITE);
setfillstyle(int pattern,WHITE);
floodfill(int x,int y,WHITE);
7. Drawing ellipse using ellipse(int x, int y, int stangle, int end angle, int x radius, int y
radius)function where x,y are coordinate values.
8.Display the images and closegraph() which is used to close current opened graphic device.

PROGRAM:
#include<graphics.h>
#include<conio.h>
void main()
{
int gdriver, gmode;
gdriver = DETECT;
initgraph(&gdriver, &gmode, "c:\\Turboc3\\BGI\\Disk\\Turboc3\\BGI");
setcolor(BROWN);
line(100,250,500,250);
line(100,250,150,400);
line(500,250,450,400);
setcolor(RED);
line(200,200,350,200);
line(225,150,325,150);
line(250,100,300,100);
line(200,200,200,250);
line(350,200,350,250);
line(225,150,225,200);
line(325,150,325,200);
line(250,100,250,150);
line(300,100,300,150);
setcolor(11);
line(400,250,400,100);
line(450,250,450,100);
line(400,100,450,100);
//windows
setcolor(7);
rectangle(130,260,150,280);
rectangle(160,260,180,280);
rectangle(190,260,210,280);
rectangle(220,260,240,280);
rectangle(250,260,270,280);
rectangle(280,260,300,280);
rectangle(310,260,330,280);
rectangle(340,260,360,280);
rectangle(370,260,390,280);
rectangle(400,260,420,280);
rectangle(430,260,450,280);
rectangle(460,260,480,280);
rectangle(140,290,160,310);
rectangle(170,290,190,310);
rectangle(200,290,220,310);
rectangle(230,290,250,310);
rectangle(260,290,280,310);
rectangle(290,290,310,310);
rectangle(320,290,340,310);
rectangle(350,290,370,310);
rectangle(380,290,400,310);
rectangle(410,290,430,310);

rectangle(440,290,460,310);
rectangle(160,320,180,340);
rectangle(190,320,210,340);
rectangle(220,320,240,340);
rectangle(250,320,270,340);
rectangle(280,320,300,340);
rectangle(310,320,330,340);
rectangle(340,320,360,340);
rectangle(370,320,390,340);
rectangle(400,320,420,340);
rectangle(430,320,450,340);
rectangle(170,350,190,370);
rectangle(200,350,220,370);
rectangle(230,350,250,370);
rectangle(260,350,280,370);
rectangle(290,350,310,370);
rectangle(320,350,340,370);
rectangle(350,350,370,370);
rectangle(380,350,400,370);
rectangle(410,350,430,370);
setcolor(3);
ellipse(150,420,0,160,30,20);
ellipse(190,420,0,160,30,20);
ellipse(230,420,0,160,30,20);
ellipse(270,420,0,160,30,20);
ellipse(310,420,0,160,30,20);
ellipse(350,420,0,160,30,20);
ellipse(390,420,0,160,30,20);
ellipse(430,420,0,160,30,20);
ellipse(470,420,0,160,30,20);
setcolor(7);
setfillstyle(1,7);
circle(445,60,15);
floodfill(445,60,7);
setcolor(7);
setfillstyle(1,7);
circle(420,85,10);
floodfill(420,85,7);
setcolor(7);
setfillstyle(1,7);
circle(420,30,20);
floodfill(420,30,7);
getch();
closegraph();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:9

DDA LINE DRAWING

__________________________________________________________________________

AIM
To write a graphics program and to implement DDA line drawing algorithm to draw line.

ALGORITHM

1.Detect graphic derive and graphics mode to initialize the graphics system using initgraph().
2.Declare the variables within detect graphics mode and init graph()
3.Read the values x1,y1,x2,y2 respectively.
4.Assign the values as dx=x2-x1,dy=y2-y1.
5.Chech if(dx>dy)then step=dx else step=dy.
6.Find out the value of next points for the line using xincr&yincr.
7.Plot the pixel positions one by one from k=0 to step such printing the points of x y where
x=x+incr&y=y+incr.
8.Display the line & close graph () which is used to close current graphics mode initialized and
detected.

PROGRAM:

#include<graphics.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define ro(a)((int)(a+0.5))
void main()
{
int x1,x2,y1,y2,dx,dy,k,step;
float xincr,yincr,x,y;
int graphicdriver=DETECT,graphicmode;
initgraph(&graphicdriver,&graphicmode," ");
printf("\n\n\tDDA LINE DRAWING ALGORITHM");
printf("\n\t----------------------------");
printf("\n\n\nEnter the value of x1,y1,x2,y2:::");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x=x1,y=y1;
dx=x2-x1;dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xincr=dx/(float)step;
yincr=dy/(float)step;
putpixel(ro(x),ro(y),2);
for(k=0;k<step;k++)
{
x=x+xincr;
y=y+yincr;
putpixel(ro(x),ro(y),2);
}
getch();
closegraph();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Bressanhams line drawing algorithm

Exno:10

__________________________________________________________________________

Aim:
To write a c graphics program and to implement Bressanhams line drawing algorithm.

Algorithm:

1. Detect graphics device and graphic mode, and initialize graphic system using Initgraph().
2. Bressanhams line drawing algorithm such as,
3.Input the twoline endpoints and store the left endpoint in (x0,y0)

4.Load (x0,

y0

into the frame buffer; that is, plot the first point.

5. Calculate constants x, y, 2y, and 2y 2, and obtain the starting


value for the decision parameter as
p0 = 2y - X
6. At each xk along the line, starting at k = 0, perform the following test:
If Pr < 0, the next point to plot is (Xk + 1, yk) and
Pk+1= P k + Y
Otherwise, the next point to plot is (xi + I , yr + 1) and
pk+, = pk + 2y - 2x
7. Repeat step 4 x times.
8.Display the images and closegraph() which is used to close current opened graphic device.

PROGRAM:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void main()
{
int gdriver = DETECT, gmode;
int x1,y1,x2,y2,dx,dy,x,y,xend,p;
initgraph(&gdriver, &gmode, "");
printf("\n\n\n\tBresanham line drawing algorithm");
printf("\n\t-----------------------------");
printf("\n\nEnter the value of x1,y1,x2,y2:::");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
dx=abs(x1-x2), dy=abs(y1-y2),p=2*dy-dx;
if(x1>x2)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel(x,y,WHITE);
while(x<xend)
{
x++;
if(p<0)
p+=2*dy;
else
{
y++;
p+=2*(dy-dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Bressanhams mid point circle generating algorithm

Exno:11

__________________________________________________________________________

Aim:
To write a c graphics program and to implement Bressanhams mid point circle generating
algorithm.

Algorithm:
1. Detect graphics device and graphic mode, and initialize graphic system using Initgraph().
2. Bressanhams mid point circle generating algorithm such as,
3.Input radius r and circle center (x, y,), and 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
P0=5/4-r
5. At each xk position, starting at k = 0, perform the following test: If Pk<0, the next point along the
circle centered on (0,0) is (xk+1, yk) and
Pk+1 =

pk

2Xk+1

+1

Otherwise, the next point along the circle is (xk + 1, yk - 1) and where 2xk+1, = 2Xk + 2
and 2yk+1 = 2yk - 2.
6. Determine symmetry points in the other seven octants.
7. Move each calculated pixel position (x, y) onto the circled path centered on (xc, yc) and plot the
coordinate values:
x=x+xc, y=y+yc
8. Repeat steps 5 through 7 until X>= y.
9.Display the images and closegraph() which is used to close current opened graphic device.

PROGRAM:
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void circlepoints(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter+x,ycenter+y,WHITE);
putpixel(xcenter-x,ycenter+y,WHITE);
putpixel(xcenter+x,ycenter-y,WHITE);
putpixel(xcenter-x,ycenter-y,WHITE);
putpixel(xcenter+y,ycenter+x,WHITE);
putpixel(xcenter-y,ycenter+x,WHITE);
putpixel(xcenter+y,ycenter-x,WHITE);
putpixel(xcenter-y,ycenter-x,WHITE);
}
void circle1(int xcenter,int ycenter,int radius)
{
int x=0,y=radius,p=1-radius;
circlepoints(xcenter, ycenter,x,y);
while( x < y)
{
if(p<0) x++;
else
{
x++;
y--;
}
if(p<0) p+=2 * x+1;
else
{
p+= 2 * (x-y) +1;
}
circlepoints(xcenter,ycenter,x,y);
}
}
void main()
{
int gdriver = DETECT, gmode;
int x1,y1,r;
initgraph(&gdriver, &gmode, "");
printf("\n\n\n\tBresanham mid-point circle generating algorithm");
printf("\n\t--------------------------------------------------");
printf("\n\n\nEnter the values of x-y coordinates and radius::");
scanf("%d%d%d",&x1,&y1,&r);
circle1(x1,y1,r);
getch();
closegraph();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:12

Ellipse generating algorithm

__________________________________________________________________________

Aim:
To write a c graphics program and to implement Ellipse generating algorithm.

Algorithm:
1. Detect graphics device and graphic mode, and initialize graphic system using Initgraph().
2. Ellipse generating algorithm such as,

as

3.Input rx, ry, and ellipse center (xc, yc,), and obtain the first point on an ellipse centered on the origin
(x0,y0)=(0,ry)
4.Calculate the initial value of the decision parameter in region as
P10=ry^2 1-rx^2ry+ rx^2
5. At each x, position in region 1, starting at k = 0, perform the following test:
If p1k < 0, the next point along the ellipse centered on (0, 0) is (Xk+1,yk) and
P1k+1 =

p1k

2ry^2xk+1

ry^2

Otherwise, the next point along the circle is (xk + 1, yk-1) and
P1k+1 =

p1k

2ry^2xk+1

2ry^2xk+1

2ry^2xk

2ry^2,

2ry^2xyk+1

2rx^2yk

2rx^2

2rx^2yk+1

+ry^2

With

and continue until 2ry^2X >= 2rx^2y.


6. Calculate the initial value of the decision parameter in region 2 using the last point (xo, yo)
calculated in region 1 as
P20= ry^2(x0+ 1/2) +

rx^2

(y0-1)^2

ry^2rX^2

7. At each yk position in region 2, starting at k = 0, perform the following test:


If p2k> 0,

the next point along the ellipse centered on (0, 0) is (xk+1,yk-1) and

P2k+1=p2k-2rx^2yK+1

rx^2

Otherwise, the next point along the circle is (Xk + 1, yk - 1) and


P2k+1 =

p2k

2ry^2Xk-1

2rx^2yk-1

r^2

using the same incremental calculations for X and Y as in region 1.


8.Determine symmetry points in the other three quadrants.
7. Move each calculated pixel position (x, y) onto the elliptical path centered on (x,, y,) and plot the
coordinate values:
x=x+xc,
y=y+yc
8. Repeat the steps for region 1 until 2ry^2x

>=

2rx^2y

9.Display the images and closegraph() which is used to close current opened graphic device.

PROGRAM:

#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#define round(a)((int)(a+0.5))
void ellip(int xcen,int ycen,int x,int y)
{
putpixel(xcen+x,ycen+y,2);
putpixel(xcen-x,ycen+y,2);
putpixel(xcen+x,ycen-y,2);
putpixel(xcen-x,ycen-y,2);
}
void main()
{
int gd=DETECT,gm;
int xcen,ycen,rx,ry;
int rx1,ry1,rx2,ry2;
int p,x,y,px,py;
void ellip(int,int,int,int);
initgraph(&gd,&gm,"");
cout<<"\n\n\t\t Ellipse DRAWING ALGORITHM";
cout<<"\n\n\nEnter the coordinates degree(Rx,Ry)::";
cin>>rx>>ry;
cout<<"Enter coordinates center(x,y)::";
cin>>xcen>>ycen;
rx1=rx*rx,ry1=ry*ry,rx2=2*rx1,ry2=2*ry1;
x=0,y=ry,px=0,py=rx2*y;
ellip(xcen,ycen,x,y);
p=round(ry1-(rx1*ry)+(0.25*rx1));
while(px<py)
{
x++;
px+=ry2;
if(p<0)
p+=ry1+px;
else
{
y--;
py-=rx2;
p+=ry1+px-py;
}
ellip(xcen,ycen,x,y);
}
p=round(ry1*(x+0.5)*(x+0.5)+rx1*(y-1)*(y-1)-rx1*ry1);

while(y>0)
{
y--;
py-=rx2;
if(p>0)
p+=rx1-py;
else
{
x++;
px+=ry2;
p+=rx1-py+px;
}
ellip(xcen,ycen,x,y);
}
getch();
closegraph();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:13

2D transformation

__________________________________________________________________________

AIM:
To write a program in C to implement 2D Transformation on an object.

ALGORITHM:

1. Enter the choice for transformation.


2. Perform the translation, rotation, scaling, reflection and shearing of 2D object.
3. Get the needed parameters for the transformation from the user.
4. Incase of rotation, object can be rotated about x or y axis.
5. Display the transmitted object in the screen.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp(int n,float c[][3])
{
float maxx,maxy;
int i;
maxx=getmaxx();
maxy=getmaxy();
maxx=maxx/2;
maxy=maxy/2;
i=0;
while(i<n-1)
{
line(maxx+c[i][0],maxy-c[i][1],maxx+c[i+1][0],maxy-c[i+1][1]);
i++;
}
i=n-1;
line(maxx+c[i][0],maxy-c[i][1],maxx+c[0][0],maxy-c[0][1]);
setcolor(GREEN);
line(0,maxy,maxx*2,maxy);
line(maxx,0,maxx,maxy*2);
setcolor(WHITE);
}
void mul(int n,float b[][3],float c[][3],float a[][3])
{
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
a[i][j]=0;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
{
a[i][j] = a[i][j] + (c[i][k] * b[k][j]);
}
}
void translation(int n,float c[][3],float tx,float ty)
{
int i;
for(i=0;i<n;i++)
{
c[i][0]=c[i][0]+tx;
c[i][1]=c[i][1]+ty;
}
}
void scaling(int n,float c[][3],float sx,float sy)
{
float b[10][3],a[10][3];

int i=0,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=sx;
b[1][1]=sy;
b[2][2]=1;
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
}
void rotation(int n,float c[][3],float ra)
{
int i=0,j;
float b[10][3],xp,yp,a[10][3];
xp=c[0][0];
yp=c[0][1];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=b[1][1]=cos(ra*3.14/180);
b[0][1]=sin(ra*3.14/180);
b[1][0]=-sin(ra*3.14/180);
b[2][0]=(-xp*cos(ra*3.14/180))+(yp*sin(ra*3.14/180))+xp;
b[2][1]=(-xp*sin(ra*3.14/180))-(yp*cos(ra*3.14/180))+yp;
b[2][2]=1;
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
}
void reflection(int n,float c[][3])
{
float b[10][3],a[10][3];
int i=0,ch,j;
cleardevice();
printf("\n\t* * MENU * *");
printf("\n\t1) ABOUT X-AXIS");
printf("\n\t2) ABOUT Y-AXIS");
printf("\n\t3) ABOUT ORIGIN");
printf("\n\t4) ABOUT X=Y");
printf("\n\t5) ABOUT -X=Y");
printf("\n\t6) EXIT");
printf("\n\tENTER YOUR CHOICE :
");
scanf("%d",&ch);
clrscr();
cleardevice();
disp(n,c);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
b[i][j]=0;
if(i==j)

b[i][j]=1;
}
switch(ch)
{
case 1:
b[1][1]=-1;
break;
case 2:
b[0][0]=-1;
break;
case 3:
b[0][0]=-1;
b[1][1]=-1;
break;
case 4:
b[0][0]=0;
b[1][1]=0;
b[0][1]=1;
b[1][0]=1;
break;
case 5:
b[0][0]=0;
b[1][1]=0;
b[0][1]=-1;
b[1][0]=-1;
break;
case 6:
break;
default:
printf("\n\tINVALID CHOICE ! ");
break;
}
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
}
void shearing(int n,float c[][3])
{
float b[10][3],sh,a[10][3];
int i=0,ch,j;
cleardevice();
printf("\n\t* * * MENU * * *");
printf("\n\t1) X SHEARING");
printf("\n\t2) Y SHEARING");
printf("\n\t3) EXIT ");
printf("\n\tENTER YOUR CHOICE :
");
scanf("%d",&ch);
if(ch==3)
return;
printf("\n\tENTER THE VALUE FOR SHEARING:
scanf("%f",&sh);
clrscr();

");

cleardevice();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
for(i=0;i<3;i++)
b[i][i]=1;
switch(ch)
{
case 1:
b[1][0]=sh;
break;
case 2:
b[0][1]=sh;
break;
case 3:
break;
default:
printf("\n\tINVALID CHOICE ! ");
break;
}
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
}
void main()
{
int i,j,k,cho,n,gd=DETECT,gm;
float c[10][3],tx,ty,sx,sy,ra;
initgraph(&gd,&gm,"d:\\turboc~1\\bgi");
printf("\nEnter the number of vertices :
");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the co-ordinates of the %d vertex :",i+1);
scanf("%f%f",&c[i][0],&c[i][1]);
c[i][2]=1;
}
do
{
clrscr();
cleardevice();
printf("\n\t\t\t * * * MENU * * *");
printf("\n\t 1) TRANSLATION");
printf("\n\t 2) SCALING ");
printf("\n\t 3) ROTATION ");
printf("\n\t 4) REFLECTION ");
printf("\n\t 5) SHEARING");
printf("\n\t 6) EXIT");
printf("\n\t ENTER YOUR CHOICE:
");
scanf("%d",&cho);
switch(cho)
{

case 1:
printf("\n\tEnter translation factor for X & Y axis :\t");
scanf("%f%f",&tx,&ty);
clrscr();
cleardevice();
setcolor(BLUE);
disp(n,c);
translation(n,c,tx,ty);
setcolor(RED);
disp(n,c);
getch();
break;
case 2:
printf("\n\tEnter scaling factor for X & Y axis :\t");
scanf("%f%f",&sx,&sy);
clrscr();
cleardevice();
setcolor(BLUE);
disp(n,c);
scaling(n,c,sx,sy);
getch();
break;
case 3:
printf("\n\n\tEnter the angle of rotation:\t");
scanf("%f",&ra);
clrscr();
cleardevice();
setcolor(BLUE);
disp(n,c);
rotation(n,c,ra);
getch();
break;
case 4:
clrscr();
cleardevice();
setcolor(BLUE);
disp(n,c);
reflection(n,c);
getch();
break;
case 5:
clrscr();
cleardevice();
setcolor(BLUE);
disp(n,c);
shearing(n,c);
getch();
break;
case 6 :
exit();
break;
default:
printf("\n\tInvalid choice !!"); break;
}
}while(cho!=6);
getch();
closegraph();}

OUTPUT

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:14

Implementing motion of the object using algorithm

__________________________________________________________________________

AIM:
To write a program in C to implement 2D Transformation on an object using algorithm.

ALGORITHM:

1. importing DDA line drawing algorithm


2. importing Bresenham circle generating line drawing algorithm
3. Perform the translation 2D object.
4. Get the needed parameters for the transformation from the user.
5. use for loop for motion
6. Read the translation vector value and perform motion on transformation object.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define ro(a)((int)(a+0.5))
void circlepoints(int xcenter,int ycenter,int x,int y);
void circle1(int xcenter,int ycenter,int radius);
int size,*bitmap;
void circlepoints(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter+x,ycenter+y,WHITE);
putpixel(xcenter-x,ycenter+y,WHITE);
putpixel(xcenter+x,ycenter-y,WHITE);
putpixel(xcenter-x,ycenter-y,WHITE);
putpixel(xcenter+y,ycenter+x,WHITE);
putpixel(xcenter-y,ycenter+x,WHITE);
putpixel(xcenter+y,ycenter-x,WHITE);
putpixel(xcenter-y,ycenter-x,WHITE);
}
void circle1(int xcenter,int ycenter,int radius)
{
int x=0;
int y=radius;
int p=1-radius;
circlepoints(xcenter, ycenter,x,y);
while( x < y)
{
if(p<0)
x++;
else
{
x++;
y--;
}
if(p<0)
p+=2 * x+1;
else
{
p+= 2 * (x-y) +1;
}
circlepoints(xcenter,ycenter,x,y);
}
}
void DAA(int x1,int y1,int x2,int y2)
{
int dx,dy,k,step;
float xincr,yincr,x=x1,y=y1;
dx=x2-x1;dy=y2-y1;

if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xincr=dx/(float)step;
yincr=dy/(float)step;
putpixel(ro(x),ro(y),2);
for(k=0;k<step;k++)
{
x=x+xincr;
y=y+yincr;
putpixel(ro(x),ro(y),2);
}}
void car(int x,int y)
{
setcolor(4);
circle1(50+x,200+y,20);
circle1(200+x,200+y,20);
setcolor(5);
circle1(50+x,200+y,10);
circle1(200+x,200+y,10);
setcolor(2);
setcolor(9);
DAA(25+x,200+y,0+x,200+y);
DAA(0+x,200+y,0+x,160+y);
DAA(0+x,160+y,40+x,160+y);
DAA(40+x,160+y,70+x,130+y);
DAA(70+x,130+y,170+x,130+y);
DAA(170+x,130+y,200+x,160+y);
DAA(200+x,160+y,260+x,160+y);
DAA(260+x,160+y,260+x,200+y);
DAA(260+x,200+y,225+x,200+y);
DAA(175+x,200+y,75+x,200+y);
}
void ani()
{
int i;
size=imagesize(0,130,270,230);
bitmap=malloc(size);
getimage(0,130,270,230,bitmap);
for(i=0;!kbhit();i=i+10)
{
if(i>350)
i=0;
putimage(i,130,bitmap,0);
delay(100);
cleardevice();
}
}
void main()
{
int op,xa,ya,xb,yb,xc,yc,tx,ty,sx,sy,angle,i,j,size,*bitmap,xcen,ycen,r;

int gd=DETECT,gm;
initgraph(&gd,&gm,"");
tx=0,ty=0;
car(tx,ty);
ani();
printf("\nEnter the tx and ty values-->");
scanf("%d%d",&tx,&ty);
car(tx,ty);
//ani();
for(i=0;!kbhit();i=i+10)
{
car(tx+i,ty);
delay(100);
cleardevice();
}
getch();
closegraph();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:15

Cohen Sutherland line clipping algorithm

__________________________________________________________________________

AIM:
To write a program in C to implement Cohen-Sutherland line clipping algorithm .

ALGORITHM:

1 : Given a line segment with endpoint p1=(x1,y1) and p2=(x2,y2)


2 : Compute the 4-bit codes for each endpoint.
3 : If both codes are 0000,(bitwise OR of the codes yields 0000 ) line lies
completely inside the window: pass the endpoints to the draw routine.
4 : If both codes have a 1 in the same bit position (bitwise AND of the codes
is not 0000), the line lies outside the window. It can be trivially rejected.
5 : If a line cannot be trivially accepted or rejected, at least one of the two
endpoints must lie outside the window and the line segment crosses a
window edge. This line must be clipped at the window edge before being
passed to the drawing routine.
6 : Examine one of the endpoints, say P1=(X1,Y1). Read P1 's 4-bit code in
order: Left-to-Right, Bottom-to-Top.
7 : When a set bit (1) is found, compute the intersection I of the
corresponding window edge with the line from P1 to P2.
Replace P1 with I and repeat the algorithm.

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
int xa,xb,ya,yb;
int encode(float x,float y)
{
return(8*(x<xa)+4*(x>xb)+2*(y<ya)+(y>yb));
}
void swap(int *a,int *b)
{
int *t=a;
a=b;
b=t;
}
void lineclip(int x1,int y1,int x2,int y2)
{
float dx,dy;
int c1,c2;
c1=encode(x1,y1);
c2=encode(x2,y2);
while(c1|c2)
{
if(c1&c2)
return;
dx=x2-x1;
dy=y2-y1;
if(c1)
{
if(c1&8)
{
y1+=dy*(xa-x1)/dx;
x1=xa;
}
else if(c1&4)
{
y1+=dx*(xb-x1)/dx;
x1=xb;
}
else if(c1&2)
{
x1+=dx*(ya-y1)/dy;
y1=ya;
}
else if(c1&1)
{
x1+=dx*(yb-y1)/dy;
y1=yb;
}
c1=encode(x1,y1);

}
else
{
if(c2&8)
{
y2+=dy*(xa-x2)/dx;
x2=xa;
}
else if(c2&4)
{
y2+=dy*(xb-xa)/dx;
x2=xb;
}
else if(c2&2)
{
x2+=dx*(yb-y2)/dy;
y2=ya;
}
else if(c2&1)
{
x2+=dx*(yb-y2)/dy;
y2=yb;
}
c2=encode(x2,y2);
}
}
line(x1,y1,x2,y2);
}
void main()
{
int gd=DETECT,gm,x1,y1,x2,y2;
initgraph(&gd,&gm,"");
textbackground(0);
setbkcolor(1);
cout<<"\n\nCohen sutherland line clipping algorithm\n-------------------------------";
cout<<"\n\n\nEnter 4 values of clipping area::";
cin>>xa>>ya>>xb>>yb;
cout<<"\n\nEnter the first co-ordinate value::";
cin>>x1>>y1;
cout<<"\n\nEnter the second co-ordinate value::";
cin>>x2>>y2;
if(x1>x2)
swap(&x1,&x2);
if(y1>y2)
swap(&y1,&y2);
cleardevice();
setcolor(13);
outtextxy(50,50,"Before clipping");
rectangle(xa,ya,xb,yb);
line(x1,y1,x2,y2);

getch();
cleardevice();
outtextxy(50,50,"After clipping");
rectangle(xa,ya,xb,yb);
lineclip(x1,y1,x2,y2);
getch();
closegraph();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:16

Liang barsky line clipping algorithm

__________________________________________________________________________

AIM:
To write a program for line clipping using Liang baskey line clipping algorithm.

ALGORITHM:
1.Define the structure and name as p and their necessary variable.
2.Under the main function declare the necessary variables.
3.Get the values for window coordinates and point values.
4.Then go to clip line function then check the values (dx,wp.x-p1.x&u1,u2)
5.Then assign their values as p1. x=p1.x +u1*dx,p1.y=p1.y+u1*dy.
6.Under the clip test function check whether (p<0.0)if it is true r=q/p.
7.Otherwise check(p>0.0)then do the assignment.
8.Return the v values.
9.Gisplay the clipping window.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
struct p
{
int x,y;
};
typedef struct p point;
int cliptest(float,float,float *,float *);
void clipline(point,point,point,point);
void main()
{
int gd=DETECT,gm;
point wp1,wp2,p1,p2;
initgraph(&gd,&gm,"");
textbackground(0);
setbkcolor(1);
printf("\n\nLiang barsey line clipping algorithm\n-------------------------------");
printf("\n\n\nEnter 4 values of clipping area::");
scanf("%d%d%d%d",&wp1.x,&wp1.y,&wp2.x,&wp2.y);
printf("\n\nEnter the first-second co-ordinate value::");
scanf("%d%d%d%d",&p1.x,&p1.y,&p2.x,&p2.y);
printf("\n\nBEFORE CIPPING");
rectangle(wp1.x,wp1.y,wp2.x,wp2.y);
line(p1.x,p1.y,p2.x,p2.y);
getch();
clipline(wp1,wp2,p1,p2);
getch();
closegraph();
}
void clipline(point wp1,point wp2,point p1,point p2)
{
float u1=0.0,u2=1.0,dx=p2.x-p1.x,dy=p2.y-p1.y;
if(cliptest(-dx,p1.x-wp1.x,&u1,&u2))
if(cliptest(dx,wp2.x-p1.x,&u1,&u2))
{
if(cliptest(-dy,p1.y-wp1.y,&u1,&u2))
if(cliptest(dy,wp2.y-p1.y,&u1,&u2))
{
if(u1<1.0)
{
p2.x=p1.x+u2*dx;
p2.y=p1.y+u2*dy;
}
if(u1>0.0)
{
p1.x=p1.x+u1*dx;
p1.y=p1.x+u1*dy;
}

}
cleardevice();
outtextxy(50,50,"ATER CLIPPING");
rectangle(wp1.x,wp1.y,wp2.x,wp2.y);
line(p1.x,p1.y,p2.x,p2.y);
getch();
}
}
int cliptest(float p,float q,float *u1,float *u2)
{
float r;
int v=1;
if(p<0.0)
{
r=q/p;
if(r>*u1)
*u1=r;
}
if(p>0.0)
{
r=q/p;
if(r<*u1)
v=0;
else if(r<*u2)
*u2=r;
}
if(p==0.0)
{
if(q<0.0)
v=0;
}
if(v==0)
{
cleardevice();
printf("\n\nLine is ompletely inside");
getch();
exit(1);
}
return v;
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:17

Text clipping

__________________________________________________________________________

AIM:
To write a program for line clipping using text clipping algorithm.

ALGORITHM:
1.To initialize graphic drive & graphic mode using DETECT graph and initgraph().
2.Reading the window coordinates from the user.
3.Reading the text coordinates from the user.
4.Display the string 1,string 2,string 3,string 4 on the window before clipping.
5.Get the values from the user.
6.Check the values whether it display inside the window coordinates.
7.Close graph() which is used to open the current graphics mode.

PORGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void show_clipped_text(int,int,char *,int,int,int);
char key=NULL;
int flag=0,x_min,y_min,x_max,y_max,width,height,dx,dy,inc_dec;
int color,x1,y1,x2,y2;
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
setcolor(15);
printf("\n\t\tEnter the value of x-min,y-min,x-max,y-max:");
scanf("%d%d%d%d",&x_min,&y_min,&x_max,&y_max);
rectangle(x_min,y_min,x_max,y_max);
settextstyle(0,0,2);
outtextxy(160,160,"String 1");
outtextxy(250,300,"String 2");
settextstyle(0,1,2);
outtextxy(350,120,"String 3");
outtextxy(440,180,"String 4");
outtextxy(100,300,"Press C to See The Clipped");
do
{
key=getch();
}
while(key!='C'&& key!='c');
settextstyle(0,0,1);
setcolor(0);
setcolor(12);
cleardevice();
outtextxy(100,100,"After Clipping");
outtextxy(213,350,"Press Key to Exit:");
setcolor(10);
rectangle(x_min,y_min,x_max,y_max);
show_clipped_text(160,160,"String 1",0,0,2);
show_clipped_text(250,300,"String 2",0,0,2);
show_clipped_text(350,120,"String 3",0,1,2);
show_clipped_text(440,180,"String 4",0,1,2);
getch();
return 0;
}
void show_clipped_text(int x,int y,char *string,int front_style,int text_direction,int front_size)
{
settextstyle(front_style,text_direction,front_size);
flag=0;
width=textwidth(string);
height=textheight(string);
if(text_direction==0)

{
if(x>=x_min&&y>=y_min&&(x+width)<=x_max&&(y+height)<=y_max)
flag=1;
}
else if(text_direction==1)
{
if(x>=x_min&&y>=y_min&&(x+height)<=x_max&&(y+width)<=y_max)
flag=1;
}
if(flag)
outtextxy(x,y,string);
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:18

Polygon clipping

__________________________________________________________________________

AIM:
To write a program for polygon clipping algorithm.

ALGORITHM:
1.Declare the necessary variables.
2.Get the number of coordinate window from the user.
3.Check whether if(!( out code 0|outcode 1))and accept =TRUE otherwise done=TRUE.
4.If the out code is in the top assign y=ymax.
5.If the out code is in Right then y=xmax.
6.Otherwise x=xmax and clip the polygon.
7.Display the clipping of polygon.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#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;
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,50,"After using polygon clipping");
rectangle(xmin,ymin,xmax,ymax);
}
outcode CompOutcode(float x,float y)
{
outcode code=0;
if(y>ymax)
code|=TOP;
else if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else if(x<xmin)
code|=LEFT;
return code;
}
void main()
{
float x1,y1,x2,y2;
int gd=DETECT,gm,n,poly[14],i;
clrscr();
printf("\n\n\t\t*****Polygon clipping******");
printf("\n\n\n\n\tEnter sides of polygon::");
scanf("%d",&n);
printf("\n\tEnter co ordinate value for polygon::");

for(i=0;i<2*n;i++)
{
scanf("%d",&poly[i]);
}
poly[2*n]=poly[0];
poly[(2*n)+1]=poly[1];
printf("\n\nEnter window co-ordinate value(xmin,ymin,ymin,ymax)::");
scanf("%f %f %f %f",&xmin,&ymin,&xmax,&ymax);
initgraph(&gd,&gm," ");
outtextxy(150,50,"Before Using polygon Clipping");
drawpoly(n+1,poly);
rectangle(xmin,ymin,xmax,ymax);
getch();
cleardevice();
for(i=0;i<n;i++)
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
getch();
restorecrtmode();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:19

Window to viewport Transformation

__________________________________________________________________________

AIM:
To write a program for window to viewport.

ALGORITHM:
1. To initialize graphic drive & graphic mode using DETECT graph and initgraph().
2. Reading the window coordinates from the user.
3.Reading the values of viewport coordinates values from the user.
4.Read the object values from the user to display in viewport.
5.Since the scaling factor is calculated using certain functions.
6.Porting to the view and displaying the object.
7.Closegraph() which is used to open the current graphic mode.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
float xwmin,xwmax,ywmin,ywmax;
float xvmin,xvmax,yvmin,yvmax;
float x[10],y[10],yv,xv,sx,sy;
int gd=DETECT,gm,i;
clrscr();
printf("\n\n\t\tWindow to Viewport ");
printf("\n\nEnter window-port coordinates (xwmin,ywmin,xwmax,ywmax)::");
scanf("%f%f%f%f",&xwmin,&ywmin,&xwmax,&ywmax);
printf("\nEnter view-port coordinates(xvmin,yvmin,xvmax,yvmax)::");
scanf("%f%f%f%f",&xvmin,&yvmin,&xvmax,&yvmax);
printf("\nEnter the vertices for triangle:");
for(i=0;i<3;i++)
{
printf("\nEnter coordiante(x%d-y%d):",i,i);
scanf("%f%f",&x[i],&y[i]);
}
sx=((xvmax-xvmin)/(xwmax-xwmin));
sy=((yvmax-yvmin)/(ywmax-ywmin));
initgraph(&gd,&gm,"");
outtextxy(80,30,"***Window port***");
rectangle(xwmin,ywmin,xwmax,ywmax);
for(i=0;i<2;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[2],y[2],x[0],y[0]);
getch();
cleardevice();
for(i=0;i<3;i++)
{
x[i]=xvmin+((x[i]-xwmin)*sx);
y[i]=yvmin+((y[i]-ywmin)*sy);
}
outtextxy(150,10,"***View port***");
rectangle(xvmin,yvmin,xvmax,yvmax);
for(i=0;i<2;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[2],y[2],x[0],y[0]);
getch();
}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Exno:20

3d transformation

__________________________________________________________________________
AIM:
To write a program in C to implement 3D Transformation on an object.

ALGORITHM:

1: Enter the choice for transformation.


2: Perform the translation, rotation and scaling of 3D object.
3: Get the needed parameters for the transformation from the user.
4: Incase of rotation, object can be rotated about x or y axis.
5: Display the transmitted object in the screen.

PORGRAM:
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int i,j;
void tcube()
{
for(i=1;i<=10;i++)
{
cleardevice();
rectangle(50,100,400,300);
rectangle(200-(i*12),200,250-(i*12),250);
rectangle(225-(i*12),175,275-(i*12),225);
line(200-(i*12),200,225-(i*12),175);
line(250-(i*12),200,275-(i*12),175);
line(200-(i*12),250,225-(i*12),225);
line(250-(i*12),250,275-(i*12),225);
delay(400);
}
outtextxy(200,50,"*AFTER TRANSLATION*");
getch();
cleardevice();
}
void rcube()
{
for(i=1;i<=5;i++)
{
cleardevice();
rectangle(100,100,500,300);
rectangle(200+(i*5),200-(i*2),250-(i*5),250-(i*2));
rectangle(225+(i*5),175+(i*2),275-(i*5),225-(i*2));
line(200+(i*5),200-(i*2),225+(i*5),175+(i*2));
line(250-(i*5),200-(i*2),275-(i*5),175+(i*2));
line(200+(i*5),250-(i*2),225+(i*5),225+(i*2));
line(250-(i*5),250-(i*2),275-(i*5),225+(i*2));
delay(200);
}
j=2;
for(;i<=10;i++)
{
cleardevice();
rectangle(100,100,400,300);
rectangle(200+(i*5),200-(i*2),250-(i*5),250-(i*2));
rectangle(225+(i*5),175+(i*2),275-(i*5),225+(i*2));
line(200+((i-j)*5),200-(i*2),225+((i-j)*5),175+(i*2));
line(250-((i-j)*5),200-(i*2),275-((i-j)*5),175+(i*2));
line(200+((i-j)*5),250-(i*2),225+((i-j)*5),225+(i*2));
line(250-((i-j)*5),250-(i*2),275-((i-j)*5),225+(i*2));

delay(200);
j=j+2;
}
outtextxy(250,50,"*After Rotation*");
getch();
cleardevice();
}
void scube()
{
for(i=1;i<=10;i++)
{
cleardevice();
rectangle(100,80,500,300);
rectangle(200,200-(i*8),250+(i*8),250);
rectangle(225,175-(i*8),275+(i*8),225);
line(200,200-(i*8),225,175-(i*8));
line(250+(i*8),200-(i*8),275+(i*8),175-(i*8));
line(200,250,225,225);
line(250+(i*8),250,275+(i*8),225);
delay(200);
}
outtextxy(250,50,"*After scaling*");
getch();
cleardevice();
}
void main()
{
int gd=DETECT,gm,ch;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\n\n\t\t 3D TRANSFORMATIONS\n\t\t*******************\n");
printf("\n\n\tMenu:");
printf("\n\n\t1.Translation\n\t2.Rotation\n\t3.Scaling\n\t4.Exit\n");
while(ch!=0)
{
printf("Enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
tcube();
break;
case 2:
rcube();
break;
case 3:
scube();
break;
case 4:
exit(0);
}}

OUTPUT:

RESULT:
Thus, the program executed successfully and output was obtained.

Potrebbero piacerti anche