CGR

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

1.

draw lines and all


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
putpixel(40,40,WHITE);
line(50,50,200,50);
circle(135,285,50);
rectangle(125,115,215,165);
ellipse(130,380,0,360,30,20);
getch();
closegraph();
}

2.DDA algo to draw line


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
int x=0,y=0,x1,y1,x2,y2,len,i=0;
float dx,dy;
int gd=DETECT,gm;
clrscr();
printf("\n Enter value for starting point of line: ");
scanf("%d%d",&x1,&y1);
printf("\n Enter value for ending point of line: ");
scanf("%d%d",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>dy)
len=dx;
else
len=dy;
dx=(x2-x1)/len;
dy=(y2-y1)/len;
x=x1+0.5;
y=y1+0.5;
initgraph(&gd,&gm,"..\\bgi");
while (i<=len)
{
putpixel(x,y,7);
x=x+dx;
y=y+dy;
i=i+1;
delay(30);
}
getch();
closegraph();
}

3.bresennham's algo draw line


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
int x,y,x1,y1,x2,y2,p,i;
int dx,dy;
int gd=DETECT,gm;
clrscr();
printf("\n Enter value for starting point of line: ");
scanf("%d%d",&x1,&y1);
printf("\n Enter value for ending point of line: ");
scanf("%d%d",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
x=x1;
y=y1;
i=0;
p=2*(dy-dx);
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
while(i<=dx)
{
putpixel(x,y,7);
if(p<=0)
{
x=x+1;
p=p+2*dy;
}
else
{
x=x+1;
y=y+1;
p=p+2*dy-2*dx;
}
i++;
delay(25);
}
getch();
closegraph(); }

4.bresennhams draw a cirle


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm;
int x,y,xc,yc,r,d;
clrscr();
printf("\n Enter the center of the circle: ");
scanf("%d %d",&xc,&yc);
printf("\n Enter the radius of the circle: ");
scanf("%d",&r);
initgraph(&gd,&gm,"c://turboc3//bgi");
d=3-2*r;
x=0;
y=r;
while(x<=y)
{
putpixel(x+xc,y+yc,WHITE);
putpixel(x+xc,-y+yc,WHITE);
putpixel(-x+xc,-y+yc,WHITE);
putpixel(-x+xc,y+yc,WHITE);
putpixel(y+xc,x+yc,WHITE);
putpixel(y+xc,-x+yc,WHITE);
putpixel(-y+xc,-x+yc,WHITE);
putpixel(-y+xc,x+yc,WHITE);
if(d<=0)
{
x=x+1;
d=d+4*x+6;
}
else
{
x=x+1;
y=y-1;
d=d+4*(x-y)+10;
}
}
getch();
closegraph();
return 0;
}

5.fill polygon using flood fill methhod


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void floodfill4(int x,int y,int newcolor,int oldcolor)
{
if(getpixel(x,y)==oldcolor)
{
delay(5);
putpixel(x,y,newcolor);
floodfill4(x+1,y,newcolor,oldcolor);
floodfill4(x-1,y,newcolor,oldcolor);
floodfill4(x,y+1,newcolor,oldcolor);
floodfill4(x,y-1,newcolor,oldcolor);
}
}
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setcolor(6);
line(210,210,260,210);
setcolor(5);
line(260,210,260,260);
setcolor(8);
line(210,260,260,260);
setcolor(9);
line(210,210,210,260);
floodfill4(220,230,7,0);
getch();
closegraph();
return 0;
}
6.fill polygon using boundry fill method
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
void bfill(int x, int y, int c, int b)
{
if(getpixel(x,y)!=c && getpixel(x,y)!=b)
{
putpixel(x,y,c);
bfill(x+1,y,c,b);
bfill(x-1,y,c,b);
bfill(x,y+1,c,b);
bfill(x,y-1,c,b);
}
}
main()
{
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(100,100,150,150);
bfill(125,125,5,15);
getch();
closegraph();
return 0;
}

7.two diemsionL transformation


A. Translation
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2;
int tx,ty;clrscr();
printf("\n Enter the x,y coordinates of 2 line end points: ");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n Original line before translation: ");
line(x1,y1,x2,y2);
printf("\n Enter the translation factor: ");
scanf("%d%d",&tx,&ty);
x1=x1+tx;
y1=y1+ty;
x2=x2+tx;
y2=y2+ty;
printf("\n Line after translation");
setcolor(6);
line(x1,y1,x2,y2);
getch();
closegraph();
}

B.scaling
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2;
int sx,sy;clrscr();
printf("\n Enter the x,y coordinates of 2 line end points: ");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n Original line before scaling: ");
line(x1,y1,x2,y2);
printf("\n Enter the scaling factor: ");
scanf("%d%d",&sx,&sy);
x1=x1*sx;
y1=y1*sy;
x2=x2*sx;
y2=y2*sy;
printf("\n Line after scaling");
setcolor(RED);
line(x1,y1,x2,y2);
getch();
closegraph();
}

8.two dimensional Rotation


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,xn,yn;
double r11,r12,r21,r22,th;
printf("\n Enter the x,y coordinates of 2 lines end points: ");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n Original line before rotation: ");
line(x1,y1,x2,y2);
printf("\n Enter the angle of rotation: ");
scanf("%lf",&th);
r11=cos((th*3.1428)/180);
r12=sin((th*3.1428)/180);
r21=(-sin(th*3.1428)/180);
r22=cos((th*3.1428)/180);
xn=((x2*r11)-(y2*r12));
yn=((x2*r21)+(y2*r22));
printf("\n Line after rotation");
setcolor(6);
line(x1,y1,xn,yn);
getch();
closegraph();
}

9.two dimensional transformation


A.x sher
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,sh_x;
clrscr();
printf("\nEnter the coordinates of rectangle:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&gd,&gm,"C://turboc3//bgi");
rectangle(x1,y1,x2,y2);
printf("\nEnter the x shear factor:");
scanf("%d",&sh_x);
x1=x1+sh_x*y1;
y1=y1;
x2=x2+sh_x*y2;
y2=y2;
printf("\nRctangle after shering:");
rectangle(x1,y1,x2,y2);
getch();
closegraph();
}

B.y shear
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,sh_y;
clrscr();
printf("\nEnter the coordinates of rectangle:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&gd,&gm,"C://turboc3//bgi");
rectangle(x1,y1,x2,y2);
printf("\nEnter the x shear factor:");
scanf("%d",&sh_y);
y1=y1+sh_y*x1;
x1=x1;
y2=y2+sh_y*x2;
x2=x2;
printf("\nRctangle after shering:");
rectangle(x1,y1,x2,y2);
getch();
closegraph();
}

11.three dimesional rotation


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void rotate();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"..s\\bgi");
printf("\n3D Transformation Rotating\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice();
rotate();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void rotate()
{
float t;
int a1,b1,a2,b2,dep;
printf("Enter the angle to rotate=");
scanf("%f",&t);
t=t*(3.14/180);
a1=mx+(x1-mx)*cos(t)-(y1-my)*sin(t);
a2=mx+(x2-mx)*cos(t)-(y2-my)*sin(t);
b1=my+(x1-mx)*sin(t)-(y1-my)*cos(t);
b2=my+(x2-mx)*sin(t)-(y2-my)*cos(t);
if(a2>a1)
dep=(a2-a1)/4;
else
dep=(a1-a2)/4;
bar3d(a1,b1,a2,b2,dep,1);
setcolor(5);
//draw();
}

16.bezier curve
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#include<graphics.h>
#include<math.h>
#include<dos.h>
void bezier (int x[4], int y[4])
{
int gd=DETECT,gm;
int i;
double t;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
for (t=0.0;t<1.0;t+=0.0005)
{
double xt=pow(1-t,3)*x[0]+3*t*pow(1-t,2)*x[1]+3*pow(t,2)*(1-t)*x[2]+pow(t,3)*x[3];
double yt=pow(1-t,3)*y[0]+3*t*pow(1-t,2)*y[1]+3*pow(t,2)*(1-t)*y[2]+pow(t,3)*y[3];
putpixel(xt,yt,WHITE);
delay(10);
}
for(i=0;i<4;i++)
putpixel(x[i],y[i],YELLOW);
getch();
closegraph();
return;
}
void main()
{
int x[4],y[4];
int i;
printf("Enter the x- and y-coordinates of the four control points.\n");
for(i=0;i<4;i++)
scanf("%d%d",&x[i],&y[i]);
bezier(x,y);
}

You might also like