<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: sets code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 03:37:48 GMT</pubDate>
    <description>DZone Snippets: sets code</description>
    <item>
      <title>Cantor's Sets with Processing</title>
      <link>http://snippets.dzone.com/posts/show/5102</link>
      <description>This simple Processing programme generates the &lt;a href="http://en.wikibooks.org/wiki/Fractals/Cantor's_Set"&gt;Cantor's Sets&lt;/a&gt; fractal.&lt;br /&gt;&lt;br /&gt;Images are generated randomly, but tend to look like &lt;a href="http://scvalex.deviantart.com/art/Cantor-s-Sets-76451637"&gt;this&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;int setHeight = 60;&lt;br /&gt;color setColour = color(184 + random(-40, 40),&lt;br /&gt;                        124 + random(-40, 40),&lt;br /&gt;                        124 + random(-40, 40));&lt;br /&gt;int wid = 600;&lt;br /&gt;&lt;br /&gt;void setup() {&lt;br /&gt;  size(wid, (int)((log(wid) / log(3) + 1) * (setHeight + 5)));&lt;br /&gt;  background(0);&lt;br /&gt;  smooth();&lt;br /&gt;  &lt;br /&gt;  cantorSet(10, 10, width - 20, setColour);&lt;br /&gt;  &lt;br /&gt;  save("cantorSet.png");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;float rnd(float x) {&lt;br /&gt;  return x + random(-20, 20);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void brushRect(int x, int y, int w, int h) {&lt;br /&gt;  for (int i = 0; i &lt; h; ++i) {&lt;br /&gt;    float r = random(3);&lt;br /&gt;    strokeWeight(r);&lt;br /&gt;    float downpull = random(h / 4);&lt;br /&gt;    float shudder = random(-2, 2);&lt;br /&gt;    line(x + shudder, y + i, x + w - shudder, y + i + downpull);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void cantorSet(int y, int offX, int wid, color col) {&lt;br /&gt;  if ((y &gt; height - 10) || (wid &lt; 1))&lt;br /&gt;    return;&lt;br /&gt;&lt;br /&gt;  stroke(col);&lt;br /&gt;  brushRect(offX, y, wid, setHeight);&lt;br /&gt;  //fill(col);&lt;br /&gt;  //rect(offX, y, wid, setHeight);&lt;br /&gt;  &lt;br /&gt;  color newCol = color(rnd(red(col)), rnd(green(col)),&lt;br /&gt;                      rnd(blue(col)));&lt;br /&gt;  cantorSet(y + setHeight + 5, offX, wid / 3, newCol);&lt;br /&gt;  cantorSet(y + setHeight + 5, offX + wid*2/3, wid / 3, newCol);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 04 Feb 2008 11:08:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5102</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>Ruby Hash Power-Set</title>
      <link>http://snippets.dzone.com/posts/show/4895</link>
      <description>// adds a #power_set method to Hash class&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# The Array power set is stolen from http://snippets.dzone.com/posts/show/3524&lt;br /&gt;class Array&lt;br /&gt;  # Returns the "power set" for this Array. This means that an array with all&lt;br /&gt;  # subsets of the array's elements will be returned.&lt;br /&gt;  def power_set&lt;br /&gt;    # the power set line is stolen from http://johncarrino.net/blog/2006/08/11/powerset-in-ruby/&lt;br /&gt;    inject([[]]){|c,y|r=[];c.each{|i|r&lt;&lt;i;r&lt;&lt;i+[y]};r}&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class Hash&lt;br /&gt;  def power_set&lt;br /&gt;    # Returns the "power set" for this Hash. This means that a array with hashes of all&lt;br /&gt;    # subsets of the hash's (key =&gt; value) pairs will be returned.&lt;br /&gt;    # Example:&lt;br /&gt;    # &gt;&gt; {:feedback_type=&gt;"", :language_code=&gt;"", :comment=&gt;""}.power_set&lt;br /&gt;    #&lt;br /&gt;    # [{}, {:comment=&gt;""}, {:language_code=&gt;""}, {:language_code=&gt;"", :comment=&gt;""}, {:feedback_type=&gt;""}, {:feedback_type=&gt;"", :comment=&gt;""}, {:feedback_type=&gt;"", :language_code=&gt;""}, {:feedback_type=&gt;"", :language_code=&gt;"", :comment=&gt;""}]&lt;br /&gt;    &lt;br /&gt;    hash_to_array = self.to_a&lt;br /&gt;    array_power_set = hash_to_array.power_set&lt;br /&gt;    hash_power_set = array_power_set.collect { |pairs| pairs.inject({}) { |hash,pair| hash[pair[0]] = pair[1]; hash } }&lt;br /&gt;    hash_power_set&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 13 Dec 2007 12:14:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4895</guid>
      <author>helder (Helder Ribeiro)</author>
    </item>
  </channel>
</rss>
