Advertisement

java画直线算法 DDA 计算机图形学

阅读量:

设(x1,y1)和(x2,y2)分别为所求直线的起点和终点坐标,由直线的微分方程得

|

|= m =直线的斜率|(2-1)|
|---|---|---|

可通过计算由x方向的增量△x引起y的改变来生成直线:

xi+1=xi+△x (2-2)
yi+1=yi+△y=yi+△x·m (2-3)

也可通过计算由y方向的增量△y引起x的改变来生成直线:

yi+1=yi+△y (2-4)
xi+1=xi+△x=xi+△y/m (2-5)
复制代码
 import java.awt.event.*;

    
 import javax.swing.*;
    
 import java.awt.*;
    
  
    
 public class paint extends JFrame implements ActionListener {
    
 	JButton btn;
    
 	JLabel lbl1, lbl2, lbl3, lbl4;
    
 	JTextField t1, t2, t3, t4;
    
 	JPanel p1, p2, p3, p4;
    
  
    
 	public paint() {
    
 		super("DDA算法画直线");
    
 		btn = new JButton("确定");
    
 		btn.addActionListener(this);
    
 		lbl1 = new JLabel("输入起点横坐标:");
    
 		lbl2 = new JLabel("输入起点纵坐标:");
    
 		lbl3 = new JLabel("输入终点横坐标:");
    
 		lbl4 = new JLabel("输入终点纵坐标:");
    
 		t1 = new JTextField(3);
    
 		t2 = new JTextField(3);
    
 		t3 = new JTextField(3);
    
 		t4 = new JTextField(3);
    
 		t1.addActionListener(this);
    
 		t2.addActionListener(this);
    
 		t3.addActionListener(this);
    
 		t4.addActionListener(this);
    
 		p1 = new JPanel();
    
 		p1.setLayout(new GridLayout(2, 2));
    
 		p1.add(lbl1);
    
 		p1.add(t1);
    
 		p1.add(lbl2);
    
 		p1.add(t2);
    
 		p1.add(lbl3);
    
 		p1.add(t3);
    
 		p1.add(lbl4);
    
 		p1.add(t4);
    
 		this.add(p1, "South");
    
 		p2 = new JPanel();
    
 		p2.setLayout(new BorderLayout());
    
 		p2.add(btn);
    
 		btn.setActionCommand("确定");
    
 		this.add(p2, "North");
    
 		this.setBackground(Color.white);
    
 	}
    
  
    
 	public void DDAdrawline(Graphics g) {
    
 		int x0, x1, y0, y1;
    
 		String s1, s2, s3, s4;
    
 		float dx, dy, k, x,y;
    
 		s1 = t1.getText();
    
 		s2 = t2.getText();
    
 		s3 = t3.getText();
    
 		s4 = t4.getText();
    
 		x0 = Integer.parseInt(s1);
    
 		y0 = Integer.parseInt(s2);
    
 		x1 = Integer.parseInt(s3);
    
 		y1 = Integer.parseInt(s4);
    
 		dy = y1 - y0;
    
 		dx = x1 - x0;
    
 		if (dx != 0) {
    
 			k = dy / dx;
    
 			if(k<1){
    
 				y = y0;
    
 				for (int xx = x0; xx <= x1; xx++) {
    
 					g.drawString(".", xx, (int) (y + 0.5f));
    
 					y = y + k;
    
 				}
    
 			}else{
    
 				System.out.println(k+"!!!!!!!!!!!!");
    
 				x = x0;
    
 				for(int yy=y0;yy<y1;yy++){
    
 					g.drawString(".", (int)(x),  yy);
    
  
    
 					int lastY = yy;
    
 					x = x + dx/dy;
    
 				}
    
 			}
    
 		} else {
    
 			for (int i = y0; i <= y1; i++) {
    
 				g.drawString(".", x0, i);
    
 			}
    
 		}
    
 	}
    
  
    
 	public void actionPerformed(ActionEvent e) {
    
 		String s = e.getActionCommand();
    
 		if (s.equals("确定")) {
    
 			Graphics g = this.getGraphics();
    
 			g.clearRect(0, 0, 500, 500);
    
 			DDAdrawline(g);
    
 		}
    
 	}
    
  
    
 	public static void main(String[] args) {
    
 		paint frame = new paint();
    
 		frame.setSize(600, 600);
    
 		frame.setLocation(450, 200);
    
 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
 		frame.setVisible(true);
    
 	}
    
 }

全部评论 (0)

还没有任何评论哟~