Guest User

Untitled

a guest
Nov 8th, 2019
83
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Sierpinski
  2. {
  3. public static ArrayList<Square> carpet(ArrayList<Square> l, int levels)
  4. {
  5. // TODO: Milestone 2
  6. if(levels < 0)
  7. throw new IllegalArgumentException();
  8.  
  9. // TODO: Milestone 3
  10. if(levels == 0)
  11. return l;
  12.  
  13. // TODO: Milestone 4
  14. ArrayList<Square> newSquares = new ArrayList<>();
  15.  
  16.  
  17.  
  18. for(Square x: l)
  19. {
  20. Square newSquare1 = new Square();
  21. Square newSquare2 = new Square();
  22. Square newSquare3 = new Square();
  23. Square newSquare4 = new Square();
  24. Square newSquare5 = new Square();
  25. Square newSquare6 = new Square();
  26. Square newSquare7 = new Square();
  27. Square newSquare8 = new Square();
  28.  
  29. if(levels > 0)
  30. {
  31. int newH = x.getHeight() / 3;
  32. int newW = x.getWidth() / 3;
  33.  
  34. newSquare1.setHeight(newH);
  35. newSquare1.setWidth(newW);
  36. newSquare2.setHeight(newH);
  37. newSquare2.setWidth(newW);
  38. newSquare3.setHeight(newH);
  39. newSquare3.setWidth(newW);
  40. newSquare4.setHeight(newH);
  41. newSquare4.setWidth(newW);
  42. newSquare5.setHeight(newH);
  43. newSquare5.setWidth(newW);
  44. newSquare6.setHeight(newH);
  45. newSquare6.setWidth(newW);
  46. newSquare7.setHeight(newH);
  47. newSquare7.setWidth(newW);
  48. newSquare8.setHeight(newH);
  49. newSquare8.setWidth(newW);
  50.  
  51. int x1 = x.getX();
  52. int x2 = x.getX() + newW;
  53. int x3 = x1 + 2 * newW;
  54.  
  55. int y1 = x.getY();
  56. int y2 = y1 + newH;
  57. int y3 = y1 + 2 * newH;
  58.  
  59. newSquare1.setX(x1);
  60. newSquare1.setY(y1);
  61. newSquares.add(newSquare1);
  62.  
  63. newSquare2.setX(x2);
  64. newSquare2.setY(y1);
  65. newSquares.add(newSquare2);
  66.  
  67. newSquare3.setX(x3);
  68. newSquare3.setY(y1);
  69. newSquares.add(newSquare3);
  70.  
  71. newSquare4.setX(x1);
  72. newSquare4.setY(y2);
  73. newSquares.add(newSquare4);
  74.  
  75. newSquare5.setX(x3);
  76. newSquare5.setY(y2);
  77. newSquares.add(newSquare5);
  78.  
  79. newSquare6.setX(x1);
  80. newSquare6.setY(y3);
  81. newSquares.add(newSquare6);
  82.  
  83. newSquare7.setX(x2);
  84. newSquare7.setY(y3);
  85. newSquares.add(newSquare7);
  86.  
  87. newSquare8.setX(x3);
  88. newSquare8.setY(y3);
  89. newSquares.add(newSquare8);
  90. }
  91.  
  92. }
  93.  
  94. return carpet(newSquares, levels - 1);
  95. }
  96. }
RAW Paste Data