/** * This class draws H-trees on a SimpleCanvas * * @author Lyndon While 2018 */ public class Htree { SimpleCanvas sc; public Htree() { sc = new SimpleCanvas(); } public void draw(int n) { draw(n, 200, 200, 180); } // draw an H-tree of order n and size s centred at x,y public void draw(int n, int x, int y, int s) { if (n == 0) return; drawH(x, y, s); // compute the coordinates of the four half-size H-trees int x0 = x - s / 2; int x1 = x + s / 2; int y0 = y - s / 2; int y1 = y + s / 2; // recursively draw four half-size H-trees of order n-1 draw(n-1, x0, y0, s / 2); // lower left H-tree draw(n-1, x0, y1, s / 2); // upper left H-tree draw(n-1, x1, y0, s / 2); // lower right H-tree draw(n-1, x1, y1, s / 2); // upper right H-tree } // draw an H of size s centred at x,y public void drawH(int x, int y, int s) { // compute the coordinates of the four tips of the H int x0 = x - s / 2; int x1 = x + s / 2; int y0 = y - s / 2; int y1 = y + s / 2; // draw the three line segments of the H sc.drawLine(x0, y0, x0, y1); // left vertical segment of the H sc.drawLine(x1, y0, x1, y1); // right vertical segment of the H sc.drawLine(x0, y, x1, y); // connect the two vertical segments of the H } }