import java.awt.*; import java.applet.Applet; import java.util.*; public class KnitWorld extends PictureWorld { // Making knitting patterns based on Escher's knitting primitives. // See Doris Schattshneider, "M.C. Escher: Visions of Symmetry", pp 48--49. public void initializePictureChoices() { // Define your pictures here and in auxiliary methods. // Add your pictures to the menu via "addPictureChoice(String name, Picture pic);" // Some helpful color abbreviations: Color black = Color.black; Color blue = Color.blue; Color cyan = Color.cyan; Color green = Color.green; Color magenta = Color.magenta; Color red = Color.red; Color white = Color.white; Color yellow = Color.yellow; addPictureChoice("A white", A(white,white,white,white,white)); addPictureChoice("B white", B(white,white,white,white,white)); addPictureChoice("A colors", A(red,blue,green,yellow,magenta)); addPictureChoice("B colors", B(red,blue,green,yellow,magenta)); addPictureChoice("knit1", knit1(red, yellow)); addPictureChoice("knit2", knit2(yellow, blue)); addPictureChoice("knit3", knit3(blue,red,yellow)); addPictureChoice("knit4", knit4(blue, yellow)); addPictureChoice("knit5", knit5(red, yellow, blue, green)); addPictureChoice("knit6", knit6(green, blue)); } public Picture knit1(Color c1, Color c2) { Picture A1 = A(c1, c2, c1, c1, c2); Picture B1 = B(c2, c1, c2 ,c2, c1); return tileKnit(A1,B1,A1,B1); } public Picture knit2(Color c1, Color c2) { Picture A1 = A(c1, c2, c1, c2, c1); Picture B1 = B(c2, c1, c2 ,c1, c2); return tileKnit(flipHorizontally(B1), flipHorizontally(A1), clockwise180(B1), clockwise180(A1)); } public Picture knit3(Color c1, Color c2, Color c3) { return tileKnit(B(c1,c2,c1,c3,c1), clockwise90(B(c1,c3,c2,c2,c1)), flipVertically(B(c1,c3,c1,c2,c1)), flipVertically(clockwise90(A(c1,c2,c3,c3,c1)))); } public Picture knit4(Color c1, Color c2) { Picture tile1 = fourPics(B(c1,c2,c1,c2,c1), clockwise270(A(c1,c2,c1,c1,c2)), flipVertically(B(c1,c2,c1,c2,c1)), clockwise90(flipVertically(B(c2,c1,c2,c2,c1)))); Picture tile2 = fourPics(A(c2,c1,c2,c1,c2), clockwise270(B(c2,c1,c2,c2,c1)), flipVertically(A(c2,c1,c2,c1,c2)), clockwise90(flipVertically(A(c1,c2,c1,c1,c2)))); return fourSame(fourPics(tile1, tile2, tile1, tile2)); } public Picture knit5(Color c1, Color c2, Color c3, Color c4) { // Replace this stub by your definition. return empty(); } public Picture knit6(Color c1, Color c2) { // Replace this stub by your definition. return empty(); } // Knitting auxiliary functions public Picture tileKnit (Picture p1, Picture p2, Picture p3, Picture p4) { return fourSame(fourSame(fourPics(p1, p2, p3, p4))); } public Picture fourSame (Picture p) { return fourPics(p, p, p, p); } public Picture fourPics (Picture p1, Picture p2, Picture p3, Picture p4) { return above(beside(p1,p2), beside(p3,p4)); } // Knitting primitives. You do not have to understand these in order to // do the problem set. public Picture A(Color c1, Color c2, Color c3, Color c4, Color c5) { return overlay(poly1(c1), overlay(poly2(c2), overlay(poly3(c3), overlay(poly4(c4), overlay(poly5(c5), border(Color.black)))))); } public Picture B(Color c1, Color c2, Color c3, Color c4, Color c5) { return overlay(poly3(c3), overlay(poly2(c2), overlay(poly1(c1), overlay(poly4(c4), overlay(poly5(c5), border(Color.black)))))); } public Rect border (Color c) { return new Rect (0.0, 0.0, 1.0, 1.0, c, false); } public Picture poly1(Color c) { Poly poly = new Poly (c, true); poly.addPoint(0.4, 0.0); poly.addPoint(0.6, 0.0); poly.addPoint(0.2, 1.0); poly.addPoint(0.0, 1.0); poly.addPoint(0.0, 0.8); return overlay(new Line (0.6, 0.0, 0.2, 1.0, Color.black), overlay(new Line (0.4, 0.0, 0.0, 0.8, Color.black), poly)); } public Picture poly2(Color c) { Poly poly = new Poly (c, true); poly.addPoint(0.0, 0.6); poly.addPoint(0.0, 0.4); poly.addPoint(1.0, 0.8); poly.addPoint(1.0, 1.0); poly.addPoint(0.8, 1.0); return overlay(new Line (0.0, 0.4, 1.0, 0.8, Color.black), overlay(new Line (0.0, 0.6, 0.8, 1.0, Color.black), poly)); } public Picture poly3(Color c) { Poly poly = new Poly (c, true); poly.addPoint(0.4, 1.0); poly.addPoint(1.0, 0.4); poly.addPoint(1.0, 0.6); poly.addPoint(0.6, 1.0); return overlay(new Line (0.4, 1.0, 1.0, 0.4, Color.black), overlay(new Line (0.6, 1.0, 1.0, 0.6, Color.black), poly)); } public Picture poly4(Color c) { Poly poly = new Poly (c, true); poly.addPoint(0.0, 0.0); poly.addPoint(0.2, 0.0); poly.addPoint(0.0, 0.2); return overlay(new Line (0.2, 0.0, 0.0, 0.2, Color.black), poly); } public Picture poly5(Color c) { Poly poly = new Poly (c, true); poly.addPoint(0.8, 0.0); poly.addPoint(1.0, 0.0); poly.addPoint(1.0, 0.2); return overlay(new Line (0.8, 0.0, 1.0, 0.2, Color.black), poly); } }