import javax.swing.JFrame;
public class MyDrawLine { public static void main(String args[]){ // dda(3,1,45,14); // midpointLine(3,1,45,14); // bresenham(3,1,45,14); bresenham2(3,1,45,14); } public static void dda(int x1,int y1,int x2,int y2){ JFrame frame=new JFrame(); TwoDimen env=new TwoDimen(); frame.getContentPane().add(env); frame.setBounds(100, 100, 600, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setVisible(true); int k,i; float x,y,dx,dy; k=Math.abs(x2-x1); if(Math.abs(y2-y1)>k){ k=Math.abs(y2-y1); } dx=(float)(x2-x1)/k; dy=(float)(y2-y1)/k; x=(float)x1; y=(float)y1; for(i=0;i<k;i++){ env.drawPoint((int)(x+0.5), (int)(y+0.5)); x=x+dx; y=y+dy; } } public static void midpointLine(int xs,int ys,int xe,int ye){ JFrame frame=new JFrame(); TwoDimen env=new TwoDimen(); frame.getContentPane().add(env); frame.setBounds(100, 100, 600, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setVisible(true); int a,b,dt1,dt2,d,x,y; a=ys-ye; b=xe-xs; d=2*a+b; dt1=2*a; dt2=2*a+2*b; x=xs; y=ys; env.drawPoint(x, y); while(x<xe){ if(d<0){ x++; y++; d=d+dt2; } else{ x++; d=d+dt1; } env.drawPoint(x, y); } } public static void bresenham(int xs,int ys,int xe,int ye){ JFrame frame=new JFrame(); TwoDimen env=new TwoDimen(); frame.getContentPane().add(env); frame.setBounds(100, 100, 600, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setVisible(true); int dx,dy,i,x,y; double m,e; dy=ye-ys; dx=xe-xs; m=(double)dy/(double)dx; e=m-0.5; x=xs; y=ys; for(i=0;i<dx;i++){ env.drawPoint(x,y); if(e>=0){ y=y+1;e=e-1; } x=x+1;e=e+m; } } public static void bresenham2(int xs,int ys,int xe,int ye){ JFrame frame=new JFrame(); TwoDimen env=new TwoDimen(); frame.getContentPane().add(env); frame.setBounds(100, 100, 600, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setVisible(true); int dx,dy,i,x,y; double m,e; dy=ye-ys; dx=xe-xs; m=(double)dy/(double)dx; e=2*m-0.5; x=xs; y=ys; for(i=0;i<dx;i++){ if(e+0.5<0.25*2){ env.drawPoint(x+1, y); env.drawPoint(x+2, y); } else if(e+0.5<0.5*2){ env.drawPoint(x+1,y); env.drawPoint(x+2,y+1); } else if(e+0.5<0.75*2){ env.drawPoint(x+1,y+1); env.drawPoint(x+2,y+1); } else{ env.drawPoint(x+1, y+1); env.drawPoint(x+2, y+2); } if(e>1){ y=y+2;e=e-2; }else if(e>0){ y=y+1;e=e-1; }else{ y=y;e=e; } x=x+1;e=e+m; } } }
class TwoDimen extends JPanel { private boolean[][] data; public TwoDimen() { data = new boolean[100][100]; initialData(data); this.setBounds(100, 100, 500, 500); this.setVisible(true); }
public void initialData(boolean[][] data) { for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[i].length; j++) { data[i][j] = false; } } repaint(); } public void drawPoint(int i,int j){ data[i+49][50-j]=true; repaint(); } @Override public void paint(Graphics g) { // TODO Auto-generated method stub super.paint(g); g.drawLine(0, 250, 500, 250); g.drawLine(250, 0, 250, 500); for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[i].length; j++) { if (data[i][j]) { g.fillRect(5 * i, 5 * j, 5, 5); } } } // g.fillRect(100, 100, 5, 5); } }