<?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>mim.Armand &#187; buckets</title>
	<atom:link href="http://armand.eu/blog/tag/buckets/feed/" rel="self" type="application/rss+xml" />
	<link>http://armand.eu/blog</link>
	<description>Artist, inventor and developer!</description>
	<lastBuildDate>Wed, 24 Jun 2015 12:02:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.2.2</generator>
	<item>
		<title>A Simple Spatial Hashing (HashGrid) for processing-2</title>
		<link>http://armand.eu/blog/a-simple-spatial-hashing-hashgrid-for-processing-2/</link>
		<comments>http://armand.eu/blog/a-simple-spatial-hashing-hashgrid-for-processing-2/#comments</comments>
		<pubDate>Mon, 23 Jun 2014 12:54:44 +0000</pubDate>
		<dc:creator><![CDATA[mim]]></dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[Processing]]></category>
		<category><![CDATA[#Grid]]></category>
		<category><![CDATA[buckets]]></category>
		<category><![CDATA[HashGrid]]></category>
		<category><![CDATA[HashGrid class]]></category>
		<category><![CDATA[OPP]]></category>
		<category><![CDATA[performance improvement]]></category>
		<category><![CDATA[Spatial grid]]></category>
		<category><![CDATA[Spatial Hashing]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://armand.eu/blog/?p=52</guid>
		<description><![CDATA[download, contribute or fork this project here! Continuing from our last post, We were just ready to create our grid &#8220;buckets&#8221;! Buckets are small rectangles forming the grid; objects corresponding to each bucket location will be saved (a reference to them) in that bucket. To create buckets (and our grid) we need to divide the &#8230; <a href="http://armand.eu/blog/a-simple-spatial-hashing-hashgrid-for-processing-2/" class="more-link">Continue reading <span class="screen-reader-text">A Simple Spatial Hashing (HashGrid) for processing-2</span> <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><small>download, contribute or fork this project <a title="spatial hash grid" href="https://github.com/mim-Armand/HashGrid" target="_blank">here</a>!</small></p>
<figure id="attachment_57" style="width: 474px;" class="wp-caption aligncenter"><a href="http://armand.eu/blog/wp-content/uploads/2014/06/Spatial_HashGrid_02.jpg"><img class="size-large wp-image-57" src="http://armand.eu/blog/wp-content/uploads/2014/06/Spatial_HashGrid_02-1024x455.jpg" alt="SPatial Hash Grid System" width="474" height="210" /></a><figcaption class="wp-caption-text">Testing the functionality of the class</figcaption></figure>
<p>Continuing from our <a title="Simple Spatial Hashing for Processing (Part 1)" href="http://armand.eu/blog/a-simple-spatial-hashing-hashgrid-for-processing-1/" target="_blank">last post</a>, We were just ready to create our grid &#8220;buckets&#8221;!<br />
Buckets are small rectangles forming the grid; objects corresponding to each bucket location will be saved (a reference to them) in that bucket.<br />
To create buckets (and our grid) we need to divide the size of our target canvas (it can be our stage size or a virtual stage) to create smaller rectangles, then we will need just to check each object against other objects only in the same bucket or neighbor buckets which will dramatically reduce the required number of iterations. but it&#8217;s important to decide about the bucket sizes according to our needs (number of objects, desired distance to check&#8230;) and since variables affecting or decision here aren&#8217;t constant, it&#8217;s better to make it flexible so we can decide about the bucket size later! we will start with defining a float variable that will dictate desired size of our bucket to the class, if it&#8217;s not defined when calling the class it can be created automatically according to number of objects and requested distance check etc. but for now &#8230; let&#8217;s keep simple!<br />
Also, we would want (I imagine at this point) two methods:<br />
1. obj.set() &#8211;&gt; Objects will declare their position to class and get referenced in corresponding buckets.<br />
2. obj.get() &#8211;&gt; which will return an array of references in all the neighbors in the  requested distance.</p>
<p>Here to check the functionality of our Spatial hash grid class I&#8217;ll use a <a title="Auto generated cover page using Processing" href="http://armand.eu/blog/auto_cover_processing_01/" target="_blank">simple class I wrote before</a>, which just generates bunch of random point objects on the stage, so we can put our class under some definable load, the<a title="Points class gist" href="https://gist.github.com/mim-Armand/5169c185aecc43ab927e" target="_blank"> Points class</a> is simple (and anyway not the subject of this topic, but let me know if you want know more about it!):</p>
<p><script src="https://gist.github.com/mim-Armand/5169c185aecc43ab927e.js"></script>So! here is the structure of our HashGrid class:<script src="https://gist.github.com/mim-Armand/b89a1846075d984b282c.js"></script><br />
Here we just declared the class and bunch of variables and methods we know we&#8217;re gonna need.<br />
There is also a displayGrid() function that will just draw our grid for us on the display so it&#8217;d be easier to understand and to debug.<br />
But we have defined another class inside the HashGrid class as well (a <a title="Nested Classes (Oracle Reference pages)" href="http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html" target="_blank">nested class</a>) near the end of this code snippet, Buckets class. that&#8217;s because in Java you should define the array type in declaration time and since our array will keep reference to Objects and there is not such a primitive called &#8220;Object&#8221; you need to define the object to be able to declare an array of that kine of objects, so we create a nested class called buckets to keep our bucket arrays and an array of our buckets in it!<br />
Also you may notice that I hard coded another object type in my HashGrid class (The Points) which is not the best practice since we want to be able to use this class everywhere with no modifications, but for now and to keep it as simple as possible we are gonna leave it like that, but later on (in another post hopefully if I could) we will make it more abstract by using an <a title="Oracle Java interface reference page" href="http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html" target="_blank">interface </a>or some other techniques.</p>
<p>Now we need to create an array of buckets, create an array in each bucket (to keep our object references) and populate our set() and get() functions:<br />
<script src="https://gist.github.com/mim-Armand/a7c907f84dbbdd8af772.js"></script>Now our HashGrid class is all functional! awesome! but there is a few things still we need to do, some of them are relatively easy to implement, like adding the functionality to be able to define the desired distance to check (checking neighbors as well as the bucket itself) and also the Interface, we are gonna work on these later-on (hopefully!) in another post. But now to test our class we can create a new processing file (tab) and put <a title="test the class" href="https://gist.github.com/mim-Armand/3dfab04834484ae2f34f" target="_blank">this</a> code on it:<script src="https://gist.github.com/mim-Armand/3dfab04834484ae2f34f.js"></script><br />
Yep! it works! you can download, contribute or fork the whole project <a title="An spatial hash grid implementation" href="https://github.com/mim-Armand/HashGrid" target="_blank">here </a>at Github:<br />
https://github.com/mim-Armand/HashGrid</p>
]]></content:encoded>
			<wfw:commentRss>http://armand.eu/blog/a-simple-spatial-hashing-hashgrid-for-processing-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
