diff --git a/shell.nix b/shell.nix index 946a87b..73be315 100644 --- a/shell.nix +++ b/shell.nix @@ -5,6 +5,7 @@ pkgs.mkShell rec { buildInputs = with pkgs; [ jdk typst + tinymist ]; nativeBuildInputs = with pkgs; [ jre diff --git a/week7/PlasmaClouds.java b/week7/PlasmaClouds.java new file mode 100644 index 0000000..f59fd75 --- /dev/null +++ b/week7/PlasmaClouds.java @@ -0,0 +1,33 @@ +package week7; + +import common.StdDraw; +import common.StdRandom; + +public class PlasmaClouds { + public static void main(String[] args) { + var hurst = Double.parseDouble(args[0]); + var s = Math.pow(2, 2 * hurst); + var variance = 0.01; + + StdDraw.enableDoubleBuffering(); + + draw(.5, .5, 1, 1, variance, s); + + StdDraw.show(); + } + + public static void draw(double x, double y, double halfSize, double color, double var, double s) { + if (halfSize < 0.01) { + StdDraw.setPenColor((int) (color * 255), (int) (color * 255), 255); + StdDraw.filledRectangle(x, y, halfSize, halfSize); + return; + } + var newColor = Math.clamp(StdRandom.gaussian(color, Math.sqrt(var)), 0, 1); + var newVar = var / s; + var newHalfSize = halfSize / 2; + int[][] offsets = { { 1, 1 }, { -1, 1 }, { -1, -1 }, { 1, -1 } }; + for (var offset : offsets) { + draw(x + newHalfSize * offset[0], y + newHalfSize * offset[1], newHalfSize, newColor, newVar, s); + } + } +} diff --git a/week7/Squares.java b/week7/Squares.java new file mode 100644 index 0000000..e82437c --- /dev/null +++ b/week7/Squares.java @@ -0,0 +1,56 @@ +package week7; + +import common.StdDraw; + +class Squares { + public static void main(String[] args) { + StdDraw.enableDoubleBuffering(); + StdDraw.setCanvasSize(512 * 4, 512); + StdDraw.setXscale(0, 4); + drawSquares(0.5, 0.5, .25, 4, 1); + drawSquares(1.5, 0.5, .25, 4, 2); + drawSquares(2.5, 0.5, .25, 4, 3); + drawSquares(3.5, 0.5, .25, 4, 4); + StdDraw.show(); + } + + public static void drawSquares( + double x, + double y, + double halfLength, + int limit, + int mode) { + var shouldRecurse = limit > 0; + limit--; + var newHalfLength = halfLength / 2.2; + + if (mode == 3 || (mode > 1 && !shouldRecurse)) + drawSingleSquare(x, y, halfLength); + + if (shouldRecurse) { + int[][] offsets = { { 1, 1 }, { -1, 1 }, { -1, -1 }, { 1, -1 } }; + for (var i = 0; i < 4; i++) { + if (mode == 2 && i == 3) + drawSingleSquare(x, y, halfLength); + if (mode == 4 && i == 2) + drawSingleSquare(x, y, halfLength); + drawSquares( + x + halfLength * offsets[i][0], + y + halfLength * offsets[i][1], + newHalfLength, + limit, + mode); + } + } + + if (mode == 1) + drawSingleSquare(x, y, halfLength); + } + + private static void drawSingleSquare(double x, double y, double halfLength) { + StdDraw.setPenColor(StdDraw.GRAY); + StdDraw.filledSquare(x, y, halfLength); + StdDraw.setPenColor(); + StdDraw.square(x, y, halfLength); + } +} \ No newline at end of file diff --git a/week7/Triangles.java b/week7/Triangles.java new file mode 100644 index 0000000..0123c58 --- /dev/null +++ b/week7/Triangles.java @@ -0,0 +1,58 @@ +package week7; + +import common.StdDraw; + +public class Triangles { + public static void main(String[] args) { + int order = Integer.parseInt(args.length > 0 ? args[0] : "1"); + StdDraw.enableDoubleBuffering(); + + drawRootTriangle(); + + if (order > 1) + // vertical alignment to center it properly within the root triangle + recurse(order, 0.5, (height(1)) / 2 - 0.15, 0.5); + + StdDraw.show(); + } + + public static void recurse(int limit, double x, double y, double width) { + limit--; + var height = height(width); + drawTriangle(x, y, width, height); + var halfWidth = width / 2; + if (limit > 0) { + recurse(limit, x, y + height * .75, halfWidth); + recurse(limit, x - halfWidth, y - height / 4, halfWidth); + recurse(limit, x + halfWidth, y - height / 4, halfWidth); + } + } + + /** + * Calculates equilateral triangle's height from it's width / side length + */ + public static double height(double width) { + return 0.5 * Math.sqrt(3) * width; + } + + public static void drawTriangle(double x, double y, double width, double height) { + var topLeft = new double[] { x - width / 2, y + height / 2 }; + var topRight = new double[] { x + width / 2, y + height / 2 }; + var bottom = new double[] { x, y - height / 2 }; + StdDraw.polygon(new double[] { topLeft[0], topRight[0], bottom[0] }, + new double[] { topLeft[1], topRight[1], bottom[1] }); + } + + /** + * Variant of draw triangle, but draws it upside down (base/water-level side is + * down) + */ + public static void drawRootTriangle() { + var width = 1; + var height = height(width); + var offset = (1 - height) / 2; + StdDraw.polygon( + new double[] { 0, 1, 0.5 }, + new double[] { offset, offset, height + offset }); + } +} diff --git a/week7/common b/week7/common new file mode 120000 index 0000000..60d3b0a --- /dev/null +++ b/week7/common @@ -0,0 +1 @@ +../common \ No newline at end of file diff --git a/week7/doc.typ b/week7/doc.typ new file mode 100644 index 0000000..4e4f3e5 --- /dev/null +++ b/week7/doc.typ @@ -0,0 +1,30 @@ +#import "@preview/wrap-it:0.1.1": wrap-content +#import "./common/common.typ" : * + +#show: template + += Week 6 + +== Exercise 2.3.22 + +_Recursive squares._ Write a program to produce each of the following recursive +patterns. The ratio of the sizes of the squares is $2.2:1$. To draw a shaded square, +draw a filled gray square, then an unfilled black square. + +#image("squares.png") + +#embedClass(name: "Squares") + +== Exercise 2.3.27 + +_Sierpinski triangles._ Write a recursive program to draw Sierpinski triangles (see PROGRAM 2.2.3). +As with Htree, use a command-line argument to control the depth of the recursion. + +#embedClass(name: "Triangles") + +== Exercise 2.3.31 + +_Plasma clouds._ Write a recursive program to draw plasma clouds, using the +method suggested in the text. + +#embedClass(name: "PlasmaClouds") \ No newline at end of file diff --git a/week7/squares.png b/week7/squares.png new file mode 100644 index 0000000..1c53df9 Binary files /dev/null and b/week7/squares.png differ diff --git a/week7/triangles.png b/week7/triangles.png new file mode 100644 index 0000000..c922116 Binary files /dev/null and b/week7/triangles.png differ