Skip to content

Commit d6f3cc6

Browse files
committed
Add randomness to the generative art example
1 parent a6cdc0d commit d6f3cc6

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/pages/indexed-types/conclusions.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Also see @duregard12:feat for an approach specialized to enumerating algebraic d
2222
More recently machine learning techniques are being explored. See, for example, @reddy20:rlcheck and @lemieux23:codamosa.
2323
@goldstein24:practice studies how property based testing is used in practice.
2424

25-
I mentioned that the probability monad can be used in generative art. Generative art is, broadly, art that is generated by some algorithmic process. This can include an element of randomness. While there are papers on generative art (e.g. [@boden09:generative; @dorin12:generative]), and many other resources that discuss it, it's much more fun to create some yourself. Figure [@fig:indexed-types:cycloid] shows an example of generative art, though this example is not one that uses randomness. The code is below, and it has many knobs that you can play with to create your own example. Just add the `@main` annotation to the `cycloid` method and you can run the code from the Scala CLI. Have fun!
25+
I mentioned that the probability monad can be used in generative art. Generative art is, broadly, art that is generated by some algorithmic process. This can include an element of randomness. While there are papers on generative art (e.g. [@boden09:generative; @dorin12:generative]), and many other resources that discuss it, it's much more fun to create some yourself. Figure [@fig:indexed-types:cycloid] shows an example of generative art. The code is below, and it has many knobs that you can play with to create your own example. Just add the `@main` annotation to the `cycloid` method and you can run the code from the Scala CLI. Have fun!
2626

2727
![A set of cycloids showing five-fold symmetry](src/pages/indexed-types/cycloid.pdf){#fig:indexed-types:cycloid}
2828

@@ -34,9 +34,10 @@ import cats.syntax.all.*
3434
import cats.effect.unsafe.implicits.global
3535
import doodle.core.*
3636
import doodle.core.format.{Pdf, Png}
37+
import doodle.interact.syntax.interpolation.*
38+
import doodle.random.{*, given}
3739
import doodle.syntax.all.*
3840
import doodle.java2d.*
39-
import doodle.interact.syntax.interpolation.*
4041

4142
def cycloid(): Unit = {
4243
given Monoid[Angle => Vec] with {
@@ -75,6 +76,18 @@ def cycloid(): Unit = {
7576
.andThen(phase(90.degrees))
7677
.andThen(radius(0.33 * amplitude)))
7778

79+
val randomCycloid: Random[Double => Angle => Vec] =
80+
for {
81+
d <- Random.int(3, 25) // Degree of symmetry
82+
n <- Random.natural(d) // Offset from d
83+
m1 <- Random.int(1, 5)
84+
m2 <- Random.int(m1, m1 + 5)
85+
} yield amplitude =>
86+
cycloid(n, amplitude) |+| cycloid(
87+
m1 * d + n,
88+
0.5 * amplitude
89+
) |+| phase(90.degrees).andThen(cycloid(m2 * d + n, 0.33 * amplitude))
90+
7891
def drawCycloid(
7992
cycloid: Angle => Vec,
8093
start: Angle = 0.degrees,
@@ -89,16 +102,20 @@ def cycloid(): Unit = {
89102
.toList
90103
)
91104

92-
val picture =
105+
/** Repeatedly draw a cycloid with increasing size and a slow turn */
106+
def drawCycloids(cycloid: Double => Angle => Vec): Picture[Unit] =
93107
(0.0)
94108
.upTo(1.0)
95-
.forSteps(50)
109+
.forSteps(30)
96110
.map { m =>
97-
drawCycloid(c1(350 * m + 100)).rotate(30.degrees * m)
111+
drawCycloid(cycloid(350 * m + 100)).rotate(30.degrees * m)
98112
}
99113
.toList
100114
.allOn
101115

116+
// val picture = drawCycloids(c1)
117+
val picture = randomCycloid.map(drawCycloids).run
118+
102119
val frame = Frame.default
103120

104121
picture.drawWithFrame(frame)

0 commit comments

Comments
 (0)