forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDDA_Algorithm.java
More file actions
75 lines (61 loc) · 1.49 KB
/
DDA_Algorithm.java
File metadata and controls
75 lines (61 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
author: kunal-kushwaha
date: 25-03-2019
*/
import java.awt.*;
import javax.swing.JFrame;
import java.lang.*;
import java.util.Scanner;
public class DDA_Algorithm extends Canvas {
static int x0, y0, x1, y1;
DDA_Algorithm(int x0, int y0, int x1, int y1) {
// initialising coordinates
this.x0 = x0;
this.y0 = y0;
this.x1 = x1;
this.y1 = y1;
}
public void paint(Graphics graphics) {
int disx, disy;
float X, Y, x, y, counter;
disy = y1 - y0;
disx = x1 - x0;
graphics.fillOval(x0, y0, 10, 10);
if (disy > disx) {
counter = Math.abs(disy);
} else {
counter = Math.abs(disx);
}
x = x0;
X = disx / counter;
y = y0;
Y = disy / counter;
while (counter-- != 0) {
x = x + X;
y = y + Y;
graphics.fillOval(Math.round(x), Math.round(y), 10, 10);
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first coordinates (x0,y0) : ");
int x0 = sc.nextInt();
int y0 = sc.nextInt();
System.out.println("Enter last coordinates (x1,y1) : ");
int x1 = sc.nextInt();
int y1 = sc.nextInt();
DDA_Algorithm dda_algorithm = new DDA_Algorithm(x0, y0, x1, y1);
JFrame frame = new JFrame();
frame.add(dda_algorithm);
frame.setSize(1000, 1000);
frame.setVisible(true);
}
}
/*
OUTPUT :
Enter first coordinates (x0,y0) :
45 30
Enter last coordinates (x1,y1) :
550 620
output : https://ibb.co/Vt3jR8c
*/