Showing posts with label Algorithm. Show all posts
Showing posts with label Algorithm. Show all posts

Sunday, 19 May 2013

35. LIANG BARSKY LINE CLIPPING ALGORITHM


Code:
#include<iostream.h>
# include<conio.h>
# include<graphics.h>
void main() {
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
int x1,y1,x2,y2,xmax,xmin,ymax,ymin,xx1,yy1,xx2,yy2,dx,dy,i;
int p[4],q[4];
float t1,t2,t[4];
cout<<"Enter the lower co-ordinates of window";
cin>>xmin>>ymin;
cout<<"Enter the upper co-ordinates of window";
cin>>xmax>>ymax;
setcolor(RED);
rectangle(xmin,ymin,xmax,ymax);
cout<<"Enter x1:";
cin>>x1;
cout<<"Enter y1:";
cin>>y1;
cout<<"Enter x2:";
cin>>x2;
cout<<"Enter y2:";
cin>>y2;
line(x1,y1,x2,y2);
dx=x2-x1;
dy=y2-y1;
p[0]=-dx;
p[1]=dx;
p[2]=-dy;
p[3]=dy;
q[0]=x1-xmin;
q[1]=xmax-x1;
q[2]=y1-ymin;
q[3]=ymax-y1;
for(i=0;i<4;i++)
{ if(p[i]!=0)
{t[i]=(float)q[i]/p[i];}
else
if(p[i]==0 && q[i]<0)
cout<<"line completely outside the window";
else
if(p[i]==0 && q[i]>=0)
cout<<"line completely inside the window";
}
if (t[0]>t[2])
{t1=t[0];}
else
{t1=t[2];}
if (t[1]<t[3])
{t2=t[1];}
else
{t2=t[3];}
if (t1<t2)
{xx1=x1+t1*dx;
xx2=x1+t2*dx;
yy1=y1+t1*dy;
yy2=y1+t2*dy;
cout<<"line after clipping:";
setcolor(WHITE);
line(xx1,yy1,xx2,yy2);}
else
{cout<<"line lies out of the window";}
getch();
}

OUTPUT:
























Need the code??




34. COHEN SUTHERLAND LINE CLIPPING ALGORITHM


Code:
importjava.applet.*;
importjava.awt.*;
importjava.util.*;
public class CohenSutherland extends Applet
{
            intxmax=90,ymax=80,xmin=40,ymin=40;
            publicint[] set(intx,int y)
            {
                        int a[]=new int[4];
                        if(x<xmin)
                                    a[3]=1;
                        else
                                    a[3]=0;
                        if(x>xmax)
                                    a[2]=1;
                        else
                                    a[2]=0;
                        if(y<ymin)
                                    a[0]=1;
                        else
                                    a[0]=0;
                        if(y>ymax)
                                    a[1]=1;
                        else
                                    a[1]=0;
                        return a;
            }
            boolean check(int a[])
            {
                        for(int i=0;i<a.length;i++)
                                    if(a[i]==1)
                                                return false;
                        return true;
            }
            int[] produceXY(inti,int x1,int y1,float m)
            {
                        int a[]=new int [2];
                        float x=0,y=0;
                        switch(i)
                        {
                                    case 0:
                                                x=xmin;
                                                y=y1+m*(x-x1);
                                                break;
                                   
case 1:
                                                x=xmax;          
                                                y=y1+m*(x-x1);
                                                break;
                                    case 3:
                                                y=ymin;
                                                x=x1+(y-y1)/m;
                                                break;
                                    case 2:
                                                y=ymax;
                                                x=x1+(y-y1)/m;
                                                break;
                        }
                        a[0]=(int)x;
                        a[1]=(int)y;
                        return a;
            }
            booleandoAnd(int a[],int b[])
            {
                        for(int i=0;i<a.length;i++)
                        {
                                    int k=a[i]&b[i];
                                    if(k==1)
                                                return false;
                        }
                        return true;
            }
            public void paint(Graphics g)
            {
                        g.drawRect(xmin,ymin,xmax-xmin,ymax-ymin);
                        g.drawRect(xmin+100,ymin,xmax-xmin,ymax-ymin);
                        int a[][]=new int[2][4];
                        int b[][]=new int[2][4];
                        int c[]=new int[2];
                        int c1=20;
                        int x1=45,y1=45,x2=20,y2=90;
                        float m=(y2-y1)*1.0f/(x2-x1);
                        g.drawString(m+" ",100,100);
                        g.drawLine(x1,y1,x2,y2);
                        a[0]=set(x1,y1);
                        a[1]=set(x2,y2);
                        g.drawString(Arrays.toString(a[0]),300,300);
                        g.drawString(Arrays.toString(a[1]),400,400);
                        if(check(a[0])&&check(a[1]))
                        {
                                    g.drawLine(x1,y1,x2,y2);
                        }
                        else
                        {
                                    if(doAnd())
                                    {
                                                for(int i=a[0].length-1;i>=0;i--)
                                                {
                                                            if(a[0][i]==1)
                                                            {
                                                                        c=produceXY(a[0].length-1-i,x1,y1,m);
                                                                        b[0]=set(c[0],c[1]);
                                                            g.drawString("first "+Arrays.toString(b[0]),200,300+c1);
                                                                        if(check(b[0]))
                                                                        {
                                                                                    x1=c[0];
                                                                                    y1=c[1];
                                                                                    break;
                                                                        }
                                                                        c1+=20;
                                                            }
                                                }
                                                for(int i=a[0].length-1;i>=0;i--)
                                                {
                                                            if(a[1][i]==1)
                                                            {
                                                                        c=produceXY(a[0].length-1-i,x1,y1,m);
                                                                        b[1]=set(c[0],c[1]);
                                                g.drawString("second "+Arrays.toString(b[1]),200,300+c1);
                                                                        if(check(b[1]))
                                                                        {
                                                                                    x2=c[0];
                                                                                    y2=c[1];
                                                                                    break;
                                                                        }
                                                                        //c1+=20;
                                                            }
                                                }
                                                g.drawLine(x1+100,y1,x2+100,y2);
                                    }
                        }
            }
}
/*<applet code="CohenSutherland.class" width=500 height=500>
</applet>*/


OUTPUT
















Need the code??





Saturday, 18 May 2013

26. BRESENHAM LINE ALGORITHM



Code:
import java.io.*;
import java.applet.*;
import java.util.*;
import java.awt.*;
public class Bre1 extends Applet
{
public void paint(Graphics g)
{
int x,y,k;
double dx,dy,p;
int x1=200,y1=300,x2=600,y2=400;
dx=Math.abs(x2-x1);
dy=Math.abs(y2-y1);
x=x1;
y=y1;
p=2*dy-dx;
g.fillOval(200,300,5,5);
for(k=0;k<dx;k++)
{
if(p<0)
{
g.fillOval(x++,y,5,5);
p=p+(2*dy);
}
else
{
g.fillOval(x++,y++,5,5);
p=p+(2*(dy-dx));
}
}
}
}
/*<applet code="Bre1.class" width="800" height="800">
 </applet>*/

OUTPUT:


  















Need the code??



25. DDA LINE ALGORITHM


Code:
import java.io.*;
import java.awt.*;
import java.applet.*;
import java.util.*;
public class DDA extends Applet
{
public void paint(Graphics g)
{
double dx,dy,steps,x,y,k;
double xc,yc;
double x1=200,y1=500,x2=600,y2=200;
dx=x2-x1;
dy=y2-y1;
if(Math.abs(dx)>Math.abs(dy))
steps=Math.abs(dx);
else
steps=Math.abs(dy);
xc=(dx/steps);
yc=(dy/steps);
x=x1;
y=y1;
g.fillOval(200,500,5,5);
for(k=1;k<=steps;k++)
{
x=x+xc;
y=y+yc;
g.fillOval((int)x,(int)y,5,5);
}
}
}
/*<applet code="DDA.class" width="800" height="800">
 </applet>*/

OUTPUT:
















Need the code??











Wednesday, 15 May 2013

24. TRAVELLING SALES PERSON PROBLEM (TSP)


Code:
import java.util.*;
import java.text.*;
class TSP
{
int weight[][],n,tour[],finalCost;
final int INF=1000;
public TSP()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter no. of nodes:=>");
n=s.nextInt();
weight=new int[n][n];
tour=new int[n-1];
for(int i=0;i<n;i++)

{
for(int j=0;j<n;j++)
{
if(i!=j)
{
System.out.print("Enter weight of "+(i+1)+" to "+(j+1)+":=>");
weight[i][j]=s.nextInt();
}
}
}
System.out.println();
System.out.println("Starting node assumed to be node 1.");
eval();
}
public int COST(int currentNode,int inputSet[],int setSize)
{
if(setSize==0)
return weight[currentNode][0];
int min=INF,minindex=0;
int setToBePassedOnToNextCallOfCOST[]=new int[n-1];
for(int i=0;i<setSize;i++)
{
int k=0;//initialise new set
for(int j=0;j<setSize;j++)
{
if(inputSet[i]!=inputSet[j])
setToBePassedOnToNextCallOfCOST[k++]=inputSet[j];
}
int temp=COST(inputSet[i],setToBePassedOnToNextCallOfCOST,setSize-1);
if((weight[currentNode][inputSet[i]]+temp) < min)
{
min=weight[currentNode][inputSet[i]]+temp;
minindex=inputSet[i];
}
}
return min;
}
public int MIN(int currentNode,int inputSet[],int setSize)
{
if(setSize==0)
return weight[currentNode][0];
int min=INF,minindex=0;
int setToBePassedOnToNextCallOfCOST[]=new int[n-1];
for(int i=0;i<setSize;i++)//considers each node of inputSet
{
int k=0;
for(int j=0;j<setSize;j++)
{
if(inputSet[i]!=inputSet[j])
setToBePassedOnToNextCallOfCOST[k++]=inputSet[j];
}
int temp=COST(inputSet[i],setToBePassedOnToNextCallOfCOST,setSize-1);
if((weight[currentNode][inputSet[i]]+temp) < min)
{
min=weight[currentNode][inputSet[i]]+temp;
minindex=inputSet[i];
}
}
return minindex;
}
public void eval()
{
int dummySet[]=new int[n-1];
for(int i=1;i<n;i++)
dummySet[i-1]=i;
finalCost=COST(0,dummySet,n-1);
constructTour();
}
public void constructTour()
{
int previousSet[]=new int[n-1];
int nextSet[]=new int[n-2]; for(int i=1;i<n;i++)
previousSet[i-1]=i;
int setSize=n-1;
tour[0]=MIN(0,previousSet,setSize);
for(int i=1;i<n-1;i++)
{
int k=0;
for(int j=0;j<setSize;j++)
{
if(tour[i-1]!=previousSet[j])
nextSet[k++]=previousSet[j];
}
--setSize;
tour[i]=MIN(tour[i-1],nextSet,setSize);
for(int j=0;j<setSize;j++)
previousSet[j]=nextSet[j];
}
display();
}
public void display()
{
System.out.println();
System.out.print("The tour is 1-");
for(int i=0;i<n-1;i++)
System.out.print((tour[i]+1)+"-");
System.out.print("1");
System.out.println();
System.out.println("The final cost is "+finalCost);
}
}
class TSPExp
{
public static void main(String args[])
{
TSP obj=new TSP();
}
}


OUTPUT :
Enter no. of nodes:=>
5
Enter weight of 1 to 2:=>4
Enter weight of 1 to 3:=>6
Enter weight of 1 to 4:=>3
Enter weight of 1 to 5:=>7
Enter weight of 2 to 1:=>3
Enter weight of 2 to 3:=>1
Enter weight of 2 to 4:=>7
Enter weight of 2 to 5:=>4
Enter weight of 3 to 1:=>7
Enter weight of 3 to 2:=>4
Enter weight of 3 to 4:=>3
Enter weight of 3 to 5:=>6
Enter weight of 4 to 1:=>8
Enter weight of 4 to 2:=>5
Enter weight of 4 to 3:=>3
Enter weight of 4 to 5:=>2
Enter weight of 5 to 1:=>4
Enter weight of 5 to 2:=>3
Enter weight of 5 to 3:=>2
Enter weight of 5 to 4:=>1


Starting node assumed to be node 1.
The tour is 1-2-3-4-5-1
The final cost is 14