-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterating-paths.html
130 lines (105 loc) · 9.61 KB
/
iterating-paths.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-us">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Iterating Paths</title>
<meta name="author" content="James Reynolds" />
<!--<link rel="stylesheet" href="/css/screen.css" type="text/css" media="screen, projection" />-->
<link rel="stylesheet" href="/css/wordpress.css" type="text/css" media="screen, projection" />
</head>
<body>
<div id="page">
<!-- header -->
<div id="header">
<div id="headerimg" onclick="location.href='http://hcg.drtoast.com';" style="cursor:pointer;">
<h1><a href="http://hcg.drtoast.com/"></a></h1>
<div class="description"></div>
</div>
</div>
<hr />
<!-- main content -->
<div id="content" class="narrowcolumn">
<div id="post">
<h2 class='entry-title'>Iterating Paths</h2>
<small>July 2, 2008 | <a href="/tag/drawing/">drawing</a></small><br />
<p><a href="/uploads/2008/07/paths-iterating.png"></a><a href="http://rcg.drtoast.com/wp-content/uploads/2008/06/paths-iterating.png"> </a>
<p style="text-align: center;"><img class="size-full wp-image-17" title="paths-iterating" src="/uploads/2008/07/paths-iterating.png" alt="" width="400" height="400" /></p>
By sending the "increment" method to a Path object, you can specify changes that will continue to add up or "drift" each time the path is drawn to the canvas.
<pre><span class="comment">#!/usr/local/bin/macruby</span>
<span class="ident">framework</span> <span class="punct">'</span><span class="string">cocoa</span><span class="punct">'</span>
<span class="ident">require</span> <span class="punct">'</span><span class="string">rubygems</span><span class="punct">'</span>
<span class="ident">require</span> <span class="punct">'</span><span class="string">hotcocoa/graphics</span><span class="punct">'</span>
<span class="ident">include</span> <span class="constant">HotCocoa</span>
<span class="ident">include</span> <span class="constant">Graphics</span>
<span class="comment"># create a new 400x400 pixel canvas to draw on</span>
<span class="ident">canvas</span> <span class="punct">=</span> <span class="constant">Canvas</span><span class="punct">.</span><span class="ident">new</span> <span class="symbol">:type</span> <span class="punct">=></span> <span class="symbol">:image</span><span class="punct">,</span> <span class="symbol">:filename</span> <span class="punct">=></span> <span class="punct">'</span><span class="string">iterating.png</span><span class="punct">',</span> <span class="symbol">:size</span> <span class="punct">=></span> <span class="punct">[</span><span class="number">400</span><span class="punct">,</span><span class="number">400</span><span class="punct">]</span>
<span class="ident">canvas</span><span class="punct">.</span><span class="ident">background</span><span class="punct">(</span><span class="constant">Color</span><span class="punct">.</span><span class="ident">white</span><span class="punct">)</span>
<span class="comment"># create a petal shape with base at (0,0), size 40x150, and bulge at 30px</span>
<span class="ident">shape</span> <span class="punct">=</span> <span class="constant">Path</span><span class="punct">.</span><span class="ident">new</span><span class="punct">.</span><span class="ident">petal</span><span class="punct">(</span><span class="number">0</span><span class="punct">,</span><span class="number">0</span><span class="punct">,</span><span class="number">40</span><span class="punct">,</span><span class="number">150</span><span class="punct">,</span><span class="number">30</span><span class="punct">)</span>
<span class="comment"># add a circle</span>
<span class="ident">shape</span><span class="punct">.</span><span class="ident">oval</span><span class="punct">(-</span><span class="number">10</span><span class="punct">,</span><span class="number">20</span><span class="punct">,</span><span class="number">20</span><span class="punct">,</span><span class="number">20</span><span class="punct">)</span>
<span class="comment"># color it red</span>
<span class="ident">shape</span><span class="punct">.</span><span class="ident">fill</span><span class="punct">(</span><span class="constant">Color</span><span class="punct">.</span><span class="ident">red</span><span class="punct">)</span>
<span class="comment"># increment shape parameters by the specified amount each iteration,</span>
<span class="comment"># or by a random value selected from the specified range</span>
<span class="ident">shape</span><span class="punct">.</span><span class="ident">increment</span><span class="punct">(</span><span class="symbol">:rotation</span><span class="punct">,</span> <span class="number">5.0</span><span class="punct">)</span>
<span class="comment">#shape.increment(:scale, 0.95)</span>
<span class="ident">shape</span><span class="punct">.</span><span class="ident">increment</span><span class="punct">(</span><span class="symbol">:scalex</span><span class="punct">,</span> <span class="number">0.99</span><span class="punct">)</span>
<span class="ident">shape</span><span class="punct">.</span><span class="ident">increment</span><span class="punct">(</span><span class="symbol">:scaley</span><span class="punct">,</span> <span class="number">0.96</span><span class="punct">)</span>
<span class="ident">shape</span><span class="punct">.</span><span class="ident">increment</span><span class="punct">(</span><span class="symbol">:x</span><span class="punct">,</span> <span class="number">10.0</span><span class="punct">)</span>
<span class="ident">shape</span><span class="punct">.</span><span class="ident">increment</span><span class="punct">(</span><span class="symbol">:y</span><span class="punct">,</span> <span class="number">12.0</span><span class="punct">)</span>
<span class="ident">shape</span><span class="punct">.</span><span class="ident">increment</span><span class="punct">(</span><span class="symbol">:hue</span><span class="punct">,-</span><span class="number">0.02</span><span class="punct">..</span><span class="number">0.02</span><span class="punct">)</span>
<span class="ident">shape</span><span class="punct">.</span><span class="ident">increment</span><span class="punct">(</span><span class="symbol">:saturation</span><span class="punct">,</span> <span class="punct">-</span><span class="number">0.1</span><span class="punct">..</span><span class="number">0.1</span><span class="punct">)</span>
<span class="ident">shape</span><span class="punct">.</span><span class="ident">increment</span><span class="punct">(</span><span class="symbol">:brightness</span><span class="punct">,</span> <span class="punct">-</span><span class="number">0.1</span><span class="punct">..</span><span class="number">0.1</span><span class="punct">)</span>
<span class="ident">shape</span><span class="punct">.</span><span class="ident">increment</span><span class="punct">(</span><span class="symbol">:alpha</span><span class="punct">,</span> <span class="punct">-</span><span class="number">0.1</span><span class="punct">..</span><span class="number">0.1</span><span class="punct">)</span>
<span class="comment"># draw 200 petals on the canvas starting at location 50,200</span>
<span class="ident">canvas</span><span class="punct">.</span><span class="ident">translate</span><span class="punct">(</span><span class="number">50</span><span class="punct">,</span><span class="number">220</span><span class="punct">)</span>
<span class="ident">canvas</span><span class="punct">.</span><span class="ident">draw</span><span class="punct">(</span><span class="ident">shape</span><span class="punct">,</span><span class="number">0</span><span class="punct">,</span><span class="number">0</span><span class="punct">,</span><span class="number">200</span><span class="punct">)</span>
<span class="ident">canvas</span><span class="punct">.</span><span class="ident">save</span></pre>
</p>
</div>
</div>
<!-- Right Column -->
<div id="sidebar">
<ul>
<!-- Recent Posts -->
<li class="pagenav">
<h2>Recent</h2>
<ul>
<li class="page-item"><a href="/2011/04/27/standalone-library">Standalone Library</a></li>
<li class="page-item"><a href="/2008/07/02/particlesflowers">Particles/Flowers</a></li>
<li class="page-item"><a href="/2008/07/02/text-spirograph">Text Spirograph</a></li>
<li class="page-item"><a href="/2008/07/02/iterating-paths">Iterating Paths</a></li>
<li class="page-item"><a href="/2008/07/02/randomizing-paths">Randomizing Paths</a></li>
</ul>
</li>
<!-- Links -->
<li class="pagenav">
<h2>Links</h2>
<ul>
<li class="page-item"><a href="http://github.com/drtoast/macruby_graphics">download library</a></li>
<li class="page-item"><a href="http://github.com/drtoast">github.com/drtoast</a></li>
<li class="page-item"><a href="http://twitter.com/drtoast">twitter.com/drtoast</a></li>
<li class="page-item"><a href="http://drtoast.com">drtoast.com</a></li>
</ul>
</li>
</ul>
</div>
<!-- Footer -->
<hr />
<div id="footer">
<p class='entry-meta'>Copyright © 2008-2012 <a href="http://github.com/drtoast">James Reynolds</a></p>
</div>
</div>
<!-- Google Analytics -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-546935-4");
pageTracker._trackPageview();
</script>
<!-- Google Analytics end -->
</body>
</html>