Vous êtes sur la page 1sur 78

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

201103002

PROGRAM:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
int main()
{
float x,y,x1,x2,y1,y2, dx, dy;
int ch,i, m,gd=DETECT,gm;
printf("\nDDA Algorithm");
printf("\nEnter the point of X1\n");
scanf("%f",&x1);
printf("Enter the point of Y1\n");
scanf("%f",&y1);
printf("Enter the point of X2\n");
scanf("%f",&x2);
printf("Enter the point of Y2\n");
scanf("%f",&y2);
initgraph(&gd,&gm," ");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
{
m=dx;
}
else
{
m=dy;
}
x=x1;
y=y1;
putpixel((int)x,(int)y,15);
for(i=1;i<=m;i++)
{
x=x+dx/m;
y=y+dy/m;
putpixel((int)x,(int)y,15);
}
getch();
closegraph();
return 0;

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

}
OUTPUT:

RESULT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

PROGRAM:
#include<stdio.h>
#include<graphics.h>
int main()
{
int gd=DETECT, gm;
int dx, dy, p, end;
float x1, x2, y1, y2, x, y;
initgraph(&gd, &gm, " ");
printf("Enter Value of X1:\n");
scanf("%f", &x1);
printf("Enter Value of Y1:\n");
scanf("%f", &y1);
printf("Enter Value of X2:\n");
scanf("%f", &x2);
printf("Enter Value of Y2:\n");
scanf("%f", &y2);
clearviewport();
dx=abs(x1-x2);
dy=abs(y1-y2);
p=2*dy-dx;
if(x1>x2)
{
x=x2;
y=y2;
end=x1;
}
else
{
x=x1;
y=y1;
end=x2;
}
putpixel(x, y, 15);
while(x<end)
{
x=x+1;
if(p < 0)
{

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

p=p+2*dy;
}
else
{
y=y+1;
p=p+2*(dy-dx);
}
putpixel(x, y, 15);
}
getch();
closegraph();
return 0;
}
OUTPUT:

RESULT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

PROGRAM:
#include<stdio.h>
#include<graphics.h>
int main()
{
int gd=DETECT,gm,i,r,x,y,xc,yc;
float d;
initgraph(&gd,&gm," ");
printf("Enter Radius\n");
scanf("%d",&r);
printf("Enter Center of circle\n");
scanf("%d",&xc);
scanf("%d",&yc);
d=1.25-r;
x=0;
y=r;
do
{
if(d<0)
{
x=x+1;
d=d+2*x+1;
}
else
{
x=x+1;
y=y-1;
d=d+2*x-2*y+10;
}
putpixel(xc+x,yc+y,5);
putpixel(xc-y,yc-x,5);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc+x,5);
putpixel(xc+y,yc+x,5);
putpixel(xc-x,yc-y,5);
putpixel(xc+x,yc-y,5);
putpixel(xc-x,yc+y,5);

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

}while(x<y);
getch();
return 0;
}
OUTPUT:

RESULT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

PROGRAM:
#include<stdio.h>
#include<graphics.h>
int main()
{
int gd=DETECT,gm;
int x,y,r;
void cir(int,int,int);
printf("Enter the Midpoints and Radius:");
scanf("%d%d%d",&x,&y,&r);
initgraph(&gd,&gm," ");
cir(x,y,r);
getch();
closegraph();
return 0;
}
void cir(int x1,int y1,int r)
{
int x=0,y=r,p=1-r;
void cliplot(int,int,int,int);
cliplot(x1,y1,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
cliplot(x1,y1,x,y);
}
}
void cliplot(int xctr,int yctr,int x,int y)

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

{
putpixel(xctr +x,yctr +y,1);
putpixel(xctr -x,yctr +y,1);
putpixel(xctr +x,yctr -y,1);
putpixel(xctr -x,yctr -y,1);
putpixel(xctr +y,yctr +x,1);
putpixel(xctr -y,yctr +x,1);
putpixel(xctr +y,yctr -x,1);
putpixel(xctr -y,yctr -x,1);
getch();
}
OUTPUT:
Enter the Midpoints and Radius:
100
100
50

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

RESULT:

PROGRAM:
#include <stdio.h>
#include <graphics.h>
void ellipseMidpoint(float, float, float, float);
void drawEllipse(float, float, float, float);
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();
return 0;
}
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);
p = rySq - (rxSq * ry) + (0.25 * rxSq); //Region 1
while (px < py)
{
x++;
px = px + 2 * rySq;
if (p < 0)

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

{
p = p + rySq + px;
}
else
{
y--;
py = py - 2 * rxSq;
p = p + rySq + px - py;
}
drawEllipse(xc, yc, x, y);
}
p = rySq*(x+0.5)*(x+0.5) + rxSq*(y-1)*(y-1) - rxSq*rySq; //Region 2
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, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
}
OUTPUT:
Enter the center coordinates of ellipse:100
200
Enter the x-coordinate of the radius : 50
Enter the y-coordinate of the radius : 60

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

