forked from yannrichet/jmathplot
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCloudPlot2D.java
More file actions
174 lines (143 loc) · 5.4 KB
/
CloudPlot2D.java
File metadata and controls
174 lines (143 loc) · 5.4 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Created on 13 juil. 07 by richet
*/
package org.math.plot.plots;
import java.awt.Color;
import javax.swing.JFrame;
import org.math.plot.FrameView;
import org.math.plot.Plot2DPanel;
import org.math.plot.PlotPanel;
import org.math.plot.render.AbstractDrawer;
import org.math.plot.utils.Array;
public class CloudPlot2D extends Plot {
double[][] NW;
double[][] NE;
double[][] SW;
double[][] SE;
double[] width_constant = {-1, -1};
double[][] XY;
float[] f;
boolean fill_shape = true;
public CloudPlot2D(String n, Color c, double[][] _XYcard, double wX, double wY) {
this(n, new Color[] { c }, _XYcard, wX, wY);
}
public CloudPlot2D(String n, Color[] c, double[][] _XYcard, double wX, double wY) {
super(n, c);
splitXYf(_XYcard);
width_constant = new double[]{wX, wY};
build();
}
private void splitXYf(double[][] xycard) {
XY = new double[xycard.length][2];
f = new float[xycard.length];
float normf = 0;
for (int i = 0; i < xycard.length; i++) {
XY[i][0] = xycard[i][0];
XY[i][1] = xycard[i][1];
f[i] = (float) xycard[i][2];
normf += f[i];//Math.max(normf, f[i]);
}
for (int i = 0; i < f.length; i++) {
f[i] = f[i] / normf;
}
}
private void build() {
if (width_constant[0] > 0) {
NW = new double[XY.length][];
NE = new double[XY.length][];
SW = new double[XY.length][];
SE = new double[XY.length][];
for (int i = 0; i < XY.length; i++) {
NW[i] = new double[]{XY[i][0] - width_constant[0] / 2, XY[i][1] + width_constant[1] / 2};
NE[i] = new double[]{XY[i][0] + width_constant[0] / 2, XY[i][1] + width_constant[1] / 2};
SW[i] = new double[]{XY[i][0] - width_constant[0] / 2, XY[i][1] - width_constant[1] / 2};
SE[i] = new double[]{XY[i][0] + width_constant[0] / 2, XY[i][1] - width_constant[1] / 2};
}
}
}
public void plot(AbstractDrawer draw, Color[] c) {
if (!visible) {
return;
}
boolean monoColor = false;
if (c.length == 1) {
monoColor = true;
}
else if (c.length != XY.length) {
throw new IllegalArgumentException("Color array length must match length of data array. ");
}
draw.canvas.includeInBounds(SW[0]);
draw.canvas.includeInBounds(NE[XY.length - 1]);
draw.setLineType(AbstractDrawer.CONTINOUS_LINE);
for (int i = 0; i < XY.length; i++) {
if (f[i] > 0) {
draw.setColor(monoColor ? c[0] : c[i]);
draw.fillPolygon(f[i], NW[i], NE[i], SE[i], SW[i]);
}
}
}
@Override
public void setData(double[][] d) {
datapanel = null;
splitXYf(d);
}
@Override
public double[][] getData() {
return XY;
}
@Override
public double[][] getBounds() {
double[][] b = new double[][]{Array.min(XY), Array.max(XY)};
for (int i = 0; i < b[0].length; i++) {
b[0][i] = b[0][i] - width_constant[i] / 2;
b[1][i] = b[1][i] + width_constant[i] / 2;
}
return b;
}
public double[] isSelected(int[] screenCoordTest, AbstractDrawer draw) {
for (int i = 0; i < XY.length; i++) {
int[] screenCoord = draw.project(XY[i]);
if ((screenCoord[0] + note_precision > screenCoordTest[0]) && (screenCoord[0] - note_precision < screenCoordTest[0])
&& (screenCoord[1] + note_precision > screenCoordTest[1]) && (screenCoord[1] - note_precision < screenCoordTest[1])) {
return XY[i];
}
}
return null;
}
public static void main(String[] args) {
Plot2DPanel p = new Plot2DPanel();
double[][] cloud = new double[100][2];
for (int i = 0; i < cloud.length; i++) {
cloud[i][0] = Math.random() + Math.random();
cloud[i][1] = Math.random() + Math.random();
}
p.addCloudPlot("cloud", Color.RED, cloud, 5, 5);
double[][] cloud2 = new double[100][2];
for (int i = 0; i < cloud2.length; i++) {
cloud2[i][0] = 2 + Math.random() + Math.random();
cloud2[i][1] = 2 + Math.random() + Math.random();
}
p.addCloudPlot("cloud2", Color.RED, cloud2, 5, 5);
p.setLegendOrientation(PlotPanel.SOUTH);
new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Color[] c = new Color[5 * 5];
for (int i = 0; i < c.length; i++) {
c[i] = new Color((int)(Math.random() * 0x1000000));
}
p = new Plot2DPanel();
cloud = new double[100][2];
for (int i = 0; i < cloud.length; i++) {
cloud[i][0] = Math.random() + Math.random();
cloud[i][1] = Math.random() + Math.random();
}
p.addCloudPlot("cloud", c, cloud, 5, 5);
cloud2 = new double[100][2];
for (int i = 0; i < cloud2.length; i++) {
cloud2[i][0] = 2 + Math.random() + Math.random();
cloud2[i][1] = 2 + Math.random() + Math.random();
}
p.addCloudPlot("cloud2", c, cloud2, 5, 5);
p.setLegendOrientation(PlotPanel.SOUTH);
new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}