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 package ffx.numerics.clustering;
39
40 import ffx.numerics.clustering.visualization.DendrogramPanel;
41 import ffx.utilities.FFXTest;
42
43 import javax.swing.*;
44 import java.awt.*;
45 import java.io.BufferedReader;
46 import java.io.IOException;
47 import java.io.InputStreamReader;
48 import java.util.ArrayList;
49 import java.util.List;
50
51
52
53
54 public class CoordTest extends FFXTest {
55
56 private static Cluster importCluster() throws IOException {
57 List<Coord> coords = readCoordinates();
58
59 double[][] distances = new double[coords.size()][coords.size()];
60 String[] names = new String[coords.size()];
61 for (int row = 0; row < coords.size(); row++) {
62 Coord coord1 = coords.get(row);
63 for (int col = row+1; col < coords.size(); col++) {
64 Coord coord2 = coords.get(col);
65 double d = Math.sqrt(Math.pow(coord2.getX()-coord1.getX(), 2)+ Math.pow(coord2.getY()-coord1.getY(), 2));
66 distances[row][col] = d;
67 distances[col][row] = d;
68 }
69 names[row] = ""+row;
70 }
71 ClusteringAlgorithm alg = new DefaultClusteringAlgorithm();
72 Cluster cluster = alg.performClustering(distances, names,
73 new AverageLinkageStrategy());
74 return cluster;
75 }
76
77
78 public static void main(String[] args) throws Exception {
79 JFrame frame = new JFrame();
80 frame.setSize(1024, 768);
81 frame.setLocation(400, 300);
82 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
83
84 JPanel content = new JPanel();
85 DendrogramPanel dp = new DendrogramPanel();
86
87 frame.setContentPane(content);
88 content.setBackground(Color.red);
89 content.setLayout(new BorderLayout());
90 content.add(dp, BorderLayout.CENTER);
91 dp.setBackground(Color.WHITE);
92 dp.setLineColor(Color.BLACK);
93 dp.setScaleValueDecimals(0);
94 dp.setScaleValueInterval(1);
95 dp.setShowDistances(false);
96
97 Cluster cluster = importCluster();
98 dp.setModel(cluster);
99 frame.setVisible(true);
100 }
101
102 private static List<Coord> readCoordinates() throws IOException {
103 List<Coord> coordList = new ArrayList<Coord>();
104 BufferedReader br = new BufferedReader(new InputStreamReader(CoordTest.class.getResourceAsStream("/testData1.txt")));
105 String line;
106
107 while ((line = br.readLine()) != null) {
108 String[] elems = line.split(" ");
109 if (elems.length != 2) {
110 continue;
111 }
112 int x;
113 int y;
114
115 try {
116 x = Integer.parseInt(elems[0]);
117 y = Integer.parseInt(elems[1]);
118 } catch (Exception e) {
119 continue;
120 }
121 coordList.add(new Coord(x, y));
122 }
123 return coordList;
124 }
125
126 public static class Coord {
127 private double x;
128 private double y;
129 public Coord(double x, double y) {
130 this.x = x;
131 this.y = y;
132 }
133
134 public double getX() {
135 return x;
136 }
137
138 public double getY() {
139 return y;
140 }
141 }
142 }