Program to implement Apriori Algorithm


import java.io.*;
class apriori
{ 
public static void main(String []arg)throws IOException
{
int i,j,m=0;
int t1=0;
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of transaction :");
int n=Integer.parseInt(b.readLine());
System.out.println("items :1--Milk 2--Bread 3--Coffee 4--Juice  5--Cookies  6--Jam");
int item[][]=new int[n][6];
for(i=0;i<n;i++)
 for(j=0;j<6;j++)
   item[i][j]=0;
String[] itemlist={"MILK","BREAD","COFFEE","JUICE","COOKIES","JAM"};
int nt[]=new int[6];
int q[]=new int[6];
for(i=0;i<n;i++)
{ System.out.println("Transaction "+(i+1)+" :");
  for(j=0;j<6;j++)
  {  //System.out.println(itemlist[j]);
     System.out.println("Is Item "+itemlist[j]+" present in this transaction(1/0)? :");
     item[i][j]=Integer.parseInt(b.readLine()); 
  }
}
 for(j=0;j<6;j++) 
  { for(i=0;i<n;i++)
    {if(item[i][j]==1)
      nt[j]=nt[j]+1;
    }
    System.out.println("Number of Item "+itemlist[j]+" :"+nt[j]);
  }

for(j=0;j<6;j++)
{ if(((nt[j]/(float)n)*100)>=50)
    q[j]=1;
  else
    q[j]=0;

  if(q[j]==1)
   {t1++;
    System.out.println("Item "+itemlist[j]+" is selected "); 
   
   }
}
 for(j=0;j<6;j++) 
  { for(i=0;i<n;i++)
   {
     
     if(q[j]==0)
       { 
        item[i][j]=0;
       }
   }
   }

int nt1[][]=new int[6][6];
 for(j=0;j<6;j++) 
    {  for(m=j+1;m<6;m++) 
       { for(i=0;i<n;i++)
         { if(item[i][j]==1 &&item[i][m]==1)
           { nt1[j][m]=nt1[j][m]+1;
           }
         }
    if(nt1[j][m]!=0)
         System.out.println("Number of Items of  "+itemlist[j]+"& "+itemlist[m]+" :"+nt1[j][m]);
    }
  
   }
for(j=0;j<6;j++)
{ for(m=j+1;m<6;m++) 
  {
  if(((nt1[j][m]/(float)n)*100)>=50)
    q[j]=1;
  else
    q[j]=0;

  if(q[j]==1)
   {
    System.out.println("Item "+itemlist[j]+"& "+itemlist[m]+" is selected "); 
   
   }
}
}
} 
}

/*OUTPUT

Z:\>javac apriori.java

Z:\>java apriori
Enter the number of transaction :
4
items :1--Milk 2--Bread 3--Coffee 4--Juice  5--Cookies  6--Jam
Transaction 1 :
Is Item MILK present in this transaction(1/0)? :
1
Is Item BREAD present in this transaction(1/0)? :
1
Is Item COFFEE present in this transaction(1/0)? :
0
Is Item JUICE present in this transaction(1/0)? :
1
Is Item COOKIES present in this transaction(1/0)? :
1
Is Item JAM present in this transaction(1/0)? :
0
Transaction 2 :
Is Item MILK present in this transaction(1/0)? :
1
Is Item BREAD present in this transaction(1/0)? :
0
Is Item COFFEE present in this transaction(1/0)? :
0
Is Item JUICE present in this transaction(1/0)? :
1
Is Item COOKIES present in this transaction(1/0)? :
0
Is Item JAM present in this transaction(1/0)? :
0
Transaction 3 :
Is Item MILK present in this transaction(1/0)? :
1
Is Item BREAD present in this transaction(1/0)? :
0
Is Item COFFEE present in this transaction(1/0)? :
0
Is Item JUICE present in this transaction(1/0)? :
0
Is Item COOKIES present in this transaction(1/0)? :
0
Is Item JAM present in this transaction(1/0)? :
1
Transaction 4 :
Is Item MILK present in this transaction(1/0)? :
0
Is Item BREAD present in this transaction(1/0)? :
1
Is Item COFFEE present in this transaction(1/0)? :
1
Is Item JUICE present in this transaction(1/0)? :
0
Is Item COOKIES present in this transaction(1/0)? :
1
Is Item JAM present in this transaction(1/0)? :
0
Number of Item MILK :3
Number of Item BREAD :2
Number of Item COFFEE :1
Number of Item JUICE :2
Number of Item COOKIES :2
Number of Item JAM :1
Item MILK is selected
Item BREAD is selected
Item JUICE is selected
Item COOKIES is selected
Number of Items of  MILK& BREAD :1
Number of Items of  MILK& COFFEE :0
Number of Items of  MILK& JUICE :2
Number of Items of  MILK& COOKIES :1
Number of Items of  MILK& JAM :0
Number of Items of  BREAD& COFFEE :0
Number of Items of  BREAD& JUICE :1
Number of Items of  BREAD& COOKIES :2
Number of Items of  BREAD& JAM :0
Number of Items of  COFFEE& JUICE :0
Number of Items of  COFFEE& COOKIES :0
Number of Items of  COFFEE& JAM :0
Number of Items of  JUICE& COOKIES :1
Number of Items of  JUICE& JAM :0
Number of Items of  COOKIES& JAM :0
Item MILK& JUICE is selected
Item BREAD& COOKIES is selected

*/

//Create two file Config.txt & transa.txt put data as follows

Config.txt
81000040

transa.txt
1 2 3 4 5
6 7 8 9 10

7 comments :

shell script code to swap 2 numbers


swap()
{
echo "Enter num1"
read num1
echo "Enter num2"
read num2

temp=$num1
num1=$num2
num2=$temp
echo "swapping numbers"
echo "num1 is $num1"
echo "num2 is $num2"
}
swap


/*
@linux-ogus:~/Desktop> chmod 755 ex2
@linux-ogus:~/Desktop> ./ex2
Enter num1
20
Enter num2
30
swapping numbers
num1 is 30
num2 is 20
*/

0 comments :

shell script code to reverse number


echo "Enter the Number"
read n1
rev=0
a=0
while [ $n1 -gt "0" ]
do
mod=`expr $n1 % 10`
rev=`expr $rev \* 10 + $mod`
n1=`expr $n1 / 10`

done
echo "$rev"

/* OUTPUT
linux-ogus:~> ./reverse_no
Enter the Number
654
456
*/

0 comments :

shell script code to perform arithmatic operations


kk=y
while [ "$kk" != "y" ]
do
echo "Enter value of x"
read x
echo "Enter value of y"
read y
echo "Enter your choice"
echo "1 Add"
echo "2 Sub"
echo "3 Multi"
echo "4 div"
echo "5 i dont know"
read case
case $case in
1) expr $x + $y ;;
2) expr $x - $y ;;
3) expr $x * $y ;;
4) expr $x % $y ;;
5) exit
esac
echo "enter option"
read option
echo "do you want to continue"
read kk
done


/*Output
linux-mj2y:~> sh arith.sh
Enter value of x
3
Enter value of y
5
Enter your choice
1 Add
2 Sub
3 Multi
4 div
5 i dont know
1
8
enter option
y
do you want to continue
n
Enter value of x
3
Enter value of y
3
Enter your choice
1 Add
2 Sub
3 Multi
4 div
5 i dont know
4
0
enter option
kk
do you want to continue
y

*/

0 comments :

shell script code to find greater no


echo "Enter first num"
read n1
echo "Enter the second num"
read n2
echo "Enter the third num"
read n3
if [ $n1 -gt $n2 ]
then
val=$n1
else
val=$n2
fi
if [ $val -gt $n3 ]
then
echo "$val is the greatest num"
else
echo "$n3 is the greatest num"
fi

/* OUTPUT

shell:~> ./greater_no
Enter first num
2
Enter the second num
4
Enter the third num
54
54 is the greatest num
*/

0 comments :

PROGRAM TO MULTITHREADING in JAVA


class a extends Thread
{
public void run()
{
for(inti=0;i<=5;i++)
{
System.out.println(this.getName()+" :"+i);
}
System.out.println("The End of child Thread:"+this.getName());
}
}
class b extends Thread
{
public void run()
{
for(int i=0;i<=5;i++)
{
System.out.println(this.getName()+" :"+i);
}
System.out.println("The End of child Thread:"+this.getName());
}
}

class c extends Thread
{
public void run()
{
for(int i=0;i<=5;i++)
{
System.out.println(this.getName()+" :"+i);
}
System.out.println("The End of child Thread:"+this.getName());
}
}

class test
{
public static void main(String args[])

{
a thra=new a();
b thrb=new b();
c thrc=new c();

thra.setName("Thread_a");
thrb.setName("Thread_b");
thrc.setName("Thread_c");

thra.setPriority(Thread.MAX_PRIORITY);
thrb.setPriority(Thread.MIN_PRIORITY);
thrc.setPriority(Thread.NORM_PRIORITY);

System.out.println("start Thread b...");
thrb.start();

System.out.println("start Thread c...");
thrc.start();

System.out.println("start Thread a...");
thra.start();

try
{

thra.join();
thrb.join();
thrc.join();
}
catch(InterruptedException e)
{}

}
}

0 comments :

Page Replacement Policies


import java.io.*;
class pagereplacement
{
public static void main(String a[])throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int mf[]=new int[3];
String wish;
int i,j,k,msize=3,fault;
int x[];

System.out.println("Enter the total no. of pages ");
int n=Integer.parseInt(b.readLine());
x=new int[n];
System.out.println("Enter the page nos.");
for(i=0;i3)
index=0;
if(j>3)
{
for(i=j;i<(j+3)&&j<(x.length-3);i++) for(k=0;k=3)
index=0;
if(flag)
{if(mf[index]!=0)
{fault++;}
mf[index]=temp;
index++;
}
System.out.println("Main frame is......");
for(i=0;i>"+mf[i]);
}
System.out.println("Number of hits is"+hit);
System.out.println("Number of faults is"+fault);
break;
default :System.out.println("Wrong Choice!!!!");
}
System.out.println("Do you want to continue(y/n):");
wish=b.readLine();
}
while(wish.equalsIgnoreCase("y"));
}

}

0 comments :

PROGRAM TO IMPLEMENT DINING PHILOSOPHER Problem in JAVA


class dinningPphilosopher
{ public static void main(String a[])
{
int Semaphore=0,i,j=1;
String philo[]={"Gandhi","Socrates","Tagore","Einstein","Newton"};
int fork[]=new int[5];
for(i=0;i<5 br="" i=""> {
if(Semaphore==0)
{ if(i%2==0)
{
System.out.println("*****************************************");
System.out.println("Batch "+j+" has started");
}
System.out.println(philo[i]+" is dinning currently");
if(i%2!=0&&i!=0)
{
Semaphore=1;
}
}
if(Semaphore==1)
{
System.out.println("Sorry you will have to wait till batch "+j+" finishes dinning");
System.out.println("Batch "+j+" has finished dining");
Semaphore=0;
j+=1;
}
}
System.out.println("All the philosophers have finished dinning");
}
}

0 comments :

PROGRAM TO IMPLEMENT BANKERS ALGORITHM

import java.io.*;
class banker
{
public static void main(String args[])throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int m,n,i,j,count=0;
System.out.println("Enter the number of process maximum(4)...");
m=Integer.parseInt(b.readLine());
System.out.println("Enter the number of Resources maximum(3)...");
n=Integer.parseInt(b.readLine());
int claim[][]=new int[m][n];
int allocation[][]=new int[m][n];
int need[][]=new int[m][n];
int resource[]=new int[n];
int available[]=new int[n];

System.out.println("Enter the resource matrix");
for(j=0;j resource[j]=Integer.parseInt(b.readLine());

System.out.println("Enter the available matrix");
for(j=0;j available[j]=Integer.parseInt(b.readLine());

for(i=0;i for(j=0;j { System.out.println("Enter the claim of process "+i+" for resource "+j);
claim[i][j]=Integer.parseInt(b.readLine());
}

for(i=0;i for(j=0;j { System.out.println("Enter the allocation of process "+i+" for resource "+j);
allocation[i][j]=Integer.parseInt(b.readLine());
}

for(i=0;i for(j=0;j { need[i][j]=claim[i][j]-allocation[i][j]; }

do
{
for(i=0;i {
if(need[i][0]==0&&need[i][1]==0&&need[i][2]==0)
continue;

if(need[i][0]<=available[0]&&need[i][1]<=available[1]&&need[i][2]<=available[2])
{ count++;
need[i][0]=need[i][1]=need[i][2]=0;
available[0]=available[0]+allocation[i][0];
available[1]=available[1]+allocation[i][1];
available[2]=available[2]+allocation[i][2];

System.out.println("..Need matrix...");
for(i=0;i { for(j=0;j {System.out.print(need[i][j]+" ");}
System.out.println();
}
System.out.println("..available matrix...");

for(j=0;j System.out.println(available[j]+" ");
}

}
}while(count<4 br="">

}
}

0 comments :

program to implement Shortest Job First Algorithm(SJF) algorithm in JAVA


import java.io.*;

class sjf
{
public static void main (String args[])throws IOException
{
int i,j,temp;
BufferedReader b= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter no. of processes");
int p=Integer.parseInt(b.readLine());
int burst[]=new int[p];
System.out.println("Enter the burst time for each process");
for(i=0;i
burst[j+1])
{
temp=burst[j+1];
burst[j+1]=burst[j];
burst[j]=temp;
}
}
}
int arv[] = new int[p];
System.out.println("Enter arrival time for each process");
for(i=0;i
arv[i]=Integer.parseInt(b.readLine());
int wait[] = new int[p];
System.out.println("Wait time for each process :");
System.out.println("Process 0 "+wait[0]+" units");
for(i=1;i
{
wait[i]=arv[i-1]+burst[i-1]+wait[i-1]-arv[i];
System.out.println("Process "+i+" "+wait[i]+" units");
}
}
}

0 comments :

PROGRAM TO IMPLEMENT CPU SCHEDULING POLICIES in JAVA


import java.io.*;
import java.util.*;
class schpol
{ public static void main(String []arg)throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int i,j,t=0,tun=0,ch,q;
String s;
int []ind=new int[10];
int []arr=new int[10];
int []burst=new int[10];
int []bur=new int[10];
System.out.println("enter no of processes :");
int n=Integer.parseInt(b.readLine());
for(i=0;i0)
{ t= (tun)-arr[i] ;
if(t>0)
{ System.out.println(" process p"+i+" waiting for "+t ); }
else
{ System.out.println("idle for "+Math.abs(t) +" time"); }

tun=tun+burst[i];
}
else
{ tun=burst[i]; }

for(j=0;j for(j=0;j { System.out.println("----");
}
System.out.println(" process p"+ind[i]+" completed");
}
break;

case 3:
tun=0;
System.out.println("enter quatum no:");
q=Integer.parseInt(b.readLine());
i=0;
tun=burst[i];
while(tun!=0)
{
for(i=0;i {
for(j=0;j { if(burst[i]!=0)
{
System.out.println("process p"+i+" executing .......");
burst[i]=burst[i]-1;
t++;
if(burst[i]==0)
{
System.out.println("process p"+i+" completed ");
break;
}
}
}
if(t { System.out.println("processor idle "); }
}
tun=0;
for(i=0;i { tun=tun+burst[i]; }
}
break;
}
System.out.println("do u want to continue(y/n) ");
s=b.readLine();
}
while(s.equals("y"));

}
}

0 comments :

program to implement Round Robin algorithm in JAVA


import java.io.*;
class RRobin
{
public static void main(String[] args)throws IOException
{
    BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
    int np,q,i,j,k;
    int s=0;
    System.out.println("ENTER THE NUMBER OF PROCESSES");
    np=Integer.parseInt(b.readLine());
    int bu[]=new int[np+1];
    int p[]=new int[np+1];
    System.out.println("ENTER THE QAUNTUM NUMBER");
    q=Integer.parseInt(b.readLine());
    System.out.println("ENTER THE BURST TIME OF EACH PROCESS");
    for(i=0;i<np;i++)
    {
        bu[i]=Integer.parseInt(b.readLine());
    }
    for(k=0;k<np;k++)
    {
        s=bu[k]+s;
    }
    s=s/q;
    for(k=0;k<s; k++)
    {
        for(i=0;i<np;i++)
        {
            if(bu[i]!=0)
            {
                System.out.println("PROCESS "+i+" IS EXECUTING");
                for(j=0;j<q;j++)
                {
                    System.out.println("..");
                    bu[i]--;
                }
            } 
        }
    }
}
}



***** OUTPUT *****

C:\>javac RRobin.java

C:\>java -cp . RRobin
ENTER THE NUMBER OF PROCESSES
3
ENTER THE QAUNTUM NUMBER
2
ENTER THE BURST TIME OF EACH PROCESS
3
4
2
PROCESS 0 IS EXECUTING
..
..
PROCESS 1 IS EXECUTING
..
..
PROCESS 2 IS EXECUTING
..
..
PROCESS 0 IS EXECUTING
..
..
PROCESS 1 IS EXECUTING
..
..
PROCESS 0 IS EXECUTING

2 comments :

program to implement MUTUAL EXCLUSION(Producer-Consumer Algorithm) in JAVA


import java.io.*;
import java.util.*;
class buffer
{ int count=0;
int flag=0;
int id[]=new int[10];
int value[]=new int[10];

class producer
{ int pid=0;
producer(int id)
{ pid=id; }
void putvalue()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
if(flag==0)
{ flag=1;
System.out.println("enter the value to be put in buffer by producer with id "+pid +" :");
int v=Integer.parseInt(b.readLine());
id[count]=pid;
value[count]=v;
count++;
flag=0;
}
}
}


class consumer
{ int cid=0;
consumer(int id)
{ cid=id; }
void getvalue()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
if(flag==0)
{ flag=1;
int j;
System.out.println("enter the id of producer whose value u want to access ");
int i=Integer.parseInt(b.readLine());
for(j=0;j { if(id[j]==i)
{ System.out.println(" required value is "+value[j]);
break;
}

}
if(j==count)
{System.out.println(" id not found "); }
else
{
while(j<(count-1))
{ id[j]=id[j+1];
value[j]=value[j+1];
j++;
}
count--;
}
flag=0;
}
}

}


void display()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i { System.out.print("producer id ="+id[i]+ " ");
System.out.println(" value ="+value[i]);
}
}

void access()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int i,j;

producer p1=new producer(10);
producer p2=new producer(20);
consumer c1=new consumer(30);
p1.putvalue();
p2.putvalue();
display();
p1.putvalue();
c1.getvalue();
display();
p1.putvalue();
p2.putvalue();
display();
}
}




class procon
{ public static void main(String []arg)throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
buffer b1=new buffer();
b1.access();
}
}

1 comments :

program to implement page replacement algorithm in JAVA


import java.io.*;

class page
{
public static void main(String []st)throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter no. of pages");
int n=Integer.parseInt(b.readLine());
int page_no[]=new int[n];
int pf=0,hits=0,i,j;
System.out.println("Enter no. of pages in page frame");
int m=Integer.parseInt(b.readLine());
int page_frame[]=new int[m];
int pointer=0,hit=0;
for(i=0;i {
System.out.println("Enter "+(i+1)+" page no.");
page_no[i]=Integer.parseInt(b.readLine());
}
for(i=0;i page_frame[i]=page_no[i];
for(i=0;i System.out.print(page_frame[i]+" ");
for(i=m;i {
for(j=0;j {
if(page_frame[j]==page_no[i])
{
hits++;
hit=1;
System.out.println();
for(int k=0;k System.out.print(page_frame[k]+" ");
}
}
if(hit==0)
{
page_frame[pointer]=page_no[i];
pointer++;
if(pointer==m)
pointer=0;
pf++;
System.out.println();
for(int k=0;k System.out.print(page_frame[k]+" ");
}
hit=0;
}
System.out.println("\nNo. of hits "+hits);
System.out.println("No. of pf "+(pf+1));
}
}

0 comments :

program to implement FIFO algorithm in JAVA


import java.io.*;
class fifo
{
public static void main(String []st)throws IOException
{
int pf=0,hits=0,i,j;
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter no of pages=>");
int p=Integer.parseInt(b.readLine());
int page_no[]=new int[p];
System.out.println("Enter no. of pages in page frame");
int m=Integer.parseInt(b.readLine());
int page_frame[]=new int[m];
int pointer=0,hit=0;
for(i=0;i
{
System.out.println("Enter "+(i+1)+" page no.");
page_no[i]=Integer.parseInt(b.readLine());
}
for(i=0;i page_frame[i]=page_no[i];

for(i=0;i System.out.print(page_frame[i]+" ");
for(i=m;i
{
for(j=0;j {
if(page_frame[j]==page_no[i])
{
hits++;
hit=1;
System.out.println();
for(int k=0;k System.out.print(page_frame[k]+" ");
}
}
if(hit==0)
{
page_frame[pointer]=page_no[i];
pointer++;
if(pointer==m)
pointer=0;
pf++;
System.out.println();
for(int k=0;k System.out.print(page_frame[k]+" ");
}
hit=0;
}
System.out.println("\nNo. of hits "+hits);
System.out.println("No. of pf "+(pf+1));
}
}

0 comments :

program to implement FCFS algorithm in JAVA


import java.io.*;

class fcfs
{
public static void main(String[] arg) throws IOException
{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number of processes");
int nop=Integer.parseInt(b.readLine());
int burst_time[] = new int[nop];
System.out.println("Enter burst time for each process");
for(int i =0;i burst_time[i]=Integer.parseInt(b.readLine());
int arrival_time[] = new int[nop];
System.out.println("Enter arrival time for each process");
for(int i=0;i arrival_time[i]=Integer.parseInt(b.readLine());
int wait_time[] = new int[nop];
System.out.println("Wait time for each process :");
System.out.println("Process 0 "+wait_time[0]+" units");
for(int i=1;i {
wait_time[i]=arrival_time[i-1]+burst_time[i-1]+wait_time[i-1]-arrival_time[i];
System.out.println("Process "+i+" "+wait_time[i]+" units");
}
}
}

code for 3D Tranformation



#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
int s1,s2,h,w;
class trans
{
public:
void draw();
void modified();
void translate();
void shearx();
void sheary();
void rotate();
void scale();
void assign();
int op[10],ip[10],mop[10],mip[10],i,dist;
}tr;
void trans::assign()
{
op[0]=op[6]=op[8]=s1;
op[1]=op[3]=op[9]=s2;
op[2]=op[4]=s1+w;
op[5]=op[7]=s2+h;
ip[0]=ip[6]=ip[8]=s1+w/2;
ip[1]=ip[3]=ip[9]=s2-h/2;
ip[2]=ip[4]=s1+w+w/2;
ip[5]=ip[7]=s2+h-h/2;
}
void trans::draw()
{
cleardevice();
setcolor(5);
drawpoly(5,op);
drawpoly(5,ip);
line(op[0],op[1],ip[0],ip[1]);
line(op[2],op[3],ip[2],ip[3]);
line(op[4],op[5],ip[4],ip[5]);
line(op[6],op[7],ip[6],ip[7]);
}
void trans::modified()
{
setcolor(15);
drawpoly(5,mop);
drawpoly(5,mip);
line(mop[0],mop[1],mip[0],mip[1]);
line(mop[2],mop[3],mip[2],mip[3]);
line(mop[4],mop[5],mip[4],mip[5]);
line(mop[6],mop[7],mip[6],mip[7]);
}
void trans::translate()
{
cout<<"\n Transition distance";
cin>>dist;
cleardevice();
draw();
for(i=0;i<10;i++)
{
mop[i]=op[i]+dist;
mip[i]=ip[i]+dist;
}
modified();
}
void trans::shearx()
{
cout<<"\n\t shearx factor";
cin>>dist;
cleardevice();
draw();
for(i=0;i<10;i=i+2)
{
mop[i]=op[i]+(dist+op[i+1]);
mop[i+1]=op[i+1];
mip[i]=ip[i]+(dist+ip[i+1]);
mip[i+1]=ip[i+1];
}
modified();
}
void trans::sheary()
{
cout<<"\n\t sheary factor";
cin>>dist;
cleardevice();
draw();
for(i=0;i<10;i=i+2)
{
mop[i]=op[i];
mop[i+1]=dist+op[i]+op[i+1];
mip[i]=ip[i];
mip[i+1]=dist+ip[i]+ip[i+1];
}
modified();
}
void trans::rotate()
{
int h,k;
float ang,rad;
cout<<"rotation angle";
cin>>ang;
rad=(3.14*ang)/180;
cout<<"\n\t rotate x by:";
cin>>h;
cout<<"\n\t rotate y by:";
cin>>k;
cleardevice();
draw();
for(int i=0;i<10;i=i+2)
{
mop[i]=h+(op[i]-h)*cos(rad)-(op[i+1]-k)*sin(rad);
mop[i+1]=k+(op[i]-h)*sin(rad)+(op[i+1]-k)*cos(rad);
mip[i]=h+(ip[i]-h)*cos(rad)-(ip[i+1]-k)*sin(rad);
mip[i+1]=k+(ip[i]-h)*sin(rad)+(ip[i+1]-k)*cos(rad);
}
modified();
}
void trans::scale()
{
int sx,sy;
cout<<"\n\t increment x by:";
cin>>sx;
cout<<"\n\t increment y by:";
cin>>sy;
cleardevice();
draw();
for(i=0;i<10;i++)
{
if(i%2==0)
{
mop[i]=op[i]*sx;
mip[i]=ip[i]*sx;
}
else
{
mop[i]=op[i]*sy;
mip[i]=ip[i]*sy;
}
}
modified();
}
void main()
{
char op,again;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<<"\n\t Give cuboid co ordinate:";
cout<<"\n\t enter top left:";
cin>>s1>>s2;
cout<<"Enter height and width:";
cin>>h>>w;
tr.assign();
do
{
cout<<"\n\t menu";
cout<<"\n\t.....";
cout<<"\n\t translate\n\t scale\n\t rotate";
cout<<"\n\t shearx\n\tsheary";
cout<<"\n\n\t I like to do transformation";
op=toupper(getch());
cout<getch();
cleardevice();
switch(op)
{
case 'T':
tr.translate();
break;
case 'S':
tr.scale();
break;
case 'X':
tr.shearx();
break;
case 'Y':
tr.sheary();
break;
case 'R':
tr.rotate();
break;
}
cout<<"do once again:";
again=getch();
cleardevice();
}
while(again!='n'&&again!='n');
getch();
closegraph();
}

0 comments :

code for polygon clipping


#include <math.h>
#include <stdio.h>
#include <graphics.h>

int     n,p,x,y,i,j;
int     xwmin,ywmin,xwmax,ywmax;
int     x1,y1,x2,y2,xmax,ymax;
float     m;
char     flag;
float     dy,dx;

main()
{
    int gd,gm;
    int a[10][10],b[10][10],c[10][10],d[10][10],e[10][10];

    detectgraph(&gd,&gm);
    initgraph(&gd,&gm,"c:\\tc\\bgi");


    printf("\n\n\tEnter the no. of edges of polygon :  ");
    scanf("%d",&n);

    printf("\n\n\tEnter the cordinates  of  polygon :\n\n\n ");

    for(i=0;i<n;i++)
    {
        printf("\tX%d  Y%d  :  ",i,i);
        scanf("%d %d",&a[i][0],&a[i][1]);
    }

    a[n][0]=a[0][0];
    a[n][1]=a[0][1];
 

0 comments :

code for Line clip


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{ clrscr();
 int gd,gm;
 gd=DETECT;
 gm=0;
 initgraph(&gd,&gm,"Z:\TC\BGI");
int xi1,yi1,xi2,yi2,m,xwmin=20,xwmax=300,ywmin=20,ywmax=500,x1,y1,x2,y2,b11,b12,b13,b14,b21,b22,b23,b24;
cout<<"enter end points of line";
cin>>x1>>y1>>x2>>y2;
rectangle(xwmin,ywmax,xwmax,ywmin);
line(x1,y1,x2,y2);
if(xwmin-x1>0)
{ b11=1;}
else{b11=0;}
if(x1-xwmax>0)
{ b12=1;}
else{b12=0;}
if(ywmin-y1>0)
{ b13=1;}
else{b13=0;}
if(y1-ywmax>0)
{ b14=1;}
else{b14=0;}
if(xwmin-x2>0)
{ b21=1;}
else{b21=0;}
if(x2-xwmax>0)
{ b22=1;}
else{b22=0;}
if(ywmin-y2>0)
{ b23=1;}
else{b23=0;}
if(y2-ywmax>0)
{ b24=1;}
else{b24=0;}
if(b11==0&&b12==0&&b13==0&&b14==0&&b21==0&&b22==0&&b23==0&&b24==0)
{cout<<"completely lie inside";}
else
{if(((b11&b21)==0)&&((b12&b22)==0)&&((b13&b23)==0)&&((b14&b24)==0))
 { m=(y2-y1)/(x2-x1);
   if(b11==1||b12==1)
   {if(b11==1)
    {xi1=xwmin;}
    else
    {xi1=xwmax;}
    yi1=y1+(m*(xi1-x1));
   }
   else
   {if(b13==1)
    {yi1=ywmin;}
    else
    {yi1=ywmax;}
    xi1=x1+((yi1-y1)/m);
   }
   if(b21==1||b22==1)
   {if(b21==1)
    {xi2=xwmin;}
    else
    {xi2=xwmax;}
    yi2=y2+(m*(xi2-x2));
   }
   else
   {if(b23==1)
    {yi2=ywmin;}
    else
    {yi2=ywmax;}
    xi2=x2+((yi2-y2)/m);
   }
   sleep(3);
   clrscr();
   rectangle(xwmin,ywmax,xwmax,ywmin);
   line(xi1,yi1,xi2,yi2);
  }
  else
  {cout<<"lie completely outside";}
  }
  getch();
}

0 comments :

2D Tranformation


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>

 void main()
{
 clrscr();
 int gdriver = DETECT, gmode;

  int x1,x2,y1,y2,steps;
 cout<<"Enter the first point\n";
 cin>>x1;
 cin>>y1;
 cout<<"Enter the last point\n";
 cin>>x2;
 cin>>y2;
 int a1[3][1]={x1,y1,1};
 int a2[3][1]={x1,y2,1};
 int a3[3][1]={x2,y2,1};
 int a4[3][1]={x2,y1,1};

 initgraph(&gdriver, &gmode, "Z:\TC\BGI");
 line(x1,y1,x1,y2);
 line(x1,y2,x2,y2);
 line(x2,y2,x2,y1);
 line(x2,y1,x1,y1);

 cout<<"1> Translation\n";
 cout<<"2> Rotation\n";
 cout<<"3> Scale\n";
 int c;
 cin>>c;
 double c1[3][1];
 double c2[3][1];
 double c3[3][1];
 double c4[3][1];
 switch(c)
 {
 case 1:int x,y;
    cout<<"Enter x displacement\n";
    cin>>x;
    cout<<"Enter y displacement\n";
    cin>>y;
    int d[3][3]={1,0,x,0,1,y,0,0,1};
    for(int i=0;i<3;i++)
    {
    c1[i][0]=0;
    c2[i][0]=0;
    c3[i][0]=0;
    c4[i][0]=0;
    }

    for(i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
    {
    c1[i][0]+=d[i][j]*a1[j][0];
    c2[i][0]+=d[i][j]*a2[j][0];
    c3[i][0]+=d[i][j]*a3[j][0];
    c4[i][0]+=d[i][j]*a4[j][0];
    }
    }
    clrscr();
    initgraph(&gdriver, &gmode, "Z:\TC\BGI");
    line(c1[0][0],c1[1][0],c2[0][0],c2[1][0]);
    line(c2[0][0],c2[1][0],c3[0][0],c3[1][0]);
    line(c3[0][0],c3[1][0],c4[0][0],c4[1][0]);
    line(c4[0][0],c4[1][0],c1[0][0],c1[1][0]);
    break;

 case 2:double u,v,o;
    cout<<"Enter angle\n";
    cin>>o;
    u=cos(o);
    v=sin(o);
    int l,m;
    l=a1[0][0]*(1-u)+a2[1][0]*v;
    m=a1 [1][0]*(1-u)-a2[0][0]*v;
    double b[3][3]={u,(-1*v),l,v,u,m,0,0,1};

    for( i=0;i<3;i++)
    {
    c1[i][0]=0;
    c2[i][0]=0;
    c3[i][0]=0;
    c4[i][0]=0;
    }

    for(i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
    {
    c1[i][0]+=b[i][j]*a1[j][0];
    c2[i][0]+=b[i][j]*a2[j][0];
    c3[i][0]+=b[i][j]*a3[j][0];
    c4[i][0]+=b[i][j]*a4[j][0];
    }
    }
    clrscr();
    initgraph(&gdriver, &gmode, "Z:\TC\BGI");
    line(c1[0][0],c1[1][0],c2[0][0],c2[1][0]);
    line(c2[0][0],c2[1][0],c3[0][0],c3[1][0]);
    line(c3[0][0],c3[1][0],c4[0][0],c4[1][0]);
    line(c4[0][0],c4[1][0],c1[0][0],c1[1][0]);

    break;

case 3: double sx,sy;
    cout<<"Enter x scaling factor\n";
    cin>>sx;
    cout<<"Enter y scaling factor\n";
    cin>>sy;
    double e[3][3]={sx,0,0,0,sy,0,0,0,1};

    for( i=0;i<3;i++)
    {
    c1[i][0]=0;
    c2[i][0]=0;
    c3[i][0]=0;
    c4[i][0]=0;
    }

    for(i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
    {
    c1[i][0]+=e[i][j]*a1[j][0];
    c2[i][0]+=e[i][j]*a2[j][0];
    c3[i][0]+=e[i][j]*a3[j][0];
    c4[i][0]+=e
    [i][j]*a4[j][0];
    }
    }
    clrscr();
    initgraph(&gdriver, &gmode, "Z:\TC\BGI");
    line(c1[0][0],c1[1][0],c2[0][0],c2[1][0]);
    line(c2[0][0],c2[1][0],c3[0][0],c3[1][0]);
    line(c3[0][0],c3[1][0],c4[0][0],c4[1][0]);
    line(c4[0][0],c4[1][0],c1[0][0],c1[1][0]);
    break;
 }
 getch();
 }

0 comments :

Besier curve


#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<process.h>

int gd,gm,maxx,maxy;
float xx[4][2];
void line1(float x2,float y2)
{
    line(xx[0][0],xx[0][1],x2,y2);
    xx[0][0]=x2;
    xx[0][1]=y2;
}

void besier(float xb, float yb, float xc, float yc, float xd, float yd, int n)
{
    float xab,yab,xbc,ybc,xcd,ycd;
    float xabc,yabc,xbcd,ybcd;
    float xabcd,yabcd;
    if(n==0)
    {
        line1(xb,yb);
        line1(xc,yc);
        line1(xd,yd);
    }
    else
    {
        xab=(xx[0][0]+xb)/2;
        yab=(xx[0][1]+yb)/2;
        xbc=(xb+xc)/2;
        ybc=(yb+yc)/2;
        xcd=(xc+xd)/2;
        ycd=(yc+yd)/2;
        xabc=(xab+xbc)/2;
        yabc=(yab+ybc)/2;
        xbcd=(xbc+xcd)/2;
        ybcd=(ybc+ycd)/2;
        xabcd=(xabc+xbcd)/2;
        yabcd=(yabc+ybcd)/2;
        n=n-1;
        besier(xab,yab,xabc,yabc,xabcd,yabcd,n);
        besier(xbcd,ybcd,xcd,ycd,xd,yd,n);
    }
}
void igraph()
{
    detectgraph(&gd,&gm);
    if(gd<0)
    {
        puts("Cannot detect driver card");
        exit(0);
    }
    initgraph(&gd, &gm, "Z:/Tc/BGI");
}
void main()
{
    int i;
    float temp1, temp2;
    igraph();
    for(i=0;i<4;i++)
    {
        printf("Enter the co-ordinates of the point %d\n", i+1);
        scanf("%f %f", &temp1, &temp2);
        xx[i][0]= temp1;
        xx[i][1]= temp2;
    }
    besier(xx[1][0], xx[1][1], xx[2][0], xx[2][1], xx[3][0], xx[3][1], 4);
    getch();
    closegraph();
}

0 comments :

code for Boundry Fill Algorithm


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<stdio.h>

void boundaryfill(int x,int y,int fcolor,int bcolor);
void main()
{ clrscr();
  int gd,gm;
  gd=DETECT;
  gm=0;
  initgraph(&gd,&gm,"Z:\TC\BGI");
  int x,y,bcolor,fcolor;

  cout<<"enter seed point";
  cin>>x>>y;
  cout<<"enter boundary color";
  cin>>bcolor;
  cout<<"enter new color";
  cin>>fcolor;
  setcolor(bcolor);
  rectangle(50,50,100,100);
  boundaryfill(x,y,fcolor,bcolor);
  getch();
  }

void boundaryfill(int x,int y,int fcolor,int bcolor)
{
  if((getpixel(x,y)!=fcolor)&&(getpixel(x,y)!=bcolor))
  { putpixel(x,y,fcolor);
    boundaryfill(x+1 ,y,fcolor,bcolor);
    boundaryfill(x-1,y,fcolor,bcolor);
    boundaryfill(x,y-1,fcolor,bcolor);
    boundaryfill(x,y+1,fcolor,bcolor);
    boundaryfill(x+1,y-1,fcolor,bcolor);
    boundaryfill(x+1,y+1,fcolor,bcolor);
    boundaryfill(x-1,y-1,fcolor,bcolor);
    boundaryfill(x-1,y+1,fcolor,bcolor);
  }
 }

0 comments :

Code to Implement Bresenhams Line Generation


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
clrscr();
int gd,gm;
gd=DETECT;
gm=0;

int xa,ya,xb,yb;
cout<<"Enter ordinate of first point";
cin>>xa;
cout<<"Enter abissca of first point";
cin>>ya;
cout<<"Enter ordinate of second point";
cin>>xb;
cout<<"Enter abissca of second point";
cin>>yb;
double dx,dy,steps,p;
dx=xb-xa;
dy=yb-ya;
if(dx>=dy)
steps=abs(dx);
else
steps=abs(dy);
p=2*(dy)-dx;
initgraph(&gd,&gm,"Z:\tc\BGI");
putpixel(xa,ya,4);
for(int i=0;i<steps;i++)
{
if(p>=0)
{
putpixel(xa+1,ya+1,4);
p+=2*(dy-dx);
}
else
{
putpixel(xa+1,ya,4);
p+=2*(dy);
}
}  
getch();  
}

0 comments :

Code to Implement DDA Line Generation


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
clrscr();
int gd,gm;
gd=DETECT;
gm=0;

int xa,ya,xb,yb;
cout<<"Enter ordinate of first point";
cin>>xa;
cout<<"Enter abissca of first point";
cin>>ya;
cout<<"Enter ordinate of second point";
cin>>xb;
cout<<"Enter abissca of second point";
cin>>yb;
double dx,dy,steps;
double xinc,yinc;
dx=xb-xa;
dy=yb-ya;
if(dx>=dy)
steps=abs(dx);
else
steps=abs(dy);
xinc=dx/steps;
yinc=dy/steps;
initgraph(&gd,&gm,"Z:\tc\BGI");
putpixel(xa,ya,4);
double x,y;
x=xa+xinc;
y=ya+yinc;

for(int i=0;i<steps;i++)
{
putpixel(ceil(x),ceil(y),4);
x+=xinc;
y+=yinc;
}
getch();
}

0 comments :