What type of variable layout do you use for rectangles and 3D boxes? I can think of three structures. The choice is probably pretty unimportant, but I’m curious what other people use, even if just out of habit and not for any practical reason. Methods are shown to demonstrate the differences, but I would probably work with public variables and no methods in practice. (To avoid creating extra classes or objects.) It should be obvious what the omitted methods do and how they extend to three dimensions.
private double left, top, width, height;
public double getRight() { return left + width; }
public void move(double dx, double dy) { left += dx; right += dy; }
public double getCenterY() { return top + height / 2; }
private double centerX, centerY, halfWidth, halfHeight;
public double getRight() { return centerX + halfWidth; }
public void move(double dx, double dy) { centerX += dx; centerY += dy; }
public double getCenterY() { return centerY; }
private double left, top, right, bottom;
public double getRight() { return right; }
public void move(double dx, double dy) { left += dx; right += dx; top += dy; bottom += dy; }
public double getCenterY() { return (top + bottom) / 2.0;
I can’t recall ever actually using the second version, but it seems the most convenient…