RESULT:
PROGRAM:
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
void ellipseplot(int,int,int,int);
int xcenter,yc,rx,ry;
int main()
{
int gd=DETECT,gm;
long xcenter,ycenter,rx,ry;
long rx2,ry2,tworx2,twory2,d1,d2,x,y,dx,dy;
initgraph(&gd,&gm," ");
printf("\nEnter the x-coordinate of the center point :");
scanf("%ld",&xcenter);
printf("\nEnter the y-coordinate of the center point :");
scanf("%ld",&ycenter);
printf("\nEnter the x-coordinate of the radius :");
scanf("%ld",&rx);
printf("\nEnter the y-coordinate of the radius :");
scanf("%ld",&ry);
rx2=rx*rx;
ry2=ry*ry;
twory2=2*ry2;
tworx2=2*rx2;
x=0;
y=ry;
d1=ry2-rx2*ry+(0.25*rx2);
dx=twory2*x;
dy=tworx2*y;
do
{

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

ellipseplot(xcenter,ycenter,x,y);
if(d1<0)
{
x=x+1;
y=y;
dx=dx+twory2;
d1=d1+dx+ry2;
}
else
{
x=x+1;
y=y-1;
dx=dx+twory2;
dy=dy-tworx2;
d1=d1+dx-dy+ry2;
}
}
while(dx<dy);
d2=ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2;
do
{
ellipseplot(xcenter,ycenter,x,y);
if(d2>0)
{
x=x;
y=y-1;
dy=dy-tworx2;
d2=d2-dy+rx2;
}
else
{
x=x+1;
y=y-1;
dy=dy-tworx2;
dx=dx+twory2;
d2=d2+dx-dy+rx2;
}
}
while(y>0);
getch();
closegraph();

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

return 0;
}
void ellipseplot(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter+x,ycenter+y,15);
putpixel(xcenter-x,ycenter+y,15);
putpixel(xcenter+x,ycenter-y,15);
putpixel(xcenter-x,ycenter-y,15);
}
OUTPUT:
Enter the x-coordinate of the center point : 100
Enter the y-coordinate of the center point : 200
Enter the x-coordinate of the radius : 50
Enter the y-coordinate of the radius : 60

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

RESULT:

PROGRAM:
#include<graphics.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
int main()
{
int gd=DETECT,gm;
int ch;
while(1)
{
printf("******Line Styles*******\n");
printf("\n1.Solid Line");
printf("\n2.Dotted Line");
printf("\n3.Center Line");
printf("\n4.Dashed Line");
printf("\n5.Userbit Line");
printf("\n6.Exit");
printf("\n\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
initgraph(&gd,&gm," ");
setlinestyle(0,1,3);
line(100,30,250,250);
getch();

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

cleardevice();closegraph();
break;
case 2:
initgraph(&gd,&gm," ");
setlinestyle(1,1,3);
line(100,30,250,250);
getch();
cleardevice();
closegraph();
break;
case 3:
initgraph(&gd,&gm," ");
setlinestyle(2,1,3);
line(100,30,250,250);
getch();
cleardevice();
closegraph();
break;
case 4:
initgraph(&gd,&gm," ");
setlinestyle(3,1,3);
line(100,30,250,250);
getch();
cleardevice();
closegraph();
break;
case 5:
initgraph(&gd,&gm," ");
setlinestyle(4,1,3);
line(100,30,250,250);
getch();
cleardevice();
closegraph();
break;
case 6:
exit(0);
}

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

}
return 0;
}
OUTPUT:
******Line Styles*******
1.Solid Line
2.Dotted Line
3.Center Line
4.Dashed Line
5.Userbit Line
6.Exit
Enter your Choice: 1
Solid Line

Enter your Choice: 2


Dotted Line
..

Enter your choice : 3

Enter your choice: 6

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

RESULT:

PROGRAM:
#include<graphics.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
int main()
{
int gd=DETECT,gm;
int ch;
while(1)
{
printf("******Circle Attributes*******\n");
printf("\n1.Empty Fill \n2.Soild Fill\n3.Line Fill\n4.Wide dot
Fill\n5.close dot Fill\n6.User Fill\n7.Exit");
printf("\n\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
initgraph(&gd,&gm," ");
setfillstyle(EMPTY_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

getch();
cleardevice();
closegraph();
break;
case 2:
initgraph(&gd,&gm," ");
setfillstyle(SOLID_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 3:
initgraph(&gd,&gm," ");
setfillstyle(LINE_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 4:
initgraph(&gd,&gm," ");
setfillstyle(WIDE_DOT_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();closegraph();
break;
case 5:
initgraph(&gd,&gm," ");
setfillstyle(CLOSE_DOT_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();closegraph();
break;

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

case 6:
initgraph(&gd,&gm," ");
setfillstyle(USER_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();closegraph();
break;
case 7:
exit(0);
}
}
return 0;
}

OUTPUT:

******Circle Attributes*******
1.Empty Fill
2.Soild Fill
3.Line Fill
4.Wide dot Fill
5.close dot Fill
6.User Fill
7.Exit
Enter your choice: 1

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

Enter your choice:2

Enter your choice: 3

Enter your Choice: 6


RESULT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

PROGRAM:
#include<graphics.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
int main()
{
int gd=DETECT,gm,ch;
while(1)
{
printf("******Ellipse Attributes*******\n");
printf("\n1.Empty Fill");
printf("\n2.Soild Fill");
printf("\n3.Line Fill");
printf("\n4.Wide dot Fill");
printf("\n5.close dot Fill");
printf("\n6.User Fill");
printf("\n7.Exit");
printf("\n\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
initgraph(&gd,&gm," ");
setfillstyle(EMPTY_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 2:
initgraph(&gd,&gm," ");
setfillstyle(SOLID_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

cleardevice();
closegraph();
break;
case 3:
initgraph(&gd,&gm," ");
setfillstyle(LINE_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 4:
initgraph(&gd,&gm," ");
setfillstyle(WIDE_DOT_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 5:
initgraph(&gd,&gm," ");
setfillstyle(CLOSE_DOT_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 6:
initgraph(&gd,&gm," ");
setfillstyle(USER_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();closegraph();
break;

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

case 7:
exit(0);
}
}
return 0;
}
OUTPUT:
******Ellipse Attributes*******
1.Empty Fill
2.Soild Fill
3.Line Fill
4.Wide dot Fill
5.close dot Fill
6.User Fill
7.Exit
Enter your choice: 1

Enter your choice: 2

Enter your choice : 6

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

RESULT:
PROGRAM:
#include<stdio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void tri();
int main()
{
int gd=DETECT,gm,c;
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);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
cleardevice();

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

draw();
getch();
tri();
getch();
return 0;
}
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void tri()
{
int x,y,a1,a2,a3,b1,b2,b3;
printf("Enter the Translation coordinates");
scanf("%d%d",&x,&y);
cleard evice();
a1=x1+x;
b1=y1+y;

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

a2=x2+x;
b2=y2+y;
a3=x3+x;
b3=y3+y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
}

OUTPUT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

RESULT:

PROGRAM:
#include<stdio.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,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);
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();
return 0;
}
void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3)
{

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

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);
}
OUTPUT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

RESULT:

PROGRAM:
#include<stdio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void scale();
int main()
{
int gd=DETECT,gm,c;
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);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
draw();

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

scale();
return 0;
}
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void scale()
{
int x,y,a1,a2,a3,b1,b2,b3;
int mx,my;
printf("Enter the scalling coordinates");
scanf("%d%d",&x,&y);
mx=(x1+x2+x3)/3;
my=(y1+y2+y3)/3;
cleardevice();
a1=mx+(x1-mx)*x;
b1=my+(y1-my)*y;
a2=mx+(x2-mx)*x;
b2=my+(y2-my)*y;
a3=mx+(x3-mx)*x;
b3=my+(y3-my)*y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
draw();
getch();
}
OUTPUT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

RESULT:
PROGRAM:
#include<stdio.h>
#include<graphics.h>
#define LEFT_EDGE 0x1
#define RIGHT_EDGE 0x2
#define BOTTOM_EDGE 0x4
#define TOP_EDGE 0x8
#define INSIDE(a) (!a)
#define REJECT(a,b) (a&b)

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

#define ACCEPT(a,b) (!(a|b))


#define FALSE 0
#define TRUE 1
struct point
{
int x;
int y;
}p1,p2,tmp;
struct win
{
int x;
int y;
}winmin,winmax;
unsigned char encode(struct point pt,struct win winmin,struct win winmax)
{
unsigned char code=0x00;
if(pt.x<winmin.x)
code=code|LEFT_EDGE;
if(pt.x>winmax.x)
code=code|RIGHT_EDGE;
if(pt.y<winmin.y)
code=code|BOTTOM_EDGE;
if(pt.y>winmax.y)
code=code|TOP_EDGE;
return(code);
}
void swappts(struct point *p1,struct point *p2)
{
tmp=*p1;
*p1=*p2;
*p2=tmp;
}
void swapcodes(unsigned char *c1,unsigned char *c2)
{
unsigned char tmp;
tmp=*c1;
*c1=*c2;
*c2=tmp;
}
void clipline(struct win winmin, struct win winmax,struct point p1,struct point
p2)

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

{
unsigned char code1,code2;
int done=FALSE,draw=FALSE;
float m;
while(!done)
{
code1=encode(p1,winmin,winmax);
code2=encode(p2,winmin,winmax);
if(ACCEPT(code1,code2))
{
done=TRUE;
draw=TRUE;
}
else if(REJECT(code1,code2))
done=TRUE;
else
{
if(INSIDE(code1))
{
swappts(&p1,&p2);
swapcodes(&code1,&code2);
}
if(p2.x!=p1.x)
m=(p2.y-p1.y)/(p2.x-p1.x);
if(code1 &LEFT_EDGE)
{
p1.y+=(winmin.x-p1.x)*m;
p1.x=winmin.x;
}
else if(code1 &RIGHT_EDGE)
{
p1.y+=(winmax.x-p1.x)*m;
p1.x=winmax.x;
}
else if(code1 &BOTTOM_EDGE)
{
if(p2.x!=p1.x)
p1.x+=(winmin.y-p1.y)/m;
p1.y=winmin.y;
}
else if(code1 &TOP_EDGE)

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

{
if(p2.x!=p1.x)
p1.x+=(winmax.y-p1.y)/m;
p1.y=winmax.y;
}
}
}
if(draw)
line(p1.x,p1.y,p2.x,p2.y);
}
int main()
{
int c,gm,gr;
printf("\nEnter the window minimum coordinates");
scanf("%d%d",&winmin.x,&winmin.y);
printf("\nEnter the window max coordinates");
scanf("%d%d",&winmax.x,&winmax.y);
printf("\nEnter the starting point");
scanf("%d%d",&p1.x,&p1.y);
printf("\nenter the end point");
scanf("%d%d",&p2.x,&p2.y);
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
printf("Before Clipping");
line(p1.x,p1.y,p2.x,p2.y);
rectangle(winmin.x,winmax.y,winmax.x,winmin.y);
getch();
cleardevice();
printf("After Clipping");
rectangle(winmin.x,winmax.y,winmax.x,winmin.y);
clipline(winmin,winmax,p1,p2);
getch();
return 0;
}
OUTPUT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

BEFORE CLIPPING:

AFTER CLIPPING:

RESULT:
PROGRAM:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

#include<stdio.h>
#include<graphics.h>
typedef enum { left,right,bottom,top } edge;
#define N_EDGE 4
#define TRUE 1
#define FALSE 0
struct point
{
int x,y;
}p,wmin,wmax,p1,p2,ipt,i,pin[50],pout[50],first[50],s[50],i;
int inside(struct point p,int b,struct point wmin,struct point wmax)
{
switch(b)
{
case left:
if(p.x<wmin.x)
return (FALSE);
break;
case right:
if(p.x>wmax.x)
return (FALSE);
break;
case bottom:
if(p.y<wmin.y)
return (FALSE);
break;
case top:
if(p.y>wmax.y)
return (FALSE);
break;
}
return (TRUE);
}
int cross(struct point p1,struct point p2,int b,struct point wmin,struct point
wmax)
{
if(inside(p1,b,wmin,wmax)==inside(p2,b,wmin,wmax))
return (FALSE);
else
return (TRUE);

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

}
struct point intersect(struct point p1,struct point p2,int b,struct point wmin,struct
point wmax)
{
float m;
if(p1.x!=p2.x)
m=(p1.y-p2.y)/(p1.x-p2.x);
switch(b)
{
case left:
ipt.x=wmin.x;
ipt.y=p2.y+(wmin.x-p2.x)*m;
break;
case right:
ipt.x=wmax.x;
ipt.y=p2.y+(wmax.x-p2.x)*m;
break;
case bottom:
ipt.y=wmin.y;
if(p1.x!=p2.x)
ipt.x=p2.x+(wmin.y-p2.y)/m;
else
ipt.x=p2.x;
break;
case top:
ipt.y=wmax.y;
if(p1.x!=p2.x)
ipt.x=p2.x+(wmax.y-p2.y)/m;
else
ipt.x=p2.x;
break;
}
return(ipt);
}
void clippoint(struct point p,int b,struct point wmin,struct point wmax,struct
point *pout,int *cnt,struct point *first[],struct point *s)
{
if(!first[b])
first[b]=&p;
elseif(cross(p,s[b],b,wmin,wmax))
{

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

ipt=intersect(p,s[b],b,wmin,wmax);
if(b<top)
clippoint(ipt,b+1,wmin,wmax,pout,cnt,first,s);
else
{
pout[*cnt]=ipt;
(*cnt)++;
}
}
s[b]=p;
if(inside(p,b,wmin,wmax))
if(b<top)
clippoint(p,b+1,wmin,wmax,pout,cnt,first,s);
else
{
pout[*cnt]=p;
(*cnt)++;
}
}
void closeclip(struct point wmin,struct point wmax,struct point *pout,int
*cnt,struct point *first[],struct point *s)
{
int b;
for(b=left;b<=top;b++)
{
if(cross(s[b],*first[b],b,wmin,wmax))
{
i=intersect(s[b],*first[b],b,wmin,wmax);
if(b<top)
clippoint(i,b+1,wmin,wmax,pout,cnt,first,s);
else
{
pout[*cnt]=i;
(*cnt)++;
}
}
}
int clippolygon(struct point wmin,struct point wmax,int n,struct point
*pin,struct point *pout)
{
struct point *first[N_EDGE]={0,0,0,0},s[N_EDGE];

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

int i,cnt=0;
for(i=0;i<n;i++)
clippoint(pin[i],left,wmin,wmax,pout,&cnt,first,s);
closeclip(wmin,wmax,pout,&cnt,first,s);
return(cnt);
}
int main()
{
int c,gm,gr,n,j,np;
initgraph(&gm,&gr," ");
printf("Enter the window minimum coordinates");
scanf("%d%d",&wmin.x,&wmin.y);
printf("Enter the window max coordinates");
scanf("%d%d",&wmax.x,&wmax.y);
rectangle(wmin.x,wmax.y,wmax.x,wmin.y);
printf("Enter the no of sides in polygon:\n");
scanf("%d",&n);
printf("Enter the coordinates(x,y)for pin ,pout:\n");
for(j=0;j<n;j++)
{
scanf("%d%d",&pin[j].x,&pin[j].y);
scanf("%d%d",&pout[j].x,&pout[j].y);
}
for(j=0;j<n;j++)
line(pin[j].x,pin[j].y,pout[j].x,pout[j].y);
rectangle(wmin.x,wmax.y,wmax.x,wmin.y);
rectangle(wmin.x,wmax.y,wmax.x,wmin.y);
np=clippolygon(wmin,wmax,n,pin,pout);
for(j=0;j<np;j++)
{
line(pout[j].x,pout[j].y,pout[(j+1)].x,pout[(j+1)].y);
}
getch();
return 0;
}
OUTPUT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

RESULT:

PROGRAM:
Translation:
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
int main()
{
int gd=DETECT,gm,errorcode;
int l,t,r,b,tf,x,y,z,dp;
initgraph(&gd,&gm," ");
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);
getch();
return 0;
}

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

OUTPUT:

Rotation:
#include<stdio.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);
}

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

int main()
{
int gd=DETECT,gm,x,y,z,ang,x1,x2,y1,y2,o;
initgraph(&gd,&gm," ");
setfillstyle(0,getmaxcolor());
mx=getmaxx();
my=getmaxy();
mix=mx/2;
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();
return 0;
}
OUTPUT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

Scaling:
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
int main()
{
int gd=DETECT,gm,errorcode, l,t,r,b,tf,x,y,z,dp;
initgraph(&gd,&gm," ");
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);

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

getch();
return 0;
}
OUTPUT:

RESULT:
PROGRAM:
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
class cube
{
public:
void drawcube(int x1[],int y1[])
{
int i;
for(i=0;i<4;i++)
{
if(i<3)
line(x1[i],y1[i],x1[i+1],y1[i+1]);

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

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]);
}
}
};
int 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;
int driver=DETECT;
int mode, sx,sy,sz,xf,yf,zf,x,y,z,size,t,tx,ty,tz;
initgraph(&driver,&mode,"");
printf("Enter the points on the cube:");
scanf(%d%d%d,&x,&y,&z);
printf("Enter the size of the edge:");
scanf(%d,&size);
x1[0]=x1[3]=x;
x1[1]=x1[2]=x+size;
x1[4]=x1[7]=x;
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;

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

getch();
cleardevice();
do
{
printf(MENU\n1.Translation\n2.Rotation\n3.Scaling\n4.Exit\n");
printf("Enter the choice:");
scanf(%d,&ch);
switch(ch)
{
case 1:
printf("enter the translation vector:");
scanf(%d%d%d,&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++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
c.drawcube(x2,y2);
getch();
cleardevice();
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 2:
printf("enter the rotation angle:");
scanf(%d,&theta);
theta=(theta*3.14)/180;
printf("enter the direction");
printf("1.rotation about x-axis\n);
printf("1.rotation about y-axis\n);
printf("1.rotation about z-axis\n);
scanf(%d,&op);
if(op==1)

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

{
for(i=0;i<8;i++)
{
x3[i]=x1[i];
y3[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++)
{
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
printf("enter correct option");
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
printf("before rotation");
c.drawcube(x2,y2);
getch();
cleardevice();
printf("after rotation");
c.drawcube(x4,y4);

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

getch();
cleardevice();
break;
case 3:
printf("enter the scaling factor:");
scanf(%d%d%d,&sx,&sy,&sz);
printf("enter the reference point:");
scanf(%d%d%d,&xf,&yf,&zf);
for(i=0;i<8;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();
printf("before scaling";
c.drawcube(x2,y2);
getch();
cleardevice();
printf("after scaling";
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 4:
exit(0);
break;
}
}while(op!=4);
getch();
return 0;
}
OUTPUT:

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

RESULT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

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 ();
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 ();
}

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

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);
else
glOrtho (-2.5*(GLfloat)w/(GLfloat)h,2.5*(GLfloat)w/(GLfloat)h,-2.5,
2.5,-10.10);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutCreateWindow (argv[0]);
init ();
glutReshapeFunc (reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
OUTPUT:

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

RESULT:
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;
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,

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

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)
{
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 int main()
{
Application.Run(new Form1());
}
}

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

OUTPUT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

RESULT:
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.Black;
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 = "Fern";
Text="Fractal Fern";
Size = new Size(241, 223);
WindowState = FormWindowState.Maximized;
}
public void GenerateFern(Graphics g, int iIterations, double dStartX,
double
dStartY, double dScale, double[] dA, double[] dB, double[]
dC, double[] dD, int[] iRand, Color oColor)
{
Random rnd = new Random();
int iRandNum;
double dX = 0;
double dY = 0;
double dNewX = 0;
double dNewY = 0;
for (int i = 0; i < iIterations; i++)

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

{
iRandNum = rnd.Next(0, 100);
if (iRandNum < iRand[0])
{
dNewX = dA[0];
dNewY = dA[1] * dY;
}
else if (iRandNum < iRand[1])
{
dNewX = (dB[0] * dX) + (dB[1] * dY) + dB[2];
dNewY = (dB[3] * dX) + (dB[4] * dY) + dB[5];
}
else if (iRandNum < iRand[2])
{
dNewX = (dC[0] * dX) + (dC[1] * dY) + dC[2];
dNewY = (dC[3] * dX) + (dC[4] * dY) + dC[5];
}
else
{
dNewX = (dD[0] * dX) + (dD[1] * dY) + dD[2];
dNewY = (dD[3] * dX) + (dD[4] * dY) + dD[5];
}
dX = dNewX;
dY = dNewY;
g.FillRectangle(new SolidBrush(oColor), (float)(dStartX +
(dNewX*dScale)),(float)(dStartY-(dNewY*dScale)),1,1);
}
}
public void DrawFern(int iIterations, double fScale, Color oColor)
{
double[] dA = new double[2];
double[] dB = new double[6];
double[] dC = new double[6];
double[] dD = new double[6];
int[] iRand = new int[4];
dA[0] = 0;
dA[1] = 0.16;
dB[0] = 0.2;
dB[1] = -0.26;
dB[2] = 0;
dB[3] = 0.23;

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

dB[4] = 0.22;
dB[5] = 1.6;
dC[0] = -0.15;
dC[1] = 0.28;
dC[2] = 0;
dC[3] = 0.26;
dC[4] = 0.25;
dC[5] = 0.44;
dD[0] = 0.85;
dD[1] = 0.04;
dD[2] = 0;
dD[3] = -0.04;
dD[4] = 0.85;
dD[5] = 1.6;
iRand[0] = 1;
iRand[1] = 8;
iRand[2] = 15;
iRand[3] = 85;
picFractal.Image = DrawFernImage(iIterations, fScale, oColor, dA,
dB,dC,dD, iRand, picFractal.Width, picFractal.Height);
}
public void DrawFern(int iIterations, double fScale, Color oColor,
double[] dA, double[] dB, double[] dC, double[] dD, int[] iRand)
{
picFractal.Image = DrawFernImage(iIterations, fScale, oColor, dA,
dB,dC,dD, iRand, picFractal.Width, picFractal.Height);
}
public Bitmap DrawFernImage(int iIterations, double fScale, Color
oColor, double[] dA, double[] dB, double[] dC, double[] dD, int[] iRand,
int iWidth, int iHeight)
{
Bitmap oImage = new Bitmap(iWidth,iHeight);
Graphics g = Graphics.FromImage(oImage);
GenerateFern(g, iIterations, iWidth / 2.0, (double)iHeight, fScale,
dA, dB,dC, dD, iRand, oColor);
return oImage;
}
public Image GetImage()
{
return picFractal.Image;
}

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

private void picFractal_Click(object sender, EventArgs e)


{
DrawFern(40000, 60.0, Color.Red);
}
public static int main()
{
Application.Run(new Form1());
}
}
OUTPUT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

RESULT:

PROGRAM:
#include<stdio.h>
#include<math.h>
#include<graphics.h>
struct s
{
int x,y ;
};
int main()
{
int n;
int gd= DETECT ,gm;
initgraph(&gd,&gm," ");
int xmax,ymax;
xmax=getmaxx();
ymax=getmaxy();
line(0,240,639,240);
line(320,0,320,479);
struct s inp[10],out[10];
printf ("\n enter the no of vertices");
scanf ("%d",&n);
for(int i=0;i<n;i++)
{
scanf ("%d%d",&inp[i].x,&inp[i].y);
}
for(i=0;i<n;i++)
{
inp[i].x=inp[i].x+320;
}
printf("\n enter your choice for reflection along different axes");
printf("\n 1.Along X- axis");
printf("\n 2.Along Y-axis");

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

printf("\n 3.Along X=Y axis");


printf("\n 4.Along X= -Y axis");
printf("\n 5.Along origin");
int ch;
scanf ("%d",&ch);
switch(ch)
{
case 1:
for(i=0;i<n;i++)
{
out[i].x=inp[i].x;
out[i].y=(240-(inp[i].y))+240;
}
break;
case 2:
for( i=0;i<n;i++)
{
out[i].x= 320-(inp[i].x-320);
out[i].y= inp[i].y;
}
break;
default :
printf("\n wrong choice");
}
inp[n]= inp[0];
out[n] = out[0];
for(i=0;i<n;i++)
{
setcolor(7);
line(inp[i].x,inp[i].y,inp[i+1].x,inp[i+1].y);
}
for(i=0;i<n;i++)
{
setcolor(5);
line(out[i].x,out[i].y,out[i+1].x,out[i+1].y);
}
getch();
for(i=0;i<n;i++)
{
printf("%d%d",out[i].x,out[i].y);
}

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

getch();
for(i=0;i<n;i++)
{
setcolor(5);
line(out[i].x,out[i].y,out[i+1].x,out[i+1].y);
}
getch();
closegraph();
return 0;
}
OUTPUT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

RESULT:

PROGRAM:
#include<graphics.h>
int main()
{
int gd=DETECT,gm,option,xref,yref;
int i,maxx,maxy,x1,y1,x2,y2,x3,y3,x4,y4,gap=50;

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

float shx=0.0,shy=0.0;
char str[5];
initgraph(&gd,&gm," ");
printf("enter the endpoints of the top of the rectangle (x1,y1)&(x2,y2):");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("enter the endpoints of bottom of the rectangle (x3,y3) & (x4,y4)");
scanf("%d%d%d%d",&x3,&y3,&x4,&y4);
printf(" Enter the axis to shear\n");
printf(" 1 - X axis Shear\n");
printf(" 2 - Y axis shear\n");
scanf("%d",&option );
if(option==1)
{
printf("enter the value for x-axis shear( can be fraction too):");
scanf("%f",&shx);
}
else
{
printf("enter the value for y-axis shear( can be fraction too):");
scanf("%f",&shy);
}
clearviewport();
maxx= getmaxx();
maxy=getmaxy();
line(3,maxy-1,maxx-5,maxy-1);
line(5,5,5,maxy-3);
for( i= 0;i<maxx-5;i=i+gap)
// code to display co-ordinates
{
outtextxy(i+3,maxy-7,"|");
itoa(i,str,10);
outtextxy(i,maxy-10,str);
}
for( i= maxy;i>0;i=i-gap)
{
outtextxy(3,i,"-");
itoa(maxy-i,str,10);
outtextxy(9,i,str);
}
setcolor(50);//drawing rectangle using endpoints
line(x1,maxy-y1,x2,maxy-y2);
line(x3,maxy-y3,x4,maxy-y4);

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

line(x1,maxy-y1,x3,maxy-y3);
line(x2,maxy-y2,x4,maxy-y4);
outtextxy(10,10,"hit any key to see the shearing effect" );
getch();
setcolor(0); // to hide the rectangle drawn
line(x1,maxy-y1,x2,maxy-y2);
line(x3,maxy-y3,x4,maxy-y4);
line(x1,maxy-y1,x3,maxy-y3);
line(x2,maxy-y2,x4,maxy-y4);
setcolor(58); // to redraw the rectangle
if(option==1)
{
// shearing about x axis so only points x1 and x2 need to be
recomputed
line(x1+shx*y1,maxy-y1,x2+shx*y2,maxy-y2);
line(x3,maxy-y3,x4,maxy-y4);
line(x1+shx*y1,maxy-y1,x3,maxy-y3);
line(x2+shx*y2,maxy-y2,x4,maxy-y4);
}
else
{
line(x1,maxy-y1,x2,maxy-(y2+shy*x2));
line(x3,maxy-y3,x4,maxy-(y4+shy*x4));
line(x1,maxy-y1,x3,maxy-y3);
line(x2,maxy-(y2+shy*x2),x4,maxy-(y4+shy*x4));
}
getch();
closegraph() ;
return 0;
}
OUTPUT:
Input
x1 ,y1, x2, y2 : 200 200 300 200
x3, y3, x4, y4 : 200 100 300 100
x- shear value: any value even fractional ( 0.4)

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

y- shear value: any value even fractional ( 0.5)

Original Image

RESULT:

PROGRAM:

Shearing w.r.t Y-axis

Shearing w.r.t X - axis

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

#include<stdio.h>
#include<graphics.h>
#include<math.h>
int main()
{
float sx,sy;int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
printf("Enter the Coordinates\nx1,y1,x2,y2,x3,y3\n");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
cleardevice();
w1=5;
w2=5;
w3=635;
w4=465;
rectangle(w1,w2,w3,w4);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
v1=425;
v2=75;
v3=550;
v4=250;
sx=(float)(v3-v1)/(w3-w1);
sy=(float)(v4-v2)/(w4-w2);
rectangle(v1,v2,v3,v4);
x1=v1+floor(((float)(x1-w1)*sx)+.5);
x2=v1+floor(((float)(x2-w1)*sx)+.5);
x3=v1+floor(((float)(x3-w1)*sx)+.5);
y1=v2+floor(((float)(y1-w2)*sy)+.5);
y2=v2+floor(((float)(y2-w2)*sy)+.5);
y3=v2+floor(((float)(y3-w2)*sy)+.5);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
return 0;
}
OUTPUT:

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

Enter the Coordinates


x1,y1,x2,y2,x3,y3
100
200
300
400
500
350

RESULT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

PROGRAM:
#include<stdio.h>
#include<graphics.h>
int ch,x,y,az,i,w,ch1,ch2,xa,ya,ra,a[10],b[10],da,db;
float x1,y1,az1,w1,dx,dy,theta,x1s,y1s,sx,sy,a1[10],b1[10];
int main()
{
int gm,gr;
initgraph(&gm,&gd," ");
printf("Enter the upper left corner of the rectangle:\n");
scanf("%d%d",&x,&y);
printf("Enter the lower right corner of the rectangle:\n");
scanf("%d%d",&az,&w);
rectangle(x,y,az,w);
da=az-x;
db=w-y;
a[0]=x;
b[0]=y;
a[1]=x+da;
b[1]=y;
a[2]=x+da;
b[2]=y+db;
a[3]=x;b[3]=y+db;
while(1)
{
printf(MENU\n1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n
5.Shearing\n6.Exit\nEnter your choice:);
scanf("%d",&ch);
switch(ch)
{
case 1:
rectangle(x,y,az,w);
printf("Enter the value of shift vector:\n");
scanf("%f%f",&dx,&dy);
x1=x+dx;
y1=y+dy;
az1=az+dx;
w1=w+dy;
rectangle(x1,y1,az1,w1);
break;

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

case 2:
rectangle(x,y,az,w);
printf("Enter the value of fixed point and angle of rotation:\n");
scanf("%d%d%d",&xa,&ya,&ra);
theta=(float)(ra*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((a[i]-xa)*cos(theta)-(b[i]-ya)*sin(theta)));
b1[i]=(ya+((a[i]-xa)*sin(theta)+(b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
case 3:
rectangle(x,y,az,w);
printf("Enter the value of scaling factor:\n");
scanf("%f%f",&sx,&sy);
x1=x*sx;
y1=y*sy;
az1=az*sx;
w1=w*sy;
rectangle(x1,y1,az1,w1);
break;
case 4:
rectangle(x,y,az,w);
printf("Enter the fixed point\n");
scanf("%d%d",&xa,&ya);
theta=(float)(90*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((a[i]-xa)*cos(theta)-(-b[i]-ya)*sin(theta)));
b1[i]=(ya+((a[i]-xa)*sin(theta)+(-b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)

RAJALAKSHMI INSTITUTE OF TECHNOLOGY

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

201103002

line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
case 5:
rectangle(x,y,az,w);
printf("1.X-axis shear\n2.Y-axis shear\nEnter your choice:\n);
scanf("%d",&ch2);
switch(ch2)
{
case 1:
printf("Enter the value of shear:\n");
scanf("%f",&x1s);
x1=x+(y*x1s);
y1=y;
az1=az+(w*x1s);
w1=w;
rectangle(x1,y1,az1,w1);
break;
case 2:
printf("Enter the value of shear:\n");
scanf("%f",&y1s);
x1=x;
y1=y+(x*y1s);
az1=az;
w1=w+(az*y1s);
rectangle(x1,y1,az1,w1);
break;
}
break;
case 6:
exit(0);
}
}
getch();
return 0;
}
OUTPUT:

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

Enter the upper left corner of the rectangle:


20
100
Enter the lower right corner of the rectangle:
100
20
MENU
1.Translation
2.Rotation
3.Scaling
4.Reflection
5.Shearing
6.Exit
Enter your choice:1
Enter the value of shift vector:
-50
-140

Enter your choice:2


Enter the value of fixed point and angle of rotation:
-50
-50
60

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

Enter your choice:3


Enter the scaling factor:
0.5
0.5

Enter your choice:4


Enter the fixed point:
1
-30

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

RAJALAKSHMI INSTITUTE OF TECHNOLOGY


201103002

Enter your choice:5


1.X-axis shear
2.Y-axis shear
Enter your choice:1
Enter the value of shear:
1.5

Enter your choice:6


RESULT:

Kuthambakkam
Chennai-600 124
Tel: 3718 1600

Vous aimerez peut-être aussi