<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wikidot="http://www.wikidot.com/rss-namespace">

	<channel>
		<title>Another example, with sq and dist methods</title>
		<link>http://kogics.wikidot.com/forum/t-229555/another-example-with-sq-and-dist-methods</link>
		<description>Posts in the discussion thread &quot;Another example, with sq and dist methods&quot; - A straight port of the Array2D example from Processing.  The most interesting part is the sq and dist methods, which should be generic enough (requires 2.8, I think) and possibly efficient (I&#039;m not implementation-savvy in the Java environment).</description>
				<copyright></copyright>
		<lastBuildDate>Sat, 06 Jun 2026 10:48:23 +0000</lastBuildDate>
		
					<item>
				<guid>http://kogics.wikidot.com/forum/t-229555#post-736280</guid>
				<title>Re: Another example, with sq and dist methods</title>
				<link>http://kogics.wikidot.com/forum/t-229555/another-example-with-sq-and-dist-methods#post-736280</link>
				<description></description>
				<pubDate>Sat, 27 Mar 2010 19:24:52 +0000</pubDate>
				<wikidot:authorName>Peter Lewerin</wikidot:authorName>				<wikidot:authorUserId>459742</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <blockquote> <p>Cool. A lot of functional programming in there?</p> </blockquote> <p>Kinda, sorta. My first significant programming experience was with Lisp, and I've worked with SML some.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://kogics.wikidot.com/forum/t-229555#post-736075</guid>
				<title>Re: Another example, with sq and dist methods</title>
				<link>http://kogics.wikidot.com/forum/t-229555/another-example-with-sq-and-dist-methods#post-736075</link>
				<description></description>
				<pubDate>Sat, 27 Mar 2010 12:42:52 +0000</pubDate>
				<wikidot:authorName>lalitp</wikidot:authorName>				<wikidot:authorUserId>66813</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <blockquote> <p>I do have about 30 years of experience with programming, though :)</p> </blockquote> <p>Cool. A lot of functional programming in there?</p> <blockquote> <p>I've cloned the source to have a look at the innards, so hopefully I'll be able to figure out commands on my<br /> own in the future.</p> </blockquote> <p>Feel free to continue asking about anything that you find unclear.</p> <p>If you have any problems working with the source, please ask here (on the just created kojo-dev forum):<br /> <a href="http://www.kogics.net/forum/c-110283/kojo-dev">http://www.kogics.net/forum/c-110283/kojo-dev</a></p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://kogics.wikidot.com/forum/t-229555#post-736008</guid>
				<title>Re: Another example, with sq and dist methods</title>
				<link>http://kogics.wikidot.com/forum/t-229555/another-example-with-sq-and-dist-methods#post-736008</link>
				<description></description>
				<pubDate>Sat, 27 Mar 2010 09:51:44 +0000</pubDate>
				<wikidot:authorName>Peter Lewerin</wikidot:authorName>				<wikidot:authorUserId>459742</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>I was thinking that this kind of generic declaration would be helpful to smooth out the learning curve a bit. I remember from my teaching days that the difference between integers and FPNs bothered my students, and the difference between floats and doubles baffled them. Still, there seems to be some pitfalls here.</p> <p>I might add that I'm a bit new with Scala; I've only used it for a year or so. I do have about 30 years of experience with programming, though :)</p> <p>I've cloned the source to have a look at the innards, so hopefully I'll be able to figure out commands on my own in the future.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://kogics.wikidot.com/forum/t-229555#post-735836</guid>
				<title>Re: Another example, with sq and dist methods</title>
				<link>http://kogics.wikidot.com/forum/t-229555/another-example-with-sq-and-dist-methods#post-735836</link>
				<description></description>
				<pubDate>Sat, 27 Mar 2010 03:25:24 +0000</pubDate>
				<wikidot:authorName>lalitp</wikidot:authorName>				<wikidot:authorUserId>66813</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>Keep the good stuff coming.</p> <p>I would think that the technique of using an implicit parameter to keep your methods generic should work with both scala 2.7 and 2.8</p> <p>The 2.8 @specialize stuff might be able to make such method efficient for primitives.</p> <p>fgClear() is to clear the foreground. It allows you to clear just the foreground (while keeping the background) within a Canvas animation.</p> <p>An example of how to use fgClear() is available in the Samples included with Kojo:<br /> Samples -&gt; Early Preview -&gt; Canvas -&gt; Sine of an Angle</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://kogics.wikidot.com/forum/t-229555#post-734023</guid>
				<title>Another example, with sq and dist methods</title>
				<link>http://kogics.wikidot.com/forum/t-229555/another-example-with-sq-and-dist-methods#post-734023</link>
				<description></description>
				<pubDate>Thu, 25 Mar 2010 09:09:01 +0000</pubDate>
				<wikidot:authorName>Peter Lewerin</wikidot:authorName>				<wikidot:authorUserId>459742</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <div class="code"> <pre><code>import java.awt.Color._ def sq[A](x: A)(implicit n: Numeric[A]): A = n.times(x, x) def dist[A](x0: A, y0: A, x1: A, y1: A)(implicit n: Numeric[A]): A = { val v = n.plus(sq(n.minus(x0, x1)), sq(n.minus(y0, y1))) val r = Math.sqrt(n.toDouble(v)) if (v.isInstanceOf[Int]) r.toInt.asInstanceOf[A] else if (v.isInstanceOf[Float]) r.toFloat.asInstanceOf[A] else r.asInstanceOf[A] } clear invisible Canvas.fgClear //? val width = 200 val height = 200 // background(0) val maxDistance = dist(width/2, height/2, width, height) val distances = Seq.tabulate(width, height){ case (i, j) =&gt; dist(width/2f, height/2f, j, i)/maxDistance * 255 } for (i &lt;- 0 until(height, 2) ; j &lt;- 0 until(width, 2)) { val c = distances(j)(i).toInt Canvas.setPenColor(new Color(c, c, c)) Canvas.point(j, i) }</code></pre></div> 
				 	]]>
				</content:encoded>							</item>
				</channel>
</rss>