.. _christmas-reflection-symmetry: Christmas decorations, snowflakes and reflection symmetry --------------------------------------------------------- An observant reader of :ref:`christmas-rotation-symmetry` (which we recommend you read before this tutorial) might have noticed snowflakes often have more than just rotational symmetry. Snowflakes also have reflection symmetry, meaning that if we look at the mirror image of one of the "arms", it's the same as the original image. .. figure:: /../../_static/figures/manual_examples/reflection_symmetry/pexels-eberhard-grossgasteiger-12366087.jpg :figwidth: 99% Reflective symmetry Landscape reflected in water (`image by Eberhard Grossgasteiger `_). Use reflection symmetry to reduce the number of code lines ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ When drawing an image with reflection symmetry, we can use a loop to cut down on how many lines of code we need (like what we did with rotation symmetry). Below is the code we used to create a snowflake in :ref:`christmas-rotation-symmetry`. Looking closely, we can see that lines 11-14 and 21-24 are very similar, but the angles point in opposite directions. .. include-turtlethread:: reflection_symmetry/01.py :linenos: :emphasize-lines: 11-14,21-24 .. image:: reflection_symmetry/manual_code_output/01.svg :width: 360 :alt: The result from the above code. A snowflake with six arms. :class: sphx-glr-script-out To take advantage of the reflection symmetry, we first need to notice that rotating 30 degrees to the left is the same as rotating -30 degrees to the right. This means we can replace ``left`` with ``right`` if we add a negative sign to the angle. .. include-turtlethread:: reflection_symmetry/02.py :linenos: :emphasize-lines: 14, 21 .. image:: reflection_symmetry/manual_code_output/02.svg :width: 360 :alt: Result from the code above. A snowflake with six arms. :class: sphx-glr-script-out Because we can change direction by changing the sign of the angle, we can use a ``for`` loop over ``-1`` and ``1`` to loop over two different directions: .. include-turtlethread:: reflection_symmetry/03.py :linenos: :emphasize-lines: 7, 12, 15 .. image:: reflection_symmetry/manual_code_output/03.svg :width: 360 :alt: Result from the code above. It's identical to the image generated by the previous conde snippet. :class: sphx-glr-script-out We can see that this code contains a lot fewer lines! Finished snowflake ^^^^^^^^^^^^^^^^^^ .. image:: /../../_static/figures/manual_examples/reflection_symmetry/embroidered_reflection_snowflake_tree.jpg :width: 400 :alt: An image of the finished embroidered snowflake. Example snowflakes ^^^^^^^^^^^^^^^^^^ .. admonition:: Try it yourself: Use code to embroider your own snowflake with both rotational and reflection symmetry! Below is a gallery of examples you can use as a starting point or for inspiration. **Snowflake 1** .. image:: reflection_symmetry/manual_code_output/gallery01.svg :width: 180 :alt: A snow flake arm with four branches, the first points backward and the rest point forward. :class: snowflake-gallery-arm .. collapse:: Snowflake :class: snowflake-gallery-snowflake .. image:: reflection_symmetry/manual_code_output/gallery01_full.svg :width: 360 :alt: A snow flake with six arms where each arm has four arms on each side. .. collapse:: Kode for arm :class: snowflake-gallery-code .. include-turtlethread:: reflection_symmetry/gallery01.py :linenos: .. collapse:: Kode for snowflake :class: snowflake-gallery-code .. include-turtlethread:: reflection_symmetry/gallery01_full.py :linenos: **Snowflake 2** .. image:: reflection_symmetry/manual_code_output/gallery02.svg :width: 180 :alt: A snow flake arm that looks like a half heart. :class: snowflake-gallery-arm .. collapse:: Snowflake :class: snowflake-gallery-snowflake .. image:: reflection_symmetry/manual_code_output/gallery02_full.svg :width: 360 :alt: A snow flake that looks like four hearts. .. collapse:: Kode for arm :class: snowflake-gallery-code .. include-turtlethread:: reflection_symmetry/gallery02.py :linenos: .. collapse:: Kode for snowflake :class: snowflake-gallery-code .. include-turtlethread:: reflection_symmetry/gallery02_full.py :linenos: **Snowflake 3** .. image:: reflection_symmetry/manual_code_output/gallery03.svg :width: 180 :alt: A snow flake arm with four circles next to each other. Each subsequent circle is smaller than the previous. :class: snowflake-gallery-arm .. collapse:: Snowflake :class: snowflake-gallery-snowflake .. image:: reflection_symmetry/manual_code_output/gallery03_full.svg :width: 360 :alt: A snow flake where each arm has four circles on each side. .. collapse:: Kode for arm :class: snowflake-gallery-code .. include-turtlethread:: reflection_symmetry/gallery03.py :linenos: .. collapse:: Kode for snowflake :class: snowflake-gallery-code .. include-turtlethread:: reflection_symmetry/gallery03_full.py :linenos: