Use the weekday number as an array index. Before adding dates, add one empty entry to each weekday before
start; otherwise the first date in those columns would appear a week too early. For day , the slot number is , whose remainder modulo 7 selects the column. The source's example makes clear that start counts empty weekday cells, rather than individual printed space characters.[ref]final class Column extends ArrayList<Data> {
private static final long serialVersionUID = 1L;
}
static String month(int start, int numDays) {
if (start < 0 || start >= 7 || numDays < 1 || numDays > 31)
throw new IllegalArgumentException("invalid month parameters");
String[] days = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};
int ncols = days.length;
List<Column> output = new ArrayList<Column>(ncols);
for (int c = 0; c < ncols; ++c) {
Column column = new Column();
column.add(new StringData(days[c], true));
output.add(column);
}
for (int c = 0; c < start; ++c)
output.get(c).add(new StringData("", true));
for (int d = 1; d <= numDays; ++d) {
int c = (start + d - 1) % ncols;
output.get(c).add(new IntegerData(d, false));
}
return printData(output);
}The two-character headers force every column to width two, and right justification aligns one-digit dates with the units digits of two-digit dates. Date appears in data row , below the header. For
month(1,30) the visible result isSu Mo Tu We Th Fr Sa
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 30For each sample, include both its marker and its bar. A bar's lower vertical limit is , including negative bars below the horizontal axis. Its horizontal half-width is , rather than . Its marker extends by in both directions. Therefore that sample's axis-aligned bounding box isTaking successive bounding unions includes every sample. Finally unite with to include the stipulated axes. The printed upper-corner formula has one extra closing parenthesis; both the lower-corner formula and the picture confirm the coordinatewise interpretation used here.[ref]
The following commented Java object method may be placed in a graph-view utility software class. It uses
Rect from part (a) and one iterator snapshot from part (e).static Rect barBounds(DataList data, double C, double M) {
if (!Double.isFinite(C) || !Double.isFinite(M) || C <= 0 || M <= 0)
throw new IllegalArgumentException("positive finite margins required");
List<Point> p = new ArrayList<Point>();
for (Point point : data) p.add(point);
int n = p.size();
if (n < 2) throw new IllegalArgumentException("at least two points required");
// This central square supplies the required minimum axis extents.
Rect bounds = new Rect(new Point(-M, -M), new Point(M, M));
for (int i = 0; i < n; ++i) {
double x = p.get(i).getX(), y = p.get(i).getY();
double width;
if (i == 0) {
width = p.get(1).getX() - x;
} else if (i == n - 1) {
width = x - p.get(i - 1).getX();
} else {
width = Math.min(x - p.get(i - 1).getX(),
p.get(i + 1).getX() - x);
}
// Include whichever is wider: marker or half of the bar.
double half = Math.max(C, width / 2.0);
// The bar reaches the baseline; the marker reaches y +/- C.
Rect item = new Rect(new Point(x - half, Math.min(0.0, y - C)),
new Point(x + half, Math.max(0.0, y + C)));
bounds = bounds.Union(item);
}
return bounds;
}This uses the usual assumption that coordinate differences and expanded bounds are representable as finite
double values. Rect rejects overflowed bounds; numerical range extension is not part of the requested geometry. As with all floating-point arithmetic, the calculation approximates the corresponding exact real number formulas.For an integer cell coordinate, occupancy is an existential predicate:The first occupied unit cell of brick is at , and its last is at . Thus the required support predicate, with the baseplate explicitly included, isFor width one, the two support cells coincide. For a wider brick, its middle may bridge an empty gap; the question only requires support beneath its two ends. Checking would incorrectly examine a cell beyond the brick's right end. The potentially infinite baseplate supplies every required support at height zero.[ref]