<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>mostly a codeBlog &#187; Uncategorized</title>
	<atom:link href="http://www.rickigregersen.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rickigregersen.com</link>
	<description>whatever I am currently programming... and I truly digress</description>
	<lastBuildDate>Mon, 21 Jun 2010 15:40:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Class Methods and Typedef Enums</title>
		<link>http://www.rickigregersen.com/2010/06/21/class-methods-and-typedef-enums/</link>
		<comments>http://www.rickigregersen.com/2010/06/21/class-methods-and-typedef-enums/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 15:22:08 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=263</guid>
		<description><![CDATA[This is a nifty little &#8220;pattern&#8221; for collecting reusable elements, color, labels, settings etc. that is standard through out your program. I do use Interface Builder, but I prefer reuse approach of code. When a projects design has been agreed upon and the basic things are laid out: font, size, color theme etc. I usually [...]]]></description>
			<content:encoded><![CDATA[<p>This is a nifty little &#8220;pattern&#8221; for collecting reusable elements, color, labels, settings etc. that is standard through out your program. I do use Interface Builder, but I prefer reuse approach of code.</p>
<p>When a projects design has been agreed upon and the basic things are laid out: font, size, color theme etc. I usually build myself an ISInterfaceElements class. This class contains a few Class Methods called getColor:(ColorType) type and getLabel:(LabelType) type. Our Proof-of-Concept Class will contain 3 different colors: BackgroundColor, ForegroundColor and TextColor. It will also contain 3 types of labels, a HeadlineLabel, TextLabel and a DetailLabel. These six components will get you pretty far in describing the text and color of an iPhone App.</p>
<h3>How to do it &#8220;manually&#8221;.</h3>
<p>If I were to build a UILabel, using nothing but code, and make it adhere to the design mentioned above it would probably go a bit like this:</p>
<pre>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a> <span style="color: #002200;">*</span>label <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>0.0f, 0.0f, 320.0f, 20.0f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Helvetica&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">14</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setTextAlignment<span style="color: #002200;">:</span>UITextAlignmentCenter<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; UIColor <span style="color: #002200;">*</span>textColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor colorWithRed<span style="color: #002200;">:</span>0.2f green<span style="color: #002200;">:</span>0.5f blue<span style="color: #002200;">:</span>0.3f alpha<span style="color: #002200;">:</span>1.0f<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setTextColor<span style="color: #002200;">:</span>textColor<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setShadowColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor darkGrayColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setShadowOffset<span style="color: #002200;">:</span>CGSizeMake<span style="color: #002200;">&#40;</span>0.0f, 1.0f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
</pre>
<p>If you do stuff like this manually and scattered around your program you will at some point encounter the &#8220;Let&#8217;s change the font and color of the app!&#8221; witch will leave you with a couple of hours of &#8220;Sherlock Holmes copy-paste&#8221;, as I like to call it.<br />
Instead we will try to gather common functionality in a single class so that any change made here will propagate outwards to all the parts of the code.<br />
We will use the approach that Apple often uses in their frameworks, e.g. when initializing a UIButton you could do it like this:</p>
<pre>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; UIButton <span style="color: #002200;">*</span>button <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIButton buttonWithType<span style="color: #002200;">:</span>UIButtonTypeCustom<span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
</pre>
<p>The way to go about this is to write a Class that has Class Methods for getting a UILabel and UIColor back, and instead of giving them ID&#8217;s like 0034, 1234 etc. we write a typedef enum with nice, easy-to-remember names like UIButtonTypeCustom above. This will make sure code completion and compiler checking will work with you instead of against you.</p>
<h3>The Class:</h3>
<p>As I wrote at the beginning our class will have 3 label and 3 colors, so we start out by writing typedef enums for these elements and we define a <strong>(UILabel*) getLabel:(LabelType)</strong> type and <strong>(UIColor*)getColor:(ColorType) type</strong> method.</p>
<pre>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #11740a; font-style: italic;">//</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;ISInterfaceElements.h</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;BlogPostCode</span><br />
<span style="color: #11740a; font-style: italic;">//</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;Created by RickiG on 6/21/10.</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;Copyright 2010 www.rickigregersen.com. Use as you please:).</span><br />
<span style="color: #11740a; font-style: italic;">//</span><br />
<br />
<span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span><br />
<br />
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">enum</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; BackgroundColor <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>,&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">//This will give the BackgroundColor the ID 0 and ForeGroundColor the ID 1 etc.</span><br />
&nbsp; &nbsp; ForegroundColor,&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">//it is not needed, if left out it will default to 0,1,2...</span><br />
&nbsp; &nbsp; TextColor,<br />
&nbsp; &nbsp; <br />
<span style="color: #002200;">&#125;</span> ColorType;<br />
<br />
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">enum</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; HeadlineLabel <span style="color: #002200;">=</span> <span style="color: #2400d9;">3</span>,&nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">//This will give the HeadlineLabel the ID 3, the TextLabel 4 etc.</span><br />
&nbsp; &nbsp; TextLabel,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">//use this only if the enums can not have the same value, in our case</span><br />
&nbsp; &nbsp; DetailLabel,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">//they are not needed, but shown to illustrate that enums just corresponds to integers</span><br />
&nbsp; &nbsp; <br />
<span style="color: #002200;">&#125;</span> LabelType;<br />
<br />
<span style="color: #a61390;">@interface</span> ISInterfaceElements <span style="color: #002200;">:</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/"><span style="color: #400080;">NSObject</span></a> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="display:block;background-color:#ffff66"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>UIColor<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getColor<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ColorType<span style="color: #002200;">&#41;</span> type;<br /></span><span style="display:block;background-color:#ffff66"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getLabel<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>LabelType<span style="color: #002200;">&#41;</span> type;<br /></span><br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
</pre>
<p>Now we implement these two methods. The logic of determining which kind of Color of Label is returned is done with a switch case (as the enums are integers):</p>
<pre>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #11740a; font-style: italic;">//</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;ISInterfaceElements.m</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;BlogPostCode</span><br />
<span style="color: #11740a; font-style: italic;">//</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;Created by RickiG on 6/21/10.</span><br />
<span style="color: #11740a; font-style: italic;">// &nbsp;Copyright 2010 www.rickigregersen.com. Use as you please:).</span><br />
<span style="color: #11740a; font-style: italic;">//</span><br />
<br />
<span style="color: #6e371a;">#import &quot;ISInterfaceElements.h&quot;</span><br />
<br />
<span style="color: #6e371a;">#define APP_FONT @&quot;HelveticaNeue&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Defined once so that it is simple to change the font of the app</span><br />
<span style="color: #6e371a;">#define APP_FONT_BOLD @&quot;HelveticaNeue-Bold&quot;</span><br />
<br />
<span style="color: #a61390;">@implementation</span> ISInterfaceElements<br />
<br />
<span style="display:block;background-color:#ffff66"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>UIColor<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getColor<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ColorType<span style="color: #002200;">&#41;</span> type <span style="color: #002200;">&#123;</span><br /></span>&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">int</span> value; <span style="color: #11740a; font-style: italic;">//We deal with the Color in terms of Hex, this is what a designer will often return.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">switch</span> <span style="color: #002200;">&#40;</span>type<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> BackgroundColor<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value <span style="color: #002200;">=</span> 0x2d3c3f;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> ForegroundColor<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value <span style="color: #002200;">=</span> 0x627376;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> TextColor<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value <span style="color: #002200;">=</span> 0xDDDDDD;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">default</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value <span style="color: #002200;">=</span> 0x000000;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">int</span> r, g, b;<br />
&nbsp; &nbsp; b <span style="color: #002200;">=</span> value <span style="color: #002200;">&amp;</span> 0x0000FF;<br />
&nbsp; &nbsp; g <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>value <span style="color: #002200;">&amp;</span> 0x00FF00<span style="color: #002200;">&#41;</span> &gt;&gt; <span style="color: #2400d9;">8</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; r <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>value <span style="color: #002200;">&amp;</span> 0xFF0000<span style="color: #002200;">&#41;</span> &gt;&gt; <span style="color: #2400d9;">16</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>UIColor colorWithRed<span style="color: #002200;">:</span>r<span style="color: #002200;">/</span>255.0f green<span style="color: #002200;">:</span>g<span style="color: #002200;">/</span>255.0f blue<span style="color: #002200;">:</span>b<span style="color: #002200;">/</span>255.0f alpha<span style="color: #002200;">:</span>1.0f<span style="color: #002200;">&#93;</span>; <br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="display:block;background-color:#ffff66"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getLabel<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>LabelType<span style="color: #002200;">&#41;</span> type <span style="color: #002200;">&#123;</span><br /></span>&nbsp; &nbsp; <br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a> <span style="color: #002200;">*</span>label <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor clearColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">switch</span> <span style="color: #002200;">&#40;</span>type<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> HeadlineLabel<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span>APP_FONT_BOLD size<span style="color: #002200;">:</span><span style="color: #2400d9;">14</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setTextColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self getColor<span style="color: #002200;">:</span>TextColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.shadowColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor darkGrayColor<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.shadowOffset <span style="color: #002200;">=</span> CGSizeMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> TextLabel<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span>APP_FONT size<span style="color: #002200;">:</span><span style="color: #2400d9;">12</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setTextColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.shadowColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor blackColor<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.shadowOffset <span style="color: #002200;">=</span> CGSizeMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> DetailLabel<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span>APP_FONT size<span style="color: #002200;">:</span><span style="color: #2400d9;">10</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setTextColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor darkGrayColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>label setTextAlignment<span style="color: #002200;">:</span>UITextAlignmentRight<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">default</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> label;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
</pre>
<h3>Using the ISInterfaceElements Class:</h3>
<p>Now that the class is set up it is really simple to obtain a UILabel or UIColor in a way that 1) does not clutter up your code with repeating setup-code 2) makes your design consistent, fonts and colors are easily changed in one location 3) follows the approach you know from Apples frameworks.</p>
<p>Make sure to import the ISInterfaceElements.h header then do the highlighted part below:</p>
<pre>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didFinishLaunchingWithOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span style="color: #400080;">NSDictionary</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>launchOptions <span style="color: #002200;">&#123;</span> &nbsp; &nbsp;<br />
<br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Override point for customization after application launch</span><br />
&nbsp; &nbsp; <br />
<span style="display:block;background-color:#ffff66">&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/UILabel_Class/"><span style="color: #400080;">UILabel</span></a> <span style="color: #002200;">*</span>headline <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>ISInterfaceElements getLabel<span style="color: #002200;">:</span>HeadlineLabel<span style="color: #002200;">&#93;</span>;<br /></span>&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>headline setText<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hello, World&quot;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>headline<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;<br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2010/06/21/class-methods-and-typedef-enums/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DTXplorer drum kit goes Processing</title>
		<link>http://www.rickigregersen.com/2009/09/09/dtxplorer-drum-kit-goes-processing/</link>
		<comments>http://www.rickigregersen.com/2009/09/09/dtxplorer-drum-kit-goes-processing/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 00:01:27 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Processing]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[drum]]></category>
		<category><![CDATA[DTXplorer]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[MIDI]]></category>
		<category><![CDATA[Midipipe]]></category>
		<category><![CDATA[OSC]]></category>
		<category><![CDATA[Osculator]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=199</guid>
		<description><![CDATA[This my DTXplorer drumkit getting feed into MidiPipe onto Osculator and finally ends up in Processing. Now thats beat detection! I have taken some time to get my Yamaha DTXplorer electronic drums to communicate nicely with my Mac, finally it worked out and I can now &#8220;harvest&#8221; the sweet, sweet MIDI coming from the drums. [...]]]></description>
			<content:encoded><![CDATA[<h5>This my DTXplorer drumkit getting feed into MidiPipe onto Osculator and finally ends up in Processing. Now thats beat detection!</h5>
<p><a href="http://www.rickigregersen.com/2009/09/09/dtxplorer-drum-kit-goes-processing/"><em>Click here to view the embedded video.</em></a></p><br />
<p><a href="http://www.rickigregersen.com/2009/09/09/dtxplorer-drum-kit-goes-processing/"><em>Click here to view the embedded video.</em></a></p>
<p>I have taken some time to get my <a href="http://www.yamaha.com/drums/drumproductdetail.html?CNTID=544640&amp;CTID=5040587">Yamaha DTXplorer</a> electronic drums to communicate nicely with my Mac, finally it worked out and I can now &#8220;harvest&#8221; the sweet, sweet MIDI coming from the drums. Im no Moby, so believe me when I say these last one and a half days of frustrations was not to immortalize the sound of my drumming. It was, of course to get the data into <a href="http://www.processing.org">Processing</a>. A friend of mine is a DJ and he wanted us to experiment with playing &#8220;live&#8221; drums along with the turntables. Furthermore, to make some sort of visualization that would be 100% true to the beat. This is very hard I found out, there are a lot of good sound libraries with beat detection out there, but they are never plug n&#8217; play. You need to tweak your code for every single song and even that is no guarantee for a consistent output. I came up with a pretty straight forward solution, if we were to use the electronic drums anyway we could just as well use the MIDI for the beat detection and then have the turntables control the colors.</p>
<p>I found out that I needed a few different applications and a MIDI cable before I got the signal all the way through. First off is the <a href="http://www.esi-audio.com/products/midimate2/">ESI MidiMate</a> cable, I actually had the old model, but I think it was broken and could not handle if the MIDI in was not connected to a ground plane. It was 295 Danish Kroner ( ≈$55 ). Then I needed <a href="http://web.mac.com/nicowald/SubtleSoft/MidiPipe.html">MidiPipe</a>. This a free application developed by Nico Wald and it is brilliant! Should I ever make any money with this setup I will run to a browser and donate some money to Nico.<br />
The idea with MidiPipe is that it &#8220;hijacks&#8221; every single MIDI port it can detect on the system, you then drag the one you need to the pipe(ESI MIDIMATE Port 1 turn up in MidiPipe the second I connected it to the drumkit) you can then drag other stuff into the pipe. I dragged in &#8220;AList&#8221; from the &#8220;Modifier&#8221; and that gave me just what it says, a list of all midi signals passing through MidiPipe.</p>
<div id="attachment_201" class="wp-caption aligncenter" style="width: 506px"><a href="http://www.rickigregersen.com/wp-content/uploads/midipipe.png"><img class="size-full wp-image-201" title="midipipe" src="http://www.rickigregersen.com/wp-content/uploads/midipipe.png" alt="MidiPipe is a life-saver" width="496" height="512" /></a><p class="wp-caption-text">MidiPipe is a life-saver</p></div>
<p>This was a cool way of testing out the drums. Lastly I dragged a &#8220;MIDI out&#8221; into the Pipe, more about that in a second. Then I went out and finally bought that license for <a href="http://www.osculator.net/wp/">OSCulator</a>. Osculator have been running my Wiimote, iPhone, Arduino and Make controller for the last year and I have been to lazy/cheap to just buy the license. Well Osculator is a $15 minimum, but Camille asks people nicely to pay $39, which I did, of course, having used his software for about a year now. What Osculator can do is just to much to explain here&#8230; I needed it to translate my MIDI into OSC (because OSC is the new MIDI <img src='http://www.rickigregersen.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) and then relay the rewritten data to a port on localhost using UDP. This was done so I could have some control over the data, also I now get something along the lines of &#8220;snare &#8211; on &#8211; 0.6&#8243; (thats the pad sending the message, that it was &#8220;on&#8221;/hit and the strength it was hit with) into Processing instead of some MIDI byte I have to bitwise shift around to get any data from it, it just makes debugging easier and sending something through Osculator opens a world of other possibilities.</p>
<div id="attachment_204" class="wp-caption aligncenter" style="width: 522px"><a href="http://www.rickigregersen.com/wp-content/uploads/osculator1.png"><img class="size-full wp-image-204" title="osculator" src="http://www.rickigregersen.com/wp-content/uploads/osculator1.png" alt="It is just crazy how many features there can be in such a simple application" width="512" height="484" /></a><p class="wp-caption-text">It is just crazy how many features there can be in such a simple application</p></div>
<p>A note here, when I started Osculator, MidiPipe noticed Osculator was listening on port 8000 and stuffed that piece of information into the &#8220;MIDI out&#8221; I mentioned earlier, now I could just select it there. So now my MIDI came in through MidiPipe, was listed for me to see and then relayed on to Osculator. In Osculator I caught each MIDI event and by selecting the signal and going Osculator &#8211; Edit &#8211; Demux, I could split the signal into its components and rebroadcast them as OSC messages on the network. For handling OSC Protocol stuff in Processing Andreas Schlegel did a really great library, you can get it <a href="http://www.sojamo.de/libraries/oscP5/">here</a> along with some other goodies Andreas made.</p>
<p>I now had 8 different OSC messages, one for each drum pad, cymbal and foot pedal (Im going to do the high hat pedal later on) that I can read out in Processing. I did a small sketch as you see in the video and just started the &#8220;play along&#8221; feature on my DTXplorer kit, with some &#8220;Hard Rock&#8221; <img src='http://www.rickigregersen.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  selected and and the computers microphone recording.</p>
<p>And as always if there is some code or help You need just leave a mail or comment.</p>
<p><a href="http://www.rickigregersen.com/wp-content/uploads/midiProcessing.mov">A version not completely compressed by the geniuses over at youtube</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/09/09/dtxplorer-drum-kit-goes-processing/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
<enclosure url="http://www.rickigregersen.com/wp-content/uploads/midiProcessing.mov" length="1515678" type="video/quicktime" />
		</item>
		<item>
		<title>Fake Gravity Fluid</title>
		<link>http://www.rickigregersen.com/2009/08/31/fake-gravity-fluid/</link>
		<comments>http://www.rickigregersen.com/2009/08/31/fake-gravity-fluid/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 22:01:41 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Particle]]></category>
		<category><![CDATA[Processing]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[flow field]]></category>
		<category><![CDATA[fun]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=196</guid>
		<description><![CDATA[I was playing around with the Traer Physics Library for processing/java. It is really fast but even though I had done some small experiments with it a year or so back, I still had to dig out the manual and refresh some basic stuff. Well Im looking into the whole FluidSolver thing, again, and I [...]]]></description>
			<content:encoded><![CDATA[<p>I was playing around with the <a href="http://www.cs.princeton.edu/~traer/physics/">Traer Physics Library</a> for processing/java. It is really fast but even though I had done some small experiments with it a year or so back, I still had to dig out the manual and refresh some basic stuff.<br />
Well Im looking into the whole FluidSolver thing, again, and I thought it could be fun to do a fast and crude Newtonian approach and just go for an effect that looks a bit like it, but is far more simple.</p>
<p><img src="http://www.rickigregersen.com/wp-content/uploads/fluid.png" alt="16834 particles fakes fluid" /></p>
<p><a href="http://www.rickigregersen.com/wp-content/uploads/fluid_applet/index.html">Example and code here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/08/31/fake-gravity-fluid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AnTrack</title>
		<link>http://www.rickigregersen.com/2009/05/02/antrack/</link>
		<comments>http://www.rickigregersen.com/2009/05/02/antrack/#comments</comments>
		<pubDate>Sat, 02 May 2009 13:52:15 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Processing]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cam]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[sound]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=79</guid>
		<description><![CDATA[My friend Kenneth expressed some interest in giving Processing a spin. He is an engineering student and usually deals in C++. The learing curve in Processing is extremely flat and if you have prior programming experience you will go from having an idea to an actual prototype in minutes. So we thought up a small [...]]]></description>
			<content:encoded><![CDATA[<p>My friend Kenneth expressed some interest in giving Processing a spin. He is an engineering student and usually deals in C++.</p>
<p>The learing curve in Processing is extremely flat and if you have prior programming experience you will go from having an idea to an actual prototype in minutes. So we thought up a small project: A Processing sketch that could detect a wavy black line on a piece of paper and subsequently play a tone according to the lines y position.</p>
<p>The result was <em><strong>anTrack</strong></em>, it uses the Capture Library to read the bitmaps of a web cam and the Minim sound library to produce a sine wave. It works just fine and my friend was hooked on the simplicity of <a href="http://www.processing.org">Processing</a>, he actually wrote most of the implementations.</p>
<p>What to use this for? I have no idea, it resembles a 1979 sci-fi horror film sound effect or a <a href="http://en.wikipedia.org/wiki/Theremin" target="_blank">Theremin</a>. We thought about building some sort of a rack in lego mindstorm that would continously scroll a piece of paper infront of the camera. A seismographic reading turned into sound, I guess, but the main goal was to have a little fun with processing for a couple of hours and we achieved that just fine.</p>
<p>anTrack.pde</p>
<pre lang="java">/*
* author: Kenneth Knudsen &amp; Ricki Gregersen
* description: reads a line from a piece of paper using a webcam
*              and outputs a tone between 440 and 880 Hz
*              according to the lines y coordinate
*/
//use the minim sound library, included in processing 1.0
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;

//CamModule instance for grapping a frame from the webcam
CamModule cam;
//SoundModule instance for outputting a sine wave at a specific frequence
SoundModule sound;
//TrackModule instance for translating a y coordinate to a frequency
TrackModule track;

void setup()
{
  size(640, 480);
  frameRate(30);
  //CamModule constructor takes an input of (PApplet, width to grap, height to grap and offset)
  //this means: we will grap 30x480 pixels from the webcam starting at 320 in the x direction
  cam = new CamModule(this, 30, height, 320);
  sound = new SoundModule(this);
  track = new TrackModule();
}

void draw()
{
  //get the bitmap from the webcam
  PImage frame = cam.bitmap();
  //process it and return the position of the line as a frequency
  float freq = track.processBitmap(frame);
  //feed the frequency to the soundModule
  sound.freq(freq);
  //present the frame grabbed from the webcam to the screen
  image(frame, 320, 0);
}
//remember to close the connection made to the soundcard
void stop()
{
  super.stop();
  sound.stop();
}</pre>
<p>The CamModule:</p>
<pre lang="java">import processing.video.*;

class CamModule
{

  private Capture cam;
  private PImage frame;
  private int w, h, offSet;

  CamModule(PApplet applet, int _w, int _h, int _o)
  {
    w = _w;
    h = _h;
    offSet = _o;
    String[] devices = Capture.list();
    //println(devices);
    //comment in the above line if you don't know which port your webcam is plugged into
    cam = new Capture(applet, width, height, devices[1]);
    frame = createImage(w, h, RGB);
  }

  public PImage bitmap()
  {
    //read the full image from the webcam and copy out the pixels needed
    if (cam.available()) {
        cam.read();
        frame.copy(cam, offSet, 0, w, h, 0, 0, w, h);
    }
    return frame;
  }
}</pre>
<p>The SoundModule.pde</p>
<pre lang="java">class SoundModule
{
  Minim m;
  AudioOutput dac;
  SineWave sine;

  SoundModule(PApplet a)
  {
    m = new Minim(a);
    dac = m.getLineOut(Minim.STEREO);
    sine = new SineWave(0, 0.5, dac.sampleRate());
    //it is all in the minim documentation which is quite good, but portamento means that there
    //will be no pause when changing from one frequency to another, instead there is a 200 ms slide from
    //the original tone to the new one.
    sine.portamento(200);
    dac.addSignal(sine);
  }

  void freq(float f)
  {
    //if a frequency is present, map it to a sine with Hz between 440 and 880 (an octave)
    if(f &gt; 0)
    {
      f = map(f, 0.1, height, 440, 880);
    }
    else{
      f = 0;
    }
    sine.setFreq(f);
  }
  //again remenber to close the connection to the soundcard
  void stop()
  {
    m.stop();
    dac.close();
  }
}</pre>
<p>The TrackModule.pde</p>
<pre lang="java">class TrackModule
{

  private float th = 80; //brightness 0-255
  private int hh = 80; //percent 0-100

    TrackModule()
  {

  }

  float processBitmap(PImage bm)
  {
    int index;
    int hits;
    int successive_hits = bm.width * (hh / 100);

    /*  this is a sort of filter.
     we run through the pixels one horizontal line at the time
     if we find a pixel with a brightness below the threshold (th)
     we add a hit. So if 80 percent of a line is dark enought to
     trigger the filter we count that as a dark line spotted and we retun the
     y value were it was spotted.
     */

    for(int y = 0; y &lt; bm.height; y++)
    {
      hits = 0;
      index = y * bm.width;
      for(int x = 0; x &lt; bm.width; x++)
      {
        color c = bm.pixels[index + x];
        if( brightness(c) &lt; th)
        {
          hits++;
          if(hits &gt; successive_hits)
          {
            return y;
          }
        }
      }
    }
    return 0.0;
  }
}</pre>
<p>There is an Applet <a title="anTrack source" href="http://www.rickigregersen.com/wp-content/code/antrack/" target="_blank">here</a>, I haven&#8217;t gone through the steps to make it work in a browser so the visitor<br />
can use their own web cam, it&#8217;s mostly done so you can get the source code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/05/02/antrack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kruskals algorithm in Processing</title>
		<link>http://www.rickigregersen.com/2009/03/18/kruskals-algorithm-in-processing/</link>
		<comments>http://www.rickigregersen.com/2009/03/18/kruskals-algorithm-in-processing/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 17:41:58 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Processing]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Kruskal]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=4</guid>
		<description><![CDATA[I was reading Robert Sedgewicks &#8220;Algorithms in Java&#8221; and had skipped to part 5 concerning Graphs. I know I ought not skip portions of a book concerning something that difficult, but 130 pages into the first part I needed to see where all this theory was going. I stumbled over the Kruskal/Prims/Dijkstra algorithms and the visual [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_37" class="wp-caption alignnone" style="width: 707px"><img class="size-full wp-image-37" title="kruskal" src="http://www.rickigregersen.com/wp-content/uploads/kruskal.png" alt="Minimum Spanning Tree (red path)" width="697" height="496" /><p class="wp-caption-text">Minimum Spanning Tree (red path)</p></div>
<p>I was reading Robert Sedgewicks &#8220;Algorithms in Java&#8221; and had skipped to part 5 concerning <em>Graphs.</em></p>
<p>I know I ought not skip portions of a book concerning something that difficult, but 130 pages into the first part I needed to see where all this theory was going.</p>
<p>I stumbled over the Kruskal/Prims/Dijkstra algorithms and the visual representations caught my eye. They were really simple to look at but you could sense there was some sort of genius system behind it. That is one of my favorite reasons for opening up Processing and start sketching.</p>
<p><strong>A few </strong><em><span style="text-decoration: line-through;"><strong>need to know</strong></span></em><strong> things on Graphs:</strong></p>
<p>Thinking in 2 dimensions, a Graph consists of a number of points on a surface. A point is called a <em>vertex </em>and each vertex can be connected to a number of other vertices. Such a connection between 2, or more, vertexes is called and <em>edge. </em></p>
<p><em></em></p>
<p><em></em></p>
<p><em></em></p>
<p><em></em></p>
<p><em> </em></p>
<div id="attachment_26" class="wp-caption alignnone" style="width: 285px"><img class="size-medium wp-image-26" title="simple graph" src="http://www.rickigregersen.com/wp-content/uploads/graph01.png" alt="simple graph" width="275" height="235" /><p class="wp-caption-text">simple graph</p></div>
<p><em><span style="font-style: normal;">There are many different types of Graphs: </span>directed graphs, </em>where directed means, that two vertices that are connected also states which direction you can travel from one vertex to the other, e.g. V1 -&gt;V2 means you can go from V1 to V2 but not the other way. <em>Undirected Graphs </em>is then, of course, a graph where it does not matter which way you travel. Directed and undirected graphs can also be <em>weighted</em> or not. This means that &#8220;it takes a certain amount of energy&#8221; to travel from one vertex to another.</p>
<div id="attachment_28" class="wp-caption alignnone" style="width: 295px"><img class="size-full wp-image-28" title="simple directed weighted graph" src="http://www.rickigregersen.com/wp-content/uploads/graph02.png" alt="simple directed weighted graph" width="285" height="178" /><p class="wp-caption-text">simple directed weighted graph</p></div>
<p>This comes clear when we think back to the 2 dimensions and that a graph can be points/vertices on a surface. If we were looking to connect three vertices using the least amount of &#8220;energy&#8221;  and vertex v1 is situated a distance of 9 meters from vertex V2 and vertex V1 is situated 4 meters from vertex V3 and vertex V2 is situated 3 meters from vertex V3, we would connect them as such: V1-V3-V2</p>
<div id="attachment_31" class="wp-caption alignnone" style="width: 254px"><img class="size-full wp-image-31" title="Euclidian weighted graph" src="http://www.rickigregersen.com/wp-content/uploads/graph03.png" alt="Euclidian weighted graph" width="244" height="123" /><p class="wp-caption-text">Euclidian weighted graph</p></div>
<h1>Minimum Spanning Tree</h1>
<p>Having scratched the surface of Graphs it is now a bit easier to understand what a <em>minimum spanning tree</em> is.</p>
<p>In the &#8220;Algorithms in Java&#8221; book the minimum spanning tree(MST) is defined as:</p>
<blockquote><p>In a weighted graph, find a minimum-weight set of edges that connects all the vertices.</p></blockquote>
<p>It is also defined as:</p>
<blockquote><p>A minimum spanning tree of a weighted graph is a spanning tree whose weight(the sum of weights of it&#8217;s edges) is no larger than the weight of any other spanning tree.</p></blockquote>
<p>It is good to dwell on that last definition for a second. It says that there might be lots and lots of spanning trees in a graph, and that some of them can have the same combined weight. According to the definition this is okay, the more the merrier, as long as it is still a minimum spanning tree.</p>
<p>This means, that from any vertex you should be able to travel to any other vertex, not necessarily along the shortest path, that is another algorithm altogether, but all the vertices is connected in a way that adds up to the combined shortest edges.</p>
<p>There are quite a few algorithms that strives to solve this problem, two of them is Prim&#8217;s and Kruskal&#8217;s algorithm.</p>
<p>Prim&#8217;s algorithm, it goes under many different names as it has been discovered and implemented by more than one person, but that is a story for <a href="http://en.wikipedia.org/wiki/Prim_algorithm">Wikipedia</a> and not this very short post.</p>
<p>Prim&#8217;s algorithm looks at a graph, selects an arbitrary vertex and starts testing all the edges connected to this vertex, once it finds the shortest edge, the edge with the smallest weight, it adds this to the tree, e.g. V12-V65. It then goes on to look for the next shortest edge from either V12 or V65. Before the algorithm can add another edge it must test that it is not creating a <em>cycle. </em>A cycle is if V1-V2 and V2-V3 and V3-V1 is connected, meaning you could start out from V1 and then end up in V1 again. A cycle is redundant in a minimum spanning tree and not desired.</p>
<p>If the algorithm consists of N vertices the MST will be finished when N-1 edges has been added to the MST, regarding no cycles has been made.</p>
<p>The Kruskla&#8217;s algorithm works in a very similar way, the main difference is that Kruskal&#8217;s algorithm starts at the first vertex, i.e V1 and then works it&#8217;s way outwards from that.</p>
<p>This means that an identical graph will yield the same MST each time when using Kruskal and that Prim&#8217;s can yield different MST&#8217;s depending on which vertex was set as the starting point(remember the second definition&#8230;)</p>
<p><a title="Kruskal Processing example" href="http://www.rickigregersen.com/wp-content/code/kruskal/" target="_blank">Here is the Processing code for the sketch shown at the beginning</a></p>
<p>There are 101 vertices, this adds up to 5050 possible edges, they are drawn in using a transparant white. The MST will be the red path as in the picture in the start of the post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/03/18/kruskals-algorithm-in-processing/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Hello world!</title>
		<link>http://www.rickigregersen.com/2009/03/18/hello-world/</link>
		<comments>http://www.rickigregersen.com/2009/03/18/hello-world/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 15:31:00 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://rickigregersen.com/?p=1</guid>
		<description><![CDATA[Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!]]></description>
			<content:encoded><![CDATA[<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/03/18/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
