-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPotential.java
More file actions
48 lines (44 loc) · 1.56 KB
/
Copy pathPotential.java
File metadata and controls
48 lines (44 loc) · 1.56 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
/* *****************************************************************************
* Name: Ada Lovelace
* Coursera User ID: 123456
* Last modified: October 16, 1842
**************************************************************************** */
import java.awt.Color;
public class Potential {
public static Charge[] readCharges() {
int n = StdIn.readInt();
Charge[] charges = new Charge[n];
for (int i = 0; i < n; i++) {
double x0 = StdIn.readDouble();
double y0 = StdIn.readDouble();
double q0 = StdIn.readDouble();
charges[i] = new Charge(x0, y0, q0);
}
return charges;
}
public static Color toColor(double val) {
val = 128 + val / 2.0e10;
int i = 0;
if (val > 255) i = 255;
else if (val >= 0) i = (int) val;
// i = i * 37 % 255;
return new Color(i, i, i);
}
public static void main(String[] args) {
Charge[] charges = readCharges();
int size = 800;// StdIn.readInt();
Picture pic = new Picture(size, size);
for (int col = 0; col < size; col++) {
for (int row = 0; row < size; row++) {
double val = 0.0;
for (int k = 0; k < charges.length; k++) {
double x = 1.0 * col / size;
double y = 1.0 * row / size;
val += charges[k].potentialAt(x, y);
}
pic.set(col, size - 1 - row, toColor(val));
}
}
pic.show();
}
}