forked from gmjonker/util
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextPlot.java
More file actions
132 lines (123 loc) · 4.6 KB
/
TextPlot.java
File metadata and controls
132 lines (123 loc) · 4.6 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
package gmjonker.math;
import java.util.Arrays;
import java.util.function.Function;
public class TextPlot
{
/**
* Plots a function.
*
* <p>If there is a point in the function that interests you particularly, consider choosing xmin/xmax and/or ymin/ymax
* and width and height such that this point falls on an ascii grid point.
*/
public static void plotf(Function<Double, Double> f, double xmin, double xmax, double ymin, double ymax,
Integer width, Integer height)
{
if (width == null || width == 0) width = 40;
if (height == null || height == 0) height = 20;
char[][] points = new char[width][height + 1];
for (int v = 0; v < width; v++)
Arrays.fill(points[v], ' ');
for (int v = 0; v < width; v++) {
double x = xmin + 1.0 * v / width * (xmax - xmin);
double y = f.apply(x);
int yIndex = height - (int) Math.round((y - ymin) / (ymax - ymin) * height);
if (yIndex >= 0 && yIndex <= height)
points[v][yIndex] = '+';
}
for (int w = 0; w < height + 1; w++) {
for (int v = 0; v < width; v++) {
System.out.print(points[v][w]);
}
System.out.println();
}
}
/**
* Plots a number of y values next to each other.
*/
public static void plotValues(double[] yValues, Integer height)
{
if (height == null)
height = 30;
int width = yValues.length;
// in x/y form, or column/row
char[][] points = new char[width][height + 1];
double ymin = Double.MAX_VALUE;
double ymax = Double.MIN_VALUE;
for (int v = 0; v < yValues.length; v++) {
double y = yValues[v];
ymin = Math.min(ymin, y);
ymax = Math.max(ymax, y);
Arrays.fill(points[v], ' ');
}
for (int v = 0; v < yValues.length; v++) {
double y = yValues[v];
int yIndex = height - (int) Math.round((y - ymin) / (ymax - ymin) * height);
points[v][yIndex] = '+';
}
for (int w = 0; w < height + 1; w++) {
for (int v = 0; v < width; v++) {
System.out.print(points[v][w]);
}
System.out.println();
}
}
/**
* Plots a number of y values next to each other.
*/
public static void plotValues(int[] yValues, Integer height)
{
double[] yv = new double[yValues.length];
for (int i = 0; i < yValues.length; i++)
yv[i] = yValues[i];
plotValues(yv, height);
}
/**
* Plots a number of x/y points. Origin is top left corner.
*/
public static void plotPoints(double[] xs, double[] ys, Integer width, Integer height)
{
plotPoints(xs, ys, width, height, false, false);
}
/**
* Plots a number of x/y points. Origin is top left corner.
*/
public static void plotPoints(double[] xs, double[] ys, Integer width, Integer height, boolean flipX, boolean flipY)
{
if (width == 0) width = 40;
if (height == 0) height = 40;
double xmin = Double.MAX_VALUE;
double xmax = Double.MIN_VALUE;
double ymin = Double.MAX_VALUE;
double ymax = Double.MIN_VALUE;
for (int i = 0; i < xs.length; i++) {
xmin = Math.min(xmin, xs[i]);
xmax = Math.max(xmax, xs[i]);
ymin = Math.min(ymin, ys[i]);
ymax = Math.max(ymax, ys[i]);
}
char[][] points = new char[width + 1][height + 1];
for (int v = 0; v < width + 1; v++)
Arrays.fill(points[v], ' ');
for (int i = 0; i < xs.length; i++) {
double x = xs[i];
double y = ys[i];
int v = flipX ? (int) Math.round(width - ((x - xmin) / (xmax - xmin) * width))
: (int) Math.round((x - xmin) / (xmax - xmin) * width);
int w = flipY ? (int) Math.round(height - ((y - ymin) / (ymax - ymin) * height))
: (int) Math.round((y - ymin) / (ymax - ymin) * height);
switch (points[v][w]) {
case ' ': points[v][w] = '░'; break;
case '░': points[v][w] = '▒'; break;
case '▒': points[v][w] = '▓'; break;
case '▓': points[v][w] = '▓'; break;
default: points[v][w] = '?'; break;
}
}
for (int w = 0; w < height + 1; w++) {
for (int v = 0; v < width + 1; v++) {
System.out.print(points[v][w]);
}
System.out.println();
}
}
}