creativescala / doodle

Compositional vector graphics in Scala / Scala.JS
https://creativescala.org/doodle/
Apache License 2.0
327 stars 75 forks source link

Maybe draw equilateral triangles, rather than isosceles ones, in Sierpinski.scala #99

Closed philipschwarz closed 3 years ago

philipschwarz commented 3 years ago

Hello. I love this Sierpinski function: it makes for a great example of recursion. I hit upon this Doodle version as I worked on https://www.slideshare.net/pjschwarz/sierpinski-triangle-polyglot-fp-for-fun-and-profit-haskell-and-scala-with-minor-corrections.

I have a small suggestion to make: see the comment I left in the code.

Of course if you are at all interested in making any change, you will do it yourself, but FWIW, here could be one way to do it: https://www.slideshare.net/pjschwarz/sierpinski-triangle-polyglot-fp-for-fun-and-profit-haskell-and-scala-with-minor-corrections#49:

  object EquilateralTriangle:
    // From https://en.wikipedia.org/wiki/Square_root_of_3:
    // The square root of 3 ...is also known as Theodorus' constant,
    // after Theodorus of Cyrene, who proved its irrationality.
    private val constantOfTheodorus: Double = Math.sqrt(3)

    // From https://en.wikipedia.org/wiki/Equilateral_triangle:
    // The altitude (height) h from any side a is √3÷2×a
    private val widthToHeightMultiplier = constantOfTheodorus / 2

    def heightFromWidth(width: Double): Double =
      widthToHeightMultiplier * width
Image.triangle(width = size, height = EquilateralTriangle.heightFromWidth(size))
philipschwarz commented 3 years ago

Here are three examples of the effect, on the Sierpinski triangle, of switching from isosceles triangles to equilateral ones: image image image

noelwelsh commented 3 years ago

Good suggestion. I've added an equilateral triangle element to the various parts of Doodle that need it.

philipschwarz commented 3 years ago

@noelwelsh great - thanks!