Course:CPSC312-2019/Snowflakes

From UBC Wiki
Example output: turquoise snowflake with 7 sides
Example output: black snowflake with 20 sides
Example output: salmon snowflake with 4 sides

Authors: Tiger Oakes, Meng Hsun "Daphne" Liu, Sebastien Ban

What is the problem?

We are looking to explore the feasibility of using Haskell to create personal and creative art work. Based off of Vitality Kaurov's Digital Snowflake project and a previous year's Random Mondrian Art project, will use Haskell to implement an algorithm that will output snowflakes with randomized patterns. Colours, slice sizes, and stem shapes are randomly generated to create the artwork. The user will be able to personalize the artwork through by setting parameters.

What is the something extra?

Snowflakes will be outputted as SVG files, either based off user input or generated randomly. The snowflake patterns are created parallel to the user's inputs, so that the art work can be original for everyone who uses the program. The program is extended with different strategies for creating shapes and sizes to reflect different options.

What did we learn from doing this?

We learned a lot about SVG paths and random IO.

We built ontop of the SVG code in the Mondrian Art project by using a data type to represent SVG elements rather than returning strings. This made it easier to decompose functions as nodes could be grouped and transformed. We tweaked the output to be valid SVG instead of HTML by wrapping attribute values in quotation marks so that it works with XML parsers.

We ended up writing three different versions of SVG paths while iterating on the polygon code. Initially the polygons were represented as an array of SVG "line" elements. However, working with so many lines was troublesome and we decided to switch to a "path" with multiple points instead. One of us had some familiarity with SVG paths already and looked up the right syntax to build and SVG path out of points, and we changed our code to output a single node instead of an array. Much later we discovered there was a simple "polygon" node in SVG that doesn't require a special syntax, and switched over to that for readability.

We started off trying to build as much as we could as pure haskell functions with hardcoded inputs. Eventually we reached the point where we needed to add random values. The Mondrian Art project represented random values as an infinite list of random numbers, but this was difficult to work with as we'd have to change many functions to return pairs of random state and the actual result. We realized we were starting to recreate monads and switched over to "do" blocks and IO instead.

We also learned how to work with prompts to display a question on the command line, to allow the user to customize the color and number of sides. Some of the questions had to be preprocessed and validated before passing off to the snowflake function.

Links to code etc

https://github.com/NotWoods/snowflakes

Resources: