Monday, March 14, 2016

BUBBLE SORTING

/*BUBBLE SORTING WITH GRAPHICAL REPRESENTATION*/


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void object(int,int,int);
void bubble(int);
void flow(int,int);
void mixing(int,int);
int a[10];
void main()
{
 int n,i,gd=0,gm=0;
 clrscr();
 printf("Enter the No of Elements : " );
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
  printf("Enter the %d Element : ",i+1);
  scanf("%d",&a[i]);
 }
 initgraph(&gd,&gm,"c://tc//bgi");
 settextstyle(3,0,1);
 outtextxy(250,50,"GIVEN NUMBER");
 for(i=0;i<n;i++)
 {
  object(100+i*50,150,a[i]);
  getch();
 }
 bubble(n);
/* for(i=0;i<n;i++)
 {
  object(100+i*50,350,a[i]);
  getch();
 }*/delay(500);
 settextstyle(3,0,2);
 outtextxy(400,400,"prepared by rishabh");
 getch();
}
void bubble(int n)
{
 int i,j,temp;

 for(i=0;i<n;i++)
  for(j=i;j<n-1;j++)
   if(a[i]>a[j+1])
   {
    flow(i,j+1);
    temp=a[i];
    a[i]=a[j+1];
    a[j+1]=temp;
   }
}
void object(int x,int y,int no)
{
 char s[8];
 sprintf(s,"%d",no);
 circle(x,y,15);
 settextstyle(2,0,6);
 outtextxy(x-3,y-10,s);
}
void flow(int f,int s)
{
 int i;
 for(i=0;i<50;i++)
 {
  setcolor(WHITE);
  object(100+f*50,150+i*4,a[f]);
  delay(15);
  setcolor(0);
  object(100+f*50,150+i*4,a[f]);
 }
 setcolor(WHITE);
 object(100+f*50,150+i*4,a[f]);
 for(i=0;i<50;i++)
 {
  setcolor(WHITE);
  object(100+s*50,150+i*4,a[s]);
  delay(10);
  setcolor(0);
  object(100+s*50,150+i*4,a[s]);
 }
 setcolor(WHITE);
 object(100+s*50,150+i*4,a[s]);
 mixing(f,s);
 for(i=50;i>0;i--)
 {
  setcolor(WHITE);
  object(100+f*50,150+i*4,a[s]);
  delay(10);
  setcolor(0);
  object(100+f*50,150+i*4,a[s]);
 }
 setcolor(WHITE);
 object(100+f*50,150+i*4,a[s]);
 for(i=50;i>0;i--)
 {
  setcolor(WHITE);
  object(100+s*50,150+i*4,a[f]);
  delay(15);
  setcolor(0);
  object(100+s*50,150+i*4,a[f]);
 }
 setcolor(WHITE);
 object(100+s*50,150+i*4,a[f]);
}
void mixing(int f,int s)
{
 int i;
 for(i=0;i<(s-f)*50;i++)
 {
  setcolor(WHITE);
  object(100+f*50+i,350,a[f]);
  object(100+s*50-i,350,a[s]);
  delay(20);
  setcolor(0);
  object(100+f*50+i,350,a[f]);
  object(100+s*50-i,350,a[s]);
  }
  setcolor(WHITE);
  object(100+f*50+i,350,a[f]);
  object(100+s*50-i,350,a[s]);

}




Sunday, March 13, 2016

Cohen sutherland polygon clipping in C


/*Cohen sutherland polygon clipping in c*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void leftclip();
void rightclip();
void topclip();
void bottomclip();
int gd=DETECT,gm,Xmin,Ymin,Xmax,Ymax,x1,y1;
void main()
{
int n,i=1,x[10],y[10];
initgraph(&gd,&gm,"c://tc//bgi");
printf("enter the Xmin,Ymin,Xmax,Ymax co ordinate of window port");
scanf("\n%d\t%d\t%d\t%d",&Xmin,&Ymin,&Xmax,&Ymax);
printf("enter the no. of side of polygon u want to enter");
scanf("\n%d",&n);
for(i=1;i<=n;i++)
{
printf("the co ordinates of sides are x[%d] y[%d]",i,i);
scanf("\n%d\t%d",&x[i],&y[i]);
}
cleardevice();
rectangle(Xmin,Ymin,Xmax,Ymax);
getch();
for(i=1;i<n;i++)
{ setcolor(i);
line(x[i],y[i],x[i+1],y[i+1]);
}  setcolor(2);
line(x[1],y[1],x[n],y[n]);
getch();
leftclip();
rightclip();
topclip();
bottomclip();
getch();
}
void leftclip()
{
for(y1=0;y1<480;y1++)
{
for(x1=0;x1<Xmin;x1++)
{
putpixel(x1,y1,BLACK);
}}}
void rightclip()
{
for(y1=0;y1<480;y1++)
{
for(x1=Xmax+1;x1<640;x1++)
{
putpixel(x1,y1,BLACK);
}}}
void topclip()
{
for(x1=0;x1<640;x1++)
{
for(y1=0;y1<Ymin;y1++)
{
putpixel(x1,y1,BLACK);
}} }
void bottomclip()
{
for(x1=0;x1<640;x1++)
{
for(y1=Ymax+1;y1<480;y1++)
{
putpixel(x1,y1,BLACK);
}}}




Bresenham circle drawing code in c

 /*This is the program for bresenham circle drawing  in C */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gdriver = DETECT,gmode,x,y,r,x1,y1;
float e;
initgraph(&gdriver,&gmode,"C:\\tc\\BGI");
printf("center co-ordinate of x and y position of circle:\n");
scanf("%d %d",&x,&y);
printf("input the radius of circle:\n");
scanf("%d",&r);
x1=1;
y1=r;
putpixel(x+0,y+r,RED);
putpixel(x+r,y+0,RED);
putpixel(x-0,y-r,RED);
putpixel(x-r,y-0,RED);

e=3-2*r;  
while(x1<y1)
{
if(e<0)
{
x1=x1+1;
e=e+4*x1+6;

}
else
{
x1=x1+1;
y1=y1-1;
e=e+4*(x1-y1)+10;
}          
putpixel(x+x1,y+y1,1);
putpixel(x+y1,y+x1,2);
putpixel(x-x1,y+y1,3);
putpixel(x-y1,y+x1,4);
putpixel(x-x1,y-y1,5);
putpixel(x-y1,y-x1,6);
putpixel(x+x1,y-y1,7);
putpixel(x+y1,y-x1,8);
delay(100);
}
getch();
}


/*NOTE:-You have to change the graphics driver location according to your compiler graphics file address which has been written in initgraph in quotation. */