mirror of
https://github.com/danbulant/introductionToProgramming
synced 2026-05-19 12:28:44 +00:00
week7
This commit is contained in:
parent
631623009b
commit
0f3bfa38b8
8 changed files with 179 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ pkgs.mkShell rec {
|
|||
buildInputs = with pkgs; [
|
||||
jdk
|
||||
typst
|
||||
tinymist
|
||||
];
|
||||
nativeBuildInputs = with pkgs; [
|
||||
jre
|
||||
|
|
|
|||
33
week7/PlasmaClouds.java
Normal file
33
week7/PlasmaClouds.java
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
56
week7/Squares.java
Normal file
56
week7/Squares.java
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
58
week7/Triangles.java
Normal file
58
week7/Triangles.java
Normal file
|
|
@ -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 });
|
||||
}
|
||||
}
|
||||
1
week7/common
Symbolic link
1
week7/common
Symbolic link
|
|
@ -0,0 +1 @@
|
|||
../common
|
||||
30
week7/doc.typ
Normal file
30
week7/doc.typ
Normal file
|
|
@ -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")
|
||||
BIN
week7/squares.png
Normal file
BIN
week7/squares.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 23 KiB |
BIN
week7/triangles.png
Normal file
BIN
week7/triangles.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
Loading…
Reference in a new issue