import java.awt.*; import java.applet.Applet; import java.util.*; public class RecursivePictureWorld extends PictureWorld { public void initializePictureChoices() { // Define your pictures here and in auxiliary methods. // Add your pictures to the menu via "addPictureChoice(String name, Picture pic);" addPictureChoice("tri", tri); addPictureChoice("pat", pat); addPictureChoice("di", di); addPictureChoice("kit", kit); addPictureChoice("pen", pen); addPictureChoice("hex", hex); addPictureChoice("rightPush(4, di))", rightPush(4, di)); addPictureChoice("rightPush(5, hex))", rightPush(5, hex)); addPictureChoice("upPush(4, di))", upPush(4, di)); addPictureChoice("upPush(5, hex))", upPush(5, hex)); addPictureChoice("upperRightPush(4, di))", upperRightPush(4, di)); addPictureChoice("cornerPush(4, di))", cornerPush(4, di)); addPictureChoice("cornerPush(5, di))", cornerPush(5, di)); addPictureChoice("cornerPush(5, hex))", cornerPush(5, hex)); addPictureChoice("cornerPush(5, pen))", cornerPush(5, pen)); addPictureChoice("cornerPush(5, tri))", cornerPush(5, tri)); addPictureChoice("cornerPush(5, kit))", cornerPush(5, kit)); addPictureChoice("cornerPush2(5, di))", cornerPush2(5, di)); addPictureChoice("cornerPush3(5, di))", cornerPush3(5, di)); addPictureChoice("cornerQuilt(5, di))", cornerQuilt(5, di)); addPictureChoice("cornerQuilt(5, hex))", cornerQuilt(5, hex)); addPictureChoice("cornerQuilt(5, pen))", cornerQuilt(5, pen)); addPictureChoice("cornerQuilt(5, tri))", cornerQuilt(5, tri)); addPictureChoice("cornerQuilt(5, kit))", cornerQuilt(5, kit)); addPictureChoice("cornerQuilt2Pics(5, hex, di))", cornerQuilt2Pics(5, hex, di)); addPictureChoice("cornerQuilt3Pics(5, hex, di, pen))", cornerQuilt3Pics(5, hex, di, pen)); } public Picture di = diamond(Color.red, true); public Picture hex = hexagon(Color.blue, true); public Picture kit = kite(Color.cyan, true); public Picture pat = patch(Color.yellow); public Picture pen = pent(Color.green, true); public Picture tri = tri(Color.magenta, true); public Picture rightPush (int n, Picture p) { if (n == 0) { return empty(); } else { return beside(p, rightPush(n - 1, p)); } } public Picture upPush (int n, Picture p) { if (n == 0) { return empty(); } else { return above(upPush(n - 1, p), p); } } public Picture upperRightPush (int n, Picture p) { if (n == 0) { return empty(); } else { return fourPics(empty(), upperRightPush(n - 1, p), p, empty()); } } public Picture cornerPush (int n, Picture p) { return upPush(n, rightPush(n, p)); } // There are many other ways to define cornerPush: public Picture cornerPush2 (int n, Picture p) { return rightPush(n, upPush(n, p)); } public Picture cornerPush3 (int n, Picture p) { if (n == 0) { return empty(); } else { return fourPics(upPush(n - 1, p), cornerPush3(n - 1, p), p, rightPush(n-1, p)); } } public Picture cornerQuilt (int n, Picture p) { return rotations(cornerPush(n, p)); } public Picture cornerPush2Pics (int n, Picture p1, Picture p2) { if (n == 0) { return empty(); } else { return fourPics(upPush(n - 1, p2), cornerPush2Pics(n - 1, p1, p2), p1, rightPush(n-1, p2)); } } public Picture cornerQuilt2Pics (int n, Picture p1, Picture p2) { return rotations(cornerPush2Pics(n, p1, p2)); } public Picture cornerPush3Pics (int n, Picture p1, Picture p2, Picture p3) { if (n == 0) { return empty(); } else { return fourPics(upPush2Pics(n - 1, p2, p3), cornerPush3Pics(n - 1, p1, p2, p3), p1, rightPush2Pics(n-1, p3, p2)); } } public Picture rightPush2Pics (int n, Picture p1, Picture p2) { if (n == 0) { return empty(); } else { return beside(p1, rightPush2Pics(n - 1, p2, p1)); } } public Picture upPush2Pics (int n, Picture p1, Picture p2) { if (n == 0) { return empty(); } else { return above(upPush2Pics(n - 1, p2, p1), p1); } } public Picture cornerQuilt3Pics (int n, Picture p1, Picture p2, Picture p3) { return rotations(cornerPush3Pics(n, p1, p2, p3)); } //Fill in these methods, which will be useful to you. public Picture patch_2x2 (Color c) { // Modify this to return a 2x2 patch return empty(); } public Picture triangles_2x2 (Color c1, Color c2) { // Modify this to return a 2x2 triangle return empty(); } // Auxiliary methods public Picture fourPics (Picture p1, Picture p2, Picture p3, Picture p4) { return above(beside(p1,p2),beside(p3,p4)); } public Picture fourOverlay (Picture p1, Picture p2, Picture p3, Picture p4) { return overlay(overlay(p1,p2), overlay(p3,p4)); } public Picture fourSame (Picture p) { return fourPics(p,p,p,p); } public Picture rotations (Picture p) { return fourPics(clockwise270(p),p,clockwise180(p),clockwise90(p)); } public Picture corner (Picture p1, Picture p2) { return fourPics(p2,p2,p1,p2); } // Various primitive pictures public Picture patch(Color c) { return overlay (new Rect(0.0, 0.0, 1.0, 1.0, Color.black, false), new Rect(0.0, 0.0, 1.0, 1.0, c, true)); } public Picture tri(Color c, boolean isFilled) { Poly triPoly = new Poly (c, isFilled); triPoly.addPoint(0.0, 0.0); triPoly.addPoint(0.0, 1.0); triPoly.addPoint(1.0, 0.0); return triPoly; } public Picture diamond(Color c, boolean isFilled) { Poly diamondPoly = new Poly (c, isFilled); diamondPoly.addPoint(0.5, 0.0); diamondPoly.addPoint(0.0, 0.5); diamondPoly.addPoint(0.5, 1.0); diamondPoly.addPoint(1.0, 0.5); return diamondPoly; } public Picture kite(Color c, boolean isFilled) { Poly kitePoly = new Poly (c, isFilled); kitePoly.addPoint(0.0, 0.0); kitePoly.addPoint(1.0, 0.5); kitePoly.addPoint(1.0, 1.0); kitePoly.addPoint(0.5, 1.0); return kitePoly; } public Picture pent(Color c, boolean isFilled) { Poly pentPoly = new Poly (c, isFilled); pentPoly.addPoint(0.5, 0.0); pentPoly.addPoint(0.0, 0.5); pentPoly.addPoint(0.5, 1.0); pentPoly.addPoint(1.0, 1.0); pentPoly.addPoint(1.0, 0.5); return pentPoly; } public Picture hexagon(Color c, boolean isFilled) { Poly hexPoly = new Poly (c, isFilled); hexPoly.addPoint(0.0, 0.0); hexPoly.addPoint(0.5, 0.0); hexPoly.addPoint(1.0, 0.5); hexPoly.addPoint(1.0, 1.0); hexPoly.addPoint(0.5, 1.0); hexPoly.addPoint(0.0, 0.5); return hexPoly; } public Picture wedge(Color c, boolean isFilled) { return fourPics(empty(), empty(), flipVertically(tri(c, isFilled)), empty()); } public Picture triangles(Color c1, Color c2) { return overlay (new Rect(0.0, 0.0, 1.0, 1.0, Color.black, false), overlay (new Line(0.0, 1.0, 1.0, 0.0, Color.black), overlay(tri(c1, true), clockwise180(tri(c2, true))))); } }