Skip to content

Commit fcc15a7

Browse files
committed
use double for plot data points & more math
1 parent 20e9a1c commit fcc15a7

File tree

12 files changed

+651
-89
lines changed

12 files changed

+651
-89
lines changed

src/main/java/com/cleanroommc/modularui/drawable/graph/AutoMajorTickFinder.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public class AutoMajorTickFinder implements MajorTickFinder {
77

88
private final boolean autoAdjust;
9-
private float multiple = 10;
9+
private double multiple = 10;
1010

1111
public AutoMajorTickFinder(boolean autoAdjust) {
1212
this.autoAdjust = autoAdjust;
@@ -18,10 +18,10 @@ public AutoMajorTickFinder(float multiple) {
1818
}
1919

2020
@Override
21-
public float[] find(float min, float max, float[] ticks) {
21+
public double[] find(double min, double max, double[] ticks) {
2222
int s = (int) Math.ceil((max - min) / multiple) + 2;
23-
if (s > ticks.length) ticks = new float[s];
24-
float next = (float) (Math.floor(min / multiple) * multiple);
23+
if (s > ticks.length) ticks = new double[s];
24+
double next = Math.floor(min / multiple) * multiple;
2525
for (int i = 0; i < s; i++) {
2626
ticks[i] = next;
2727
if (next > max) {
@@ -34,17 +34,17 @@ public float[] find(float min, float max, float[] ticks) {
3434
return ticks;
3535
}
3636

37-
void calculateAutoTickMultiple(float min, float max) {
38-
float step = (max - min) / 5;
37+
void calculateAutoTickMultiple(double min, double max) {
38+
double step = (max - min) / 5;
3939
if (step < 1) {
4040
int significantPlaces = (int) Math.abs(Math.log10(step)) + 2;
41-
float ten = (float) Math.pow(10, significantPlaces);
41+
double ten = Math.pow(10, significantPlaces);
4242
step = (int) (step * ten + 0.2f) / ten;
4343
} else if (step == 1) {
4444
step = 0.2f;
4545
} else {
4646
int significantPlaces = (int) Math.log10(step) - 1;
47-
float ten = (float) Math.pow(10, significantPlaces);
47+
double ten = Math.pow(10, significantPlaces);
4848
step = (int) (step / ten + 0.2f) * ten;
4949
}
5050
setMultiple(step);
@@ -54,7 +54,7 @@ public boolean isAutoAdjust() {
5454
return autoAdjust;
5555
}
5656

57-
public void setMultiple(float multiple) {
57+
public void setMultiple(double multiple) {
5858
this.multiple = multiple;
5959
}
6060
}

src/main/java/com/cleanroommc/modularui/drawable/graph/AutoMinorTickFinder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ public AutoMinorTickFinder(int amountBetweenMajors) {
1212
}
1313

1414
@Override
15-
public float[] find(float min, float max, float[] majorTicks, float[] ticks) {
15+
public double[] find(double min, double max, double[] majorTicks, double[] ticks) {
1616
int s = majorTicks.length * this.amountBetweenMajors;
17-
if (ticks.length < s) ticks = new float[s];
17+
if (ticks.length < s) ticks = new double[s];
1818
int k = 0;
1919
for (int i = 0; i < majorTicks.length - 1; i++) {
20-
if (Float.isNaN(majorTicks[i + 1])) break;
21-
float next = majorTicks[i];
22-
float d = (majorTicks[i + 1] - next) / (amountBetweenMajors + 1);
20+
if (Double.isNaN(majorTicks[i + 1])) break;
21+
double next = majorTicks[i];
22+
double d = (majorTicks[i + 1] - next) / (amountBetweenMajors + 1);
2323
for (int j = 0; j < amountBetweenMajors; j++) {
2424
next += d;
2525
if (next >= min) ticks[k++] = next;

src/main/java/com/cleanroommc/modularui/drawable/graph/GraphAxis.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.cleanroommc.modularui.drawable.GuiDraw;
55
import com.cleanroommc.modularui.drawable.text.TextRenderer;
66
import com.cleanroommc.modularui.utils.Alignment;
7-
import com.cleanroommc.modularui.utils.FloatArrayMath;
7+
import com.cleanroommc.modularui.utils.DAM;
88

99
import net.minecraft.client.renderer.BufferBuilder;
1010

@@ -24,14 +24,14 @@ public class GraphAxis {
2424

2525
public final GuiAxis axis;
2626

27-
public float[] majorTicks = new float[8];
28-
public float[] minorTicks = new float[16];
27+
public double[] majorTicks = new double[8];
28+
public double[] minorTicks = new double[16];
2929
public TextRenderer.Line[] tickLabels = new TextRenderer.Line[8];
3030
private float maxLabelWidth = 0;
3131
public MajorTickFinder majorTickFinder = new AutoMajorTickFinder(true);
3232
public MinorTickFinder minorTickFinder = new AutoMinorTickFinder(2);
3333
public String label;
34-
public float min, max;
34+
public double min, max;
3535
public boolean autoLimits = true;
3636

3737
public GraphAxis(GuiAxis axis) {
@@ -44,21 +44,21 @@ void compute(List<Plot> plots) {
4444
this.min = 0;
4545
this.max = 0;
4646
} else if (plots.size() == 1) {
47-
this.min = FloatArrayMath.min(plots.get(0).getData(this.axis));
48-
this.max = FloatArrayMath.max(plots.get(0).getData(this.axis));
47+
this.min = DAM.min(plots.get(0).getData(this.axis));
48+
this.max = DAM.max(plots.get(0).getData(this.axis));
4949
} else {
50-
float min = Float.MAX_VALUE, max = Float.MIN_VALUE;
50+
double min = Double.MAX_VALUE, max = Double.MIN_VALUE;
5151
for (Plot plot : plots) {
52-
float m = FloatArrayMath.min(plot.getData(this.axis));
52+
double m = DAM.min(plot.getData(this.axis));
5353
if (m < min) min = m;
54-
m = FloatArrayMath.max(plot.getData(this.axis));
54+
m = DAM.max(plot.getData(this.axis));
5555
if (m > max) max = m;
5656
}
5757
this.min = min;
5858
this.max = max;
5959
}
6060
if (this.axis.isVertical()) {
61-
float padding = (this.max - this.min) * 0.05f;
61+
double padding = (this.max - this.min) * 0.05f;
6262
this.max += padding;
6363
this.min -= padding;
6464
}
@@ -74,12 +74,12 @@ void compute(List<Plot> plots) {
7474
}
7575
textRenderer.setScale(TICK_LABEL_SCALE);
7676
this.maxLabelWidth = 0;
77-
float maxDiff = FloatArrayMath.max(FloatArrayMath.diff(this.majorTicks));
77+
double maxDiff = DAM.max(DAM.diff(this.majorTicks));
7878
int significantPlaces = (int) Math.abs(Math.log10(maxDiff)) + 2;
7979
DecimalFormat format = new DecimalFormat();
8080
format.setMaximumFractionDigits(significantPlaces);
8181
for (int i = 0; i < this.tickLabels.length; i++) {
82-
if (Float.isNaN(this.majorTicks[i])) break;
82+
if (Double.isNaN(this.majorTicks[i])) break;
8383
this.tickLabels[i] = textRenderer.line(format.format(this.majorTicks[i]));
8484
if (this.tickLabels[i].getWidth() > this.maxLabelWidth) {
8585
this.maxLabelWidth = this.tickLabels[i].getWidth();
@@ -106,7 +106,7 @@ void applyPadding(GraphView graphView) {
106106
}
107107

108108
void drawGridLines(BufferBuilder buffer, GraphView view, GraphAxis other, boolean major, float d, int r, int g, int b, int a) {
109-
float[] pos = major ? this.majorTicks : this.minorTicks;
109+
double[] pos = major ? this.majorTicks : this.minorTicks;
110110
float dHalf = d / 2;
111111
if (axis.isHorizontal()) {
112112
float otherMin = view.g2sY(other.max);
@@ -120,7 +120,7 @@ void drawGridLines(BufferBuilder buffer, GraphView view, GraphAxis other, boolea
120120
}
121121

122122
void drawTicks(BufferBuilder buffer, GraphView view, GraphAxis other, boolean major, float thickness, float length, int r, int g, int b, int a) {
123-
float[] pos = major ? this.majorTicks : this.minorTicks;
123+
double[] pos = major ? this.majorTicks : this.minorTicks;
124124
float dHalf = thickness / 2;
125125
if (axis.isHorizontal()) {
126126
float otherMin = view.g2sY(other.min);
@@ -131,30 +131,30 @@ void drawTicks(BufferBuilder buffer, GraphView view, GraphAxis other, boolean ma
131131
}
132132
}
133133

134-
private void drawLinesOnHorizontal(BufferBuilder buffer, GraphView view, float[] pos, float dHalf,
134+
private void drawLinesOnHorizontal(BufferBuilder buffer, GraphView view, double[] pos, float dHalf,
135135
float crossLow, float crossHigh, int r, int g, int b, int a) {
136-
for (float p : pos) {
137-
if (Float.isNaN(p)) break;
136+
for (double p : pos) {
137+
if (Double.isNaN(p)) break;
138138
if (p < min || p > max) continue;
139139

140-
p = view.g2sX(p);
140+
float fp = view.g2sX(p);
141141

142-
float x0 = p - dHalf;
143-
float x1 = p + dHalf;
142+
float x0 = fp - dHalf;
143+
float x1 = fp + dHalf;
144144
GuiDraw.drawRectRaw(buffer, x0, crossLow, x1, crossHigh, r, g, b, a);
145145
}
146146
}
147147

148-
private void drawLinesOnVertical(BufferBuilder buffer, GraphView view, float[] pos, float dHalf,
148+
private void drawLinesOnVertical(BufferBuilder buffer, GraphView view, double[] pos, float dHalf,
149149
float crossLow, float crossHigh, int r, int g, int b, int a) {
150-
for (float p : pos) {
151-
if (Float.isNaN(p)) break;
150+
for (double p : pos) {
151+
if (Double.isNaN(p)) break;
152152
if (p < min || p > max) continue;
153153

154-
p = view.g2sY(p);
154+
float fp = view.g2sY(p);
155155

156-
float y0 = p - dHalf;
157-
float y1 = p + dHalf;
156+
float y0 = fp - dHalf;
157+
float y1 = fp + dHalf;
158158
GuiDraw.drawRectRaw(buffer, crossLow, y0, crossHigh, y1, r, g, b, a);
159159
}
160160
}
@@ -166,8 +166,8 @@ void drawLabels(GraphView view, GraphAxis other) {
166166
textRenderer.setAlignment(Alignment.TopCenter, 100);
167167
float y = view.g2sY(other.min) + TICK_LABEL_OFFSET;
168168
for (int i = 0; i < this.majorTicks.length; i++) {
169-
float pos = this.majorTicks[i];
170-
if (Float.isNaN(pos)) break;
169+
double pos = this.majorTicks[i];
170+
if (Double.isNaN(pos)) break;
171171
if (pos < min || pos > max) continue;
172172
textRenderer.setPos((int) (view.g2sX(pos) - 50), (int) y);
173173
textRenderer.draw(this.tickLabels[i].getText());
@@ -177,8 +177,8 @@ void drawLabels(GraphView view, GraphAxis other) {
177177
textRenderer.setAlignment(Alignment.CenterRight, this.maxLabelWidth, 20);
178178
float x = view.g2sX(other.min) - TICK_LABEL_OFFSET - this.maxLabelWidth;
179179
for (int i = 0; i < this.majorTicks.length; i++) {
180-
float pos = this.majorTicks[i];
181-
if (Float.isNaN(pos)) break;
180+
double pos = this.majorTicks[i];
181+
if (Double.isNaN(pos)) break;
182182
if (pos < min || pos > max) continue;
183183
textRenderer.setPos((int) x, (int) (view.g2sY(pos) - 10));
184184
textRenderer.draw(this.tickLabels[i].getText());
@@ -190,11 +190,11 @@ public GuiAxis getAxis() {
190190
return axis;
191191
}
192192

193-
public float getMax() {
193+
public double getMax() {
194194
return max;
195195
}
196196

197-
public float getMin() {
197+
public double getMin() {
198198
return min;
199199
}
200200

src/main/java/com/cleanroommc/modularui/drawable/graph/GraphDrawable.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public class GraphDrawable implements IDrawable {
2121
//private IDrawable background;
2222
private int backgroundColor = Color.WHITE.main;
2323

24-
private boolean borderTop = true, borderLeft = true, borderBottom = false, borderRight = false;
2524
private float majorTickThickness = 1f, majorTickLength = 3f, minorTickThickness = 0.5f, minorTickLength = 1.5f;
2625
private float gridLineWidth = 0.5f;
2726
private int gridLineColor = Color.withAlpha(Color.BLACK.main, 0.4f);
@@ -188,23 +187,23 @@ public GraphDrawable backgroundColor(int color) {
188187
return this;
189188
}
190189

191-
public GraphDrawable plot(float[] x, float[] y) {
190+
public GraphDrawable plot(double[] x, double[] y) {
192191
return plot(new Plot().data(x, y));
193192
}
194193

195-
public GraphDrawable plot(float[] x, float[] y, int color) {
194+
public GraphDrawable plot(double[] x, double[] y, int color) {
196195
return plot(new Plot()
197196
.data(x, y)
198197
.color(color));
199198
}
200199

201-
public GraphDrawable plot(float[] x, float[] y, float thickness) {
200+
public GraphDrawable plot(double[] x, double[] y, float thickness) {
202201
return plot(new Plot()
203202
.data(x, y)
204203
.thickness(thickness));
205204
}
206205

207-
public GraphDrawable plot(float[] x, float[] y, float thickness, int color) {
206+
public GraphDrawable plot(double[] x, double[] y, float thickness, int color) {
208207
return plot(new Plot()
209208
.data(x, y)
210209
.thickness(thickness)
@@ -213,6 +212,7 @@ public GraphDrawable plot(float[] x, float[] y, float thickness, int color) {
213212

214213
public GraphDrawable plot(Plot plot) {
215214
this.plots.add(plot);
215+
plot.redraw();
216216
return this;
217217
}
218218

0 commit comments

Comments
 (0)