<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yosh</title>
    <description>The latest articles on DEV Community by Yosh (@lesguillemets).</description>
    <link>https://dev.to/lesguillemets</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F42143%2F20333b43-727b-495f-9e50-91cc40b58580.jpeg</url>
      <title>DEV Community: Yosh</title>
      <link>https://dev.to/lesguillemets</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lesguillemets"/>
    <language>en</language>
    <item>
      <title>Drawing a fancy pattern for New Year's cards with Clojure + quil</title>
      <dc:creator>Yosh</dc:creator>
      <pubDate>Fri, 05 Jan 2018 04:25:05 +0000</pubDate>
      <link>https://dev.to/lesguillemets/drawing-a-fancy-pattern-for-new-years-cards-with-clojure--quil-3ej8</link>
      <guid>https://dev.to/lesguillemets/drawing-a-fancy-pattern-for-new-years-cards-with-clojure--quil-3ej8</guid>
      <description>&lt;p&gt;I like sending New Year's cards. Each year I prepare the design, buy these blank cards, and print them at home. This year I thought I'd try &lt;a href="https://github.com/quil/quil"&gt;quil&lt;/a&gt;, basically &lt;a href="https://processing.org/"&gt;Processing&lt;/a&gt; in clojure, to draw some fancy patterns for it. Because I can't draw at all...&lt;/p&gt;

&lt;p&gt;Edit: &lt;a href="https://dev.to/syngrato"&gt;syngrato&lt;/a&gt; adapted this code into ClojureScript (Thanks!). You can have some fun &lt;a href="http://quil.info/sketches/show/-L2FznrshfGMRFrpuU7p"&gt;here&lt;/a&gt;. The license is MIT. &lt;/p&gt;

&lt;h2&gt;
  
  
  Design
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Motif
&lt;/h3&gt;

&lt;p&gt;What kind of pattern is suitable for New Year's cards? In Japan (I'm a Japanese, by the way), things which we can wish each other's health and prosperity with have been popular --- pine trees, cranes, and turtles to name some. They are symbols of health &amp;amp; long lives because they live long (always green for pines). Another popular symbol is the &lt;em&gt;ume&lt;/em&gt; &lt;a href="https://ja.wikipedia.org/wiki/%E3%82%A6%E3%83%A1"&gt;梅&lt;/a&gt;, Japanese apricot, because they bloom the earliest in spiring and celebrating the New Year is all about celebrating the coming spring. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Ume&lt;/em&gt; flowers are traditionally abstracted into design (for lack of my vocab) like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0ycBad-t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jxzsfimripb6u6flcxk5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0ycBad-t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jxzsfimripb6u6flcxk5.png" alt="梅鉢紋 from http://hakko-daiodo.com/main-0"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(Image taken from &lt;a href="http://hakko-daiodo.com/main-0"&gt;this site&lt;/a&gt;, which allows replication). Five circles with stamen nicely signifies the &lt;em&gt;ume&lt;/em&gt; flowers. I like them and its simplicity will go well with quil.&lt;/p&gt;

&lt;p&gt;I talked with Mom and she suggested a traditional design called &lt;a href="https://www.google.co.jp/search?q=%E5%88%A9%E4%BC%91%E6%A2%85%E3%80%80%E6%A8%A1%E6%A7%98&amp;amp;tbm=isch"&gt;利休梅&lt;/a&gt; (do check out the link which is google image search), where the flower is symbolised with 5+1 circles + lines connecting the centre and the petals.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ci30oJ_f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/vnw6yb54zvs07t3sxhtp.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ci30oJ_f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/vnw6yb54zvs07t3sxhtp.JPG" alt="rikyu-ume"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The flowers are, she says, usually scattered to form a design. Maybe I can tile them to make a pattern?&lt;/p&gt;

&lt;h3&gt;
  
  
  Pattern
&lt;/h3&gt;

&lt;p&gt;Tessellation is always an interesting topic. For pentagons, I found &lt;a href="https://en.wikipedia.org/wiki/Cairo_pentagonal_tiling"&gt;the Cairo pentagonal tiling&lt;/a&gt; quite beautiful and symmetric (remember we'll be putting flowers to the pentagons)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c4H-HrLO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/9825c93aw7uc1z5uakm9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c4H-HrLO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/9825c93aw7uc1z5uakm9.png" alt="Cairo pentagonal tiling"&gt;&lt;/a&gt;(&lt;a href="https://commons.wikimedia.org/wiki/File:P2_dual.png"&gt;Image&lt;/a&gt; by &lt;a href="https://commons.wikimedia.org/wiki/User:TED-43"&gt;TED-43&lt;/a&gt;, licensed under the CC-BY License.)&lt;/p&gt;

&lt;p&gt;So let's assign a flower to each of the pentagon there. From now on, I'll call the hexagon formed by 4 pentagons &lt;em&gt;Hexa&lt;/em&gt;. Let the length of the squares and regular triangles &lt;em&gt;r&lt;/em&gt;, which I set to 50px in the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Coding
&lt;/h2&gt;

&lt;p&gt;Let's look at a Hexa and name some lengths. The structure is so elegant that every length I'll need can be calculated nicely. Here's a doodle I drew while coding, which shows how the lengths &lt;code&gt;r1&lt;/code&gt; - &lt;code&gt;r3&lt;/code&gt;  is defined.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---4JpjfpM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/81fi4n68o4vm152a6940.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---4JpjfpM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/81fi4n68o4vm152a6940.jpg" alt="lengths"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After these simple math, coding itself is quite straightforward. To make things easier, a Hexa is drawn in an angle where it stands straight. I defined &lt;code&gt;draw-flower&lt;/code&gt; to draw a single flower; &lt;code&gt;draw-hex-pattern&lt;/code&gt; for a Hexa, in which 4 &lt;code&gt;draw-flower&lt;/code&gt; is called with appropriate translation and rotation; then &lt;code&gt;draw-hex-at&lt;/code&gt;, which accepts the location in the form of &lt;code&gt;(structure hexa x y)&lt;/code&gt;, where &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; are the hexagonal coordinates. Full code (which is probably not ideal) follows..&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; licensed under the MIT license
(ns nen.core
  (:require [quil.core :as q]
            [quil.middleware :as m]))


(defstruct hexa :x :y)
(def r 50)
(def r1 (/ r (Math/sqrt 3)))
(def r2 (* r (+ 1 (/ 1 (Math/sqrt 3)))))
(def r3 (/ (* r (+ 1 (Math/sqrt 3))) 2))
(def corolla-size 15)
(def centre-size 12)
(def str-w 3)
(defn draw-corolla [x y]  (q/ellipse x y corolla-size corolla-size))
(defn draw-corolla-r [x y]
  (let [dx (* 0.1 corolla-size (- (Math/random) 0.5))
        dy (* 0.1 corolla-size (- (Math/random) 0.5)) ]
    (draw-corolla (+ x 0) (+ y 0))
    )
  )

(defn draw-flower []
  (q/fill 223 130 138)
  (doseq
    ; every corolla is located half-way for aesthetics
    [ [x y]
      [[0 0]
       [(/ r2 8) (/ r3 4)]
       [(/ r2 8) (- (/ r3 4))]
       [(/ (+ r1 r2) 4) (/ r1 4)]
       [(/ (+ r1 r2) 4) ( - (/ r1 4))] ]]
    (do
      (q/stroke 223 130 138)
      (q/stroke-weight str-w)
      (q/line (/ r1 2) 0 x y)
      (q/no-stroke)
      (draw-corolla-r x y)))
  (q/fill 248 181 0)
  (q/ellipse (/ r1 2) 0 centre-size centre-size)
  )

(defn draw-hex-pattern []
  (q/fill 223 130 138)

  (draw-flower)

  (q/with-translation
    [ (/ (+ r1 r2) 2) (- (/ r1 2)) ]
    (q/with-rotation
      [(/ Math/PI -2)]
      (draw-flower)))

  (q/with-translation
    [ (/ (+ r1 r2) 2)  (/ r1 2) ]
    (q/with-rotation
      [(/ Math/PI 2)]
      (draw-flower)))

  (q/with-translation
    [ (+ r1 r2) 0 ]
    (q/with-rotation
      [Math/PI]
      (draw-flower)))
  )

(defn draw-hex-at [h]
  (q/with-translation
    [
     (* (:x h) (+ r1 (/ r2 2)) )
     (+
       (* 2 r3 (:y h))
       (if (even? (:x h))
         0
         r3))]
    (draw-hex-pattern)))

(defn setup []
  (q/frame-rate 30)
  (q/color-mode :rgb)
  (for [x (range 10) y (range 5)]
    (struct hexa x y)))

(defn update-state [s] s)

(defn draw-state [state]
  (q/background 255)
  (q/no-loop)

  (q/with-translation [50 100]
    (doseq [h state]
      (draw-hex-at h)))

  (q/save "out.png")
  )

(q/defsketch nen
  :title "super-omedetai"
  :size [800 800]
  ; setup function called only once, during sketch initialization.
  :setup setup
  ; update-state is called on each iteration before draw-state.
  :update update-state
  :draw draw-state
  :features [:keep-on-top]
  :middleware [m/fun-mode])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;which results in something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ccsDfR1u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/47vlujrkofnuj9e77xpo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ccsDfR1u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/47vlujrkofnuj9e77xpo.png" alt="resulting image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That looks beautiful. I love it.&lt;/p&gt;

&lt;p&gt;After adding some texts and my hand-written messages (which is blurred in this post) and my name, here's my new year's card. I'd use the spaces in top-left to add some personal messages to each person.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n69DqDY8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/xb1t9apxmxi0ms8piaem.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n69DqDY8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/xb1t9apxmxi0ms8piaem.jpg" alt="Happy new year"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I wish you all a happy new year, and hope that 2018 will be a wonderful year for all of us.&lt;/p&gt;

</description>
      <category>clojure</category>
      <category>quil</category>
      <category>processing</category>
      <category>art</category>
    </item>
  </channel>
</rss>
