To Run follow following Step
----------------------------------
D:\RMI\Prog1>javac *.java
D:\RMI\Prog1>rmic myRmiImpl
D:\RMI\Prog1>start rmiregistry
D:\RMI\Prog1>start java -classpath . -Djava.security.policy=java.policy myRmiServer
D:\RMI\Prog1>java -classpath . -Djava.security.policy=java.policy myRmiClient 127.0.0.1
Server System date : Sun Dec 09 19:16:41 PST 2007
GTU MCA Practical Solution for C ,C++, java, DBMS, SQL, RMI, CORBA, Network Programming, Servlet, JSP, ASP Code.
RMI String Manipulation
RMI Interface
----------------
public interface myRMIInterface extends java.rmi.Remote
{
public String sayhello(String str) throws java.rmi.RemoteException;
public String copystring(String str) throws java.rmi.RemoteException;
public String reversestring(String str) throws java.rmi.RemoteException;
public String palstring(String str) throws java.rmi.RemoteException;
}
----------------
public interface myRMIInterface extends java.rmi.Remote
{
public String sayhello(String str) throws java.rmi.RemoteException;
public String copystring(String str) throws java.rmi.RemoteException;
public String reversestring(String str) throws java.rmi.RemoteException;
public String palstring(String str) throws java.rmi.RemoteException;
}
RMI Implementation
----------------------
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
import java.lang.*;
public class myRMIImpl extends UnicastRemoteObject implements myRMIInterface
{
public myRMIImpl(String name) throws RemoteException
{
super();
try
{
Naming.rebind(name,this);
}
catch(Exception e)
{
System.out.println("Error "+e);
}
}
public String sayhello(String str)
{
return ("Hello "+str);
}
public String copystring(String str)
{
String str1=" ";
char ch;
int i;
for(i=0;i
{
ch=str.charAt(i);
str1+=ch;
}
return (str1);
}
public String reversestring(String str)
{
String rev="";
char ch;
int i;
for(i=(str.length())-1;i>=0;i--)
{
ch=str.charAt(i);
rev+=ch;
}
return rev;
}
public String palstring(String str)
{
String rev="";
char ch;
int i;
for(i=(str.length())-1;i>=0;i--)
{
ch=str.charAt(i);
rev+=ch;
}
if(rev==str)
{
return("Palindrom");
}
else
return("Not Palindrom");
}
}
RMI Client
------------
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.io.*;
import java.lang.*;
public class myRMIClient
{
public static void main(String []argv)
{
System.setSecurityManager(new RMISecurityManager());
if(argv.length != 1)
{
System.out.println("Please enter IPAddress of Server");
}
String servername=argv[0];
try
{
myRMIInterface interef=(myRMIInterface)
Naming.lookup("rmi://"+servername+"/myRMIImplInstance");
String str;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch;
do
{
System.out.println("------------------------");
System.out.println(" MENU ");
System.out.println("------------------------");
System.out.println("1.Hello Server");
System.out.println("2.Copy String ");
System.out.println("3.Reverse String");
System.out.println("4.To Check Palindrom String");
System.out.println("5.Upper case String");
System.out.println("6.Toggle case String");
System.out.println("7.EXIT");
System.out.println("------------------------");
System.out.print("Enter Ur Choice : ");
str=br.readLine();
ch=Integer.parseInt(str);
switch(ch)
{
case 1:
System.out.print("Enter name : ");
str=br.readLine();
System.out.println(interef.sayhello(str));
break;
case 2:
System.out.print("Enter String : ");
str=br.readLine();
System.out.println("Copy String is : "+interef.copystring(str));
break;
case 3:
System.out.print("Enter String : ");
str=br.readLine();
System.out.println("Reverse String is : "+interef.reversestring(str));
break;
case 4:
System.out.print("Enter String to check Palindrom : ");
str=br.readLine();
System.out.println(interef.palstring(str));
break;
case 7:
System.out.println("EXIT");
break;
}
}while(ch!=7);
}
catch(Exception e)
{
System.out.println("Error "+e);
}
}
}
RMI Server
---------------
import java.rmi.*;
import java.rmi.server.*;
public class myRMIServer
{
public static void main(String []argv)
{
System.setSecurityManager(new RMISecurityManager());
try
{
myRMIImpl impl=new myRMIImpl("myRMIImplInstance");
System.out.println("Server Waiting for Client");
}
catch(Exception e)
{
System.out.println("Error "+e);
}
}
}
RMI Matrix
RMI Interface
-------------------
public interface myRMIInterface extends java.rmi.Remote
{
public int getadd(int a,int b) throws java.rmi.RemoteException;
public int getsub(int a,int b) throws java.rmi.RemoteException;
public int getmul(int a,int b) throws java.rmi.RemoteException;
}
RMI Implementation
---------------------------
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public class myRMIImpl extends UnicastRemoteObject implements myRMIInterface
{
public myRMIImpl(String name) throws RemoteException
{
super();
try
{
Naming.rebind(name,this);
}
catch(Exception e)
{
System.out.println("Error "+e);
}
}
public int getadd(int a,int b)
{
return(a+b);
}
public int getsub(int a,int b)
{
return(a-b);
}
public int getmul(int a,int b)
{
return(a*b);
}
}
-------------------
public interface myRMIInterface extends java.rmi.Remote
{
public int getadd(int a,int b) throws java.rmi.RemoteException;
public int getsub(int a,int b) throws java.rmi.RemoteException;
public int getmul(int a,int b) throws java.rmi.RemoteException;
}
RMI Implementation
---------------------------
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public class myRMIImpl extends UnicastRemoteObject implements myRMIInterface
{
public myRMIImpl(String name) throws RemoteException
{
super();
try
{
Naming.rebind(name,this);
}
catch(Exception e)
{
System.out.println("Error "+e);
}
}
public int getadd(int a,int b)
{
return(a+b);
}
public int getsub(int a,int b)
{
return(a-b);
}
public int getmul(int a,int b)
{
return(a*b);
}
}
RMI Client
----------------
import java.io.*;
import java.lang.*;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class myRMIClient
{
public static void main(String []argv)
{
System.setSecurityManager(new RMISecurityManager());
if(argv.length!=1)
{
System.out.println("IP Address of Server Required");
System.exit(0);
}
String servername=argv[0];
try
{
myRMIInterface interef = (myRMIInterface)
Naming.lookup("rmi://"+servername+"/myRMIImplInstance");
int row,col,ch;
String str;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("---------------------------");
System.out.println("1.Matrix Addition");
System.out.println("2.Matrix Subtraction");
System.out.println("3.Matrix Multiplication");
System.out.println("4.EXIT");
System.out.println("---------------------------");
System.out.print("Enter Ur Choice : ");
str=br.readLine();
ch=Integer.parseInt(str);
int i,j;
int [][]a=new int[10][10];
int [][]b=new int[10][10];
int [][]c=new int[10][10];
switch(ch)
{
case 1:
System.out.println("Enter no of row :");
str=br.readLine();
row=Integer.parseInt(str);
System.out.println("Enter no of col :");
str=br.readLine();
col=Integer.parseInt(str);
for(i=0;i|
{
for(j=0;j
{
System.out.println("Enter value :");
str=br.readLine();
a[i][j]=Integer.parseInt(str);
}
}
System.out.println(" Matrix B");
for(i=0;i|
{
for(j=0;j
{
System.out.println("Enter value :");
str=br.readLine();
b[i][j]=Integer.parseInt(str);
}
}
for(i=0;i|
for(j=0;j
c[i][j]=interef.getadd(a[i][j],b[i][j]);
System.out.println("---------------------");
System.out.println(" Matrix A");
System.out.println("---------------------");
for(i=0;i|
{
for(j=0;j
System.out.print(" "+a[i][j]);
System.out.println();
}
System.out.println("---------------------");
System.out.println(" Matrix B");
System.out.println("---------------------");
for(i=0;i|
{
for(j=0;j
System.out.print(" "+b[i][j]);
System.out.println();
}
System.out.println("---------------------");
System.out.println("Matrix Addition : ");
System.out.println("---------------------");
for(i=0;i|
{
for(j=0;j
System.out.print(" "+c[i][j]);
System.out.println();
}
break;
case 2:
System.out.println("Enter no of row :");
str=br.readLine();
row=Integer.parseInt(str);
System.out.println("Enter no of col :");
str=br.readLine();
col=Integer.parseInt(str);
for(i=0;i|
{
for(j=0;j
{
System.out.println("Enter value :");
str=br.readLine();
a[i][j]=Integer.parseInt(str);
}
}
System.out.println(" Matrix B");
for(i=0;i|
{
for(j=0;j
{
System.out.println("Enter value :");
str=br.readLine();
b[i][j]=Integer.parseInt(str);
}
}
for(i=0;i|
for(j=0;j
c[i][j]=interef.getsub(a[i][j],b[i][j]);
System.out.println("---------------------");
System.out.println(" Matrix A");
System.out.println("---------------------");
for(i=0;i|
{
for(j=0;j
System.out.print(" "+a[i][j]);
System.out.println();
}
System.out.println("---------------------");
System.out.println(" Matrix B");
System.out.println("---------------------");
for(i=0;i|
{
for(j=0;j
System.out.print(" "+b[i][j]);
System.out.println();
}
System.out.println("---------------------");
System.out.println("Matrix Subtraction : ");
System.out.println("---------------------");
for(i=0;i|
{
for(j=0;j
System.out.print(" "+c[i][j]);
System.out.println();
}
break;
case 3:
System.out.println("Enter no of row :");
str=br.readLine();
row=Integer.parseInt(str);
System.out.println("Enter no of col :");
str=br.readLine();
col=Integer.parseInt(str);
for(i=0;i|
{
for(j=0;j
{
System.out.println("Enter value :");
str=br.readLine();
a[i][j]=Integer.parseInt(str);
}
}
System.out.println(" Matrix B");
for(i=0;i|
{
for(j=0;j
{
System.out.println("Enter value :");
str=br.readLine();
b[i][j]=Integer.parseInt(str);
}
}
int k;
for(k=0;k|
for(i=0;i|
for(j=0;j
c[i][j]+=interef.getmul(a[i][k],b[k][j]);
System.out.println("---------------------");
System.out.println(" Matrix A");
System.out.println("---------------------");
for(i=0;i|
{
for(j=0;j
System.out.print(" "+a[i][j]);
System.out.println();
}
System.out.println("---------------------");
System.out.println(" Matrix B");
System.out.println("---------------------");
for(i=0;i|
{
for(j=0;j
System.out.print(" "+b[i][j]);
System.out.println();
}
System.out.println("---------------------");
System.out.println("Matrix Multiplication : ");
System.out.println("---------------------");
for(i=0;i|
{
for(j=0;j
System.out.print(" "+c[i][j]);
System.out.println();
}
break;
}
}while(ch!=4);
}
catch(Exception e)
{
System.out.println("Error "+e);
}
}
}
RMI Server
----------------
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public class myRMIServer
{
public static void main(String []argv)
{
System.setSecurityManager(new RMISecurityManager());
try
{
System.out.println("Server Waiting for client");
myRMIImpl impl=new myRMIImpl("myRMIImplInstance");
}
catch(Exception e)
{
System.out.println("Error "+e);
}
}
}
RMI System Date
RMI Interface
----------------------
public interface myRmiInterface extends java.rmi.Remote
{
public java.util.Date getdate() throws java.rmi.RemoteException;
}
----------------------
public interface myRmiInterface extends java.rmi.Remote
{
public java.util.Date getdate() throws java.rmi.RemoteException;
}
RMI Implementation
------------------------
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
public class myRmiImpl extends UnicastRemoteObject implements myRmiInterface
{
public myRmiImpl(String name) throws RemoteException
{
super();
try
{
Naming.rebind(name,this);
}
catch(Exception e)
{
System.out.println("Error "+e);
}
}
public java.util.Date getdate()
{
return new java.util.Date();
}
}
RMI Client
---------------
import java.io.*;
import java.util.Date;
import java.lang.*;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class myRmiClient
{
public static void main(String[] argv)
{
System.setSecurityManager(new RMISecurityManager());
if(argv.length!=1)
{
System.out.println("Require IP Address of Server");
System.exit(0);
}
String servername = argv[0];
try
{
myRmiInterface interef = (myRmiInterface)
Naming.lookup("rmi://"+servername+"/myRmiImplInstance");
Date d=interef.getdate();
System.out.println("Server System date : "+d);
}
catch(Exception e)
{
System.out.println("Error "+e);
}
}
}
RMI Server
---------------
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public class myRmiServer
{
public static void main(String []argv)
{
System.setSecurityManager(new RMISecurityManager());
try
{
System.out.println("Server Waiting for client");
myRmiImpl impl = new myRmiImpl("myRmiImplInstance");
}
catch(Exception e)
{
System.out.println("Error "+e);
}
}
}
Bresenham's line drawing algorithm - 1
2.Write a menu driven program for Bresenham's line drawing algorithm for
the attributes like :
->solid line
->dashed line
->dotted line
in four quadrant of 2 - D plane.
/*------------------------------Class BRESEN-------------------------------*/
class BRESEN
{
int gd,gm;
int x1,y1,x2,y2;
int dx,dy,m,ch,count,count1;
public:
BRESEN()
{
gd=DETECT;
dx=dy=m=0;
count=0,count1=3;
}
void getpoint();
void bres_line(int);
void gentle(int);
void sharp(int);
};
/*-----------------------------getpoint()----------------------------------*/
void BRESEN::getpoint()
{
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
cout<<"Enter X1 : ";
cin>>x1;
cout<<"Enter Y1 : ";
cin>>y1;
cout<<"Enter X2 : ";
cin>>x2;
cout<<"Enter Y2 : ";
cin>>y2;
cleardevice();
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
line(getmaxx()/2,0,getmaxx()/2,getmaxy());
}
/*-----------------------------bres_line()---------------------------------*/
void BRESEN::bres_line(int ch)
{
dx=abs(x2-x1);
dy=abs(y2-y1);
m=(y2-y1)/(x2-x1);
if(dx > dy)
gentle(ch);
else
sharp(ch);
}
/*-------------------------------gentle()----------------------------------*/
void BRESEN::gentle(int ch)
{
int x,y,xend,p=0;
if (x1>x2)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
p=(2*dy)-dx;
//Initialize the counter variable
if(ch==2)
count=0,count1=5;
else
count=0,count1=3;
while(x
{
x++;
if(p<0)
p+=(2*dy);
else if(p>=0)
{
if(m>0)
y++;
else
y--;
p+=(2*dy)-(2*dx);
}
// For Solid line
if(ch==1)
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
// For Dot-Dashed line
if(ch==2)
{
count=0,count1=5;
if(count<5 && count1==5)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==5)
count1=0;
}
else
{
count1++;
if(count1==5)
count=0;
if(count1==3)
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
}
}
//For Dashed line
if(ch==3)
{
count=0,count1=3;
if(count<3 && count1==3)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==3)
count1=0;
}
else
{
count1++;
if(count1==3)
count=0;
}
}
if(ch==4)
{
if(count<1)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==1)
count1=0;
}
else
{
count1++;
if(count1==2)
count=0;
}
}
delay(200);
}
}
/*-------------------------------sharp()-----------------------------------*/
void BRESEN::sharp(int ch)
{
int x,y,yend,p=0;
if (y1>y2)
{
x=x2;
y=y2;
yend=y1;
}
else
{
x=x1;
y=y1;
yend=y2;
}
// To Maintain the Dotted Line
if(ch!=4)
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
p=(2*dx)-dy;
//Initialize counter variable
if(ch==2)
count=0,count1=5;
else
count=0,count1=3;
while(y
{
y++;
if(p<0)
p+=(2*dx);
else if(m>0)
x++;
else
{
x--;
p+=(2*dx)-(2*dy);
}
// For Solid line
if(ch==1)
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
// For Dot-Dashed Line
if(ch==2)
{
if(count<5 && count1==5)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==5)
count1=0;
}
else
{
count1++;
if(count1==5)
count=0;
if(count1==3)
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
}
}
// For Dashed line
if(ch==3)
{
if(count<3 && count1==3)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==3)
count1=0;
}
else
{
count1++;
if(count1==3)
count=0;
}
}
if(ch==4)
{
if(count<1)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==1)
count1=0;
}
else
{
count1++;
if(count1==2)
count=0;
}
}
delay(100);
}
}
/*-------------------------------main()-------------------------------------*/
int main()
{
BRESEN b;
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
int ch;
do
{
cleardevice();
cout<<"\n----------------------------------------------------";
cout<<"\n BRESENHAM ALGORITHM ";
cout<<"\n----------------------------------------------------";
cout<<"\n 1.SOLID LINE";
cout<<"\n 2.DOT-DASHED LINE";
cout<<"\n 3.DASHED LINE";
cout<<"\n 4.DOTTED LINE";
cout<<"\n 5.EXIT";
cout<<"\n------------------------------------------------------";
cout<<"\nENTER YR CHOICE : ";
cin>>ch;
switch(ch)
{
case 1:
b.getpoint();
b.bres_line(ch);
getch();
break;
case 2:
b.getpoint();
b.bres_line(ch);
getch();
break;
case 3:
b.getpoint();
b.bres_line(ch);
getch();
break;
case 4:
b.getpoint();
b.bres_line(ch);
break;
case 5:
break;
}
}while(ch!=5);
return 0;
}
the attributes like :
->solid line
->dashed line
->dotted line
in four quadrant of 2 - D plane.
/*------------------------------Class BRESEN-------------------------------*/
class BRESEN
{
int gd,gm;
int x1,y1,x2,y2;
int dx,dy,m,ch,count,count1;
public:
BRESEN()
{
gd=DETECT;
dx=dy=m=0;
count=0,count1=3;
}
void getpoint();
void bres_line(int);
void gentle(int);
void sharp(int);
};
/*-----------------------------getpoint()----------------------------------*/
void BRESEN::getpoint()
{
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
cout<<"Enter X1 : ";
cin>>x1;
cout<<"Enter Y1 : ";
cin>>y1;
cout<<"Enter X2 : ";
cin>>x2;
cout<<"Enter Y2 : ";
cin>>y2;
cleardevice();
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
line(getmaxx()/2,0,getmaxx()/2,getmaxy());
}
/*-----------------------------bres_line()---------------------------------*/
void BRESEN::bres_line(int ch)
{
dx=abs(x2-x1);
dy=abs(y2-y1);
m=(y2-y1)/(x2-x1);
if(dx > dy)
gentle(ch);
else
sharp(ch);
}
/*-------------------------------gentle()----------------------------------*/
void BRESEN::gentle(int ch)
{
int x,y,xend,p=0;
if (x1>x2)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
p=(2*dy)-dx;
//Initialize the counter variable
if(ch==2)
count=0,count1=5;
else
count=0,count1=3;
while(x
{
x++;
if(p<0)
p+=(2*dy);
else if(p>=0)
{
if(m>0)
y++;
else
y--;
p+=(2*dy)-(2*dx);
}
// For Solid line
if(ch==1)
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
// For Dot-Dashed line
if(ch==2)
{
count=0,count1=5;
if(count<5 && count1==5)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==5)
count1=0;
}
else
{
count1++;
if(count1==5)
count=0;
if(count1==3)
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
}
}
//For Dashed line
if(ch==3)
{
count=0,count1=3;
if(count<3 && count1==3)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==3)
count1=0;
}
else
{
count1++;
if(count1==3)
count=0;
}
}
if(ch==4)
{
if(count<1)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==1)
count1=0;
}
else
{
count1++;
if(count1==2)
count=0;
}
}
delay(200);
}
}
/*-------------------------------sharp()-----------------------------------*/
void BRESEN::sharp(int ch)
{
int x,y,yend,p=0;
if (y1>y2)
{
x=x2;
y=y2;
yend=y1;
}
else
{
x=x1;
y=y1;
yend=y2;
}
// To Maintain the Dotted Line
if(ch!=4)
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
p=(2*dx)-dy;
//Initialize counter variable
if(ch==2)
count=0,count1=5;
else
count=0,count1=3;
while(y
{
y++;
if(p<0)
p+=(2*dx);
else if(m>0)
x++;
else
{
x--;
p+=(2*dx)-(2*dy);
}
// For Solid line
if(ch==1)
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
// For Dot-Dashed Line
if(ch==2)
{
if(count<5 && count1==5)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==5)
count1=0;
}
else
{
count1++;
if(count1==5)
count=0;
if(count1==3)
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
}
}
// For Dashed line
if(ch==3)
{
if(count<3 && count1==3)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==3)
count1=0;
}
else
{
count1++;
if(count1==3)
count=0;
}
}
if(ch==4)
{
if(count<1)
{
putpixel((x+getmaxx()/2),(getmaxy()/2-y),14);
count++;
if(count==1)
count1=0;
}
else
{
count1++;
if(count1==2)
count=0;
}
}
delay(100);
}
}
/*-------------------------------main()-------------------------------------*/
int main()
{
BRESEN b;
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
int ch;
do
{
cleardevice();
cout<<"\n----------------------------------------------------";
cout<<"\n BRESENHAM ALGORITHM ";
cout<<"\n----------------------------------------------------";
cout<<"\n 1.SOLID LINE";
cout<<"\n 2.DOT-DASHED LINE";
cout<<"\n 3.DASHED LINE";
cout<<"\n 4.DOTTED LINE";
cout<<"\n 5.EXIT";
cout<<"\n------------------------------------------------------";
cout<<"\nENTER YR CHOICE : ";
cin>>ch;
switch(ch)
{
case 1:
b.getpoint();
b.bres_line(ch);
getch();
break;
case 2:
b.getpoint();
b.bres_line(ch);
getch();
break;
case 3:
b.getpoint();
b.bres_line(ch);
getch();
break;
case 4:
b.getpoint();
b.bres_line(ch);
break;
case 5:
break;
}
}while(ch!=5);
return 0;
}
Bresenham's line drawing algorithm
void Bres_Line(int,int,int,int);
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int Xa,Ya,Xb,Yb;
clrscr();
cout<<"\nEnter The X-coordinate for first point : ";
cin>>Xa;
cout<<"\nEnter The Y-coordinate for first point : ";
cin>>Ya;
cout<<"\nEnter The X-coordinate for Second point : ";
cin>>Xb;
cout<<"\nEnter The Y-coordinate for Second point : ";
cin>>Yb;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\tc\bgi");
line((int)getmaxx()/2,0,(int)getmaxx()/2,getmaxy());
line(0,(int)getmaxy()/2,getmaxx(),(int)getmaxy()/2);
/* Line Algorithm Call */
Bres_Line(Xa,Ya,Xb,Yb);
/* clean up */
getch();
closegraph();
return 0;
}
void Bres_Line(int Xa,int Ya,int Xb,int Yb)
{
int dx,dy,x,y,Xend,Yend,p;
float m;
dx = abs(Xb - Xa);
dy = abs(Yb - Ya);
m = dy/dx;
if(dx > dy) //gentle slope
{
if(Xa > Xb)
{
x = Xb;
y = Yb;
Xend = Xa;
}
else
{
x = Xa;
y = Ya;
Xend = Xb;
}
putpixel(x+getmaxx()/2,getmaxy()/2-y,222);
p= (2*dy) - dx;
while( x < Xend )
{
x = x + 1;
if(p < 0)
{
p=p+(2*dy);
}
else
{
if(p>=0)
if(m>0)
y++;
else
y--;
p=p+(2*(dy-dx));
}
putpixel(x+getmaxx()/2,getmaxy()/2-y,222);
delay(500);
}
}
else //sharp slope
{
}
}
Subscribe to:
Comments (Atom)