└── DDA algorithm.java /DDA algorithm.java: -------------------------------------------------------------------------------- 1 | import java.awt.*; 2 | import javax.swing.*; 3 | 4 | public class DDALineDrawing extends JPanel { 5 | 6 | private int x1, y1, x2, y2; 7 | 8 | public DDALineDrawing(int x1, int y1, int x2, int y2) { 9 | this.x1 = x1; 10 | this.y1 = y1; 11 | this.x2 = x2; 12 | this.y2 = y2; 13 | } 14 | 15 | @Override 16 | protected void paintComponent(Graphics g) { 17 | super.paintComponent(g); 18 | drawLineDDA(g, x1, y1, x2, y2); 19 | } 20 | 21 | private void drawLineDDA(Graphics g, int x1, int y1, int x2, int y2) { 22 | int dx = x2 - x1; 23 | int dy = y2 - y1; 24 | 25 | int steps = Math.max(Math.abs(dx), Math.abs(dy)); 26 | 27 | float xIncrement = (float) dx / steps; 28 | float yIncrement = (float) dy / steps; 29 | 30 | float x = x1; 31 | float y = y1; 32 | 33 | for (int i = 0; i <= steps; i++) { 34 | g.fillRect(Math.round(x), Math.round(y), 1, 1); // Draw pixel 35 | x += xIncrement; 36 | y += yIncrement; 37 | } 38 | } 39 | 40 | public static void main(String[] args) { 41 | JFrame frame = new JFrame("DDA Line Drawing Algorithm"); 42 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 43 | frame.setSize(800, 800); 44 | 45 | // Example: Line from (50, 50) to (400, 300) 46 | DDALineDrawing panel = new DDALineDrawing(50, 50, 400, 300); 47 | frame.add(panel); 48 | 49 | frame.setVisible(true); 50 | } 51 | } 52 | --------------------------------------------------------------------------------