<?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>Pixel graphics and performance</title>
		<link>http://kogics.wikidot.com/forum/t-1015480/pixel-graphics-and-performance</link>
		<description>Posts in the discussion thread &quot;Pixel graphics and performance&quot; - Is Kojo inherently slow or unsuitable for pixel graphics?</description>
				<copyright></copyright>
		<lastBuildDate>Mon, 10 Aug 2026 10:10:33 +0000</lastBuildDate>
		
					<item>
				<guid>http://kogics.wikidot.com/forum/t-1015480#post-2104218</guid>
				<title>Re: Pixel graphics and performance</title>
				<link>http://kogics.wikidot.com/forum/t-1015480/pixel-graphics-and-performance#post-2104218</link>
				<description></description>
				<pubDate>Sun, 07 Sep 2014 09:48:47 +0000</pubDate>
				<wikidot:authorName>lalitp</wikidot:authorName>				<wikidot:authorUserId>66813</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>Everything that's drawn in Kojo goes into a scene graph. This is really inefficient for pixel graphics, so there is indeed a recommended way to do performant pixel graphics. The basic idea is that you create a raw image:</p> <div class="code"> <pre><code>val img = image(200, 200)</code></pre></div> <p>And then set the individual pixels in the image.</p> <div class="code"> <pre><code>for (x &lt;- 0 until 200; y &lt;- 0 until 200) { val clr = Color(r.get(x, y), g.get(x, y), b.get(x, y)) setImagePixel(img, x, y, clr) }</code></pre></div> <p>Once your image is ready, you need to ask Kojo to draw it (i.e. put it into the scene graph). There are two ways to do this. The first is via the Turtle API:</p> <div class="code"> <pre><code>setCostumeImage(img) // or create a new turtle with the image as its costume</code></pre></div> <p>The second is via Pictures (Kojo's functional graphics API)</p> <div class="code"> <pre><code>val pic = PicShape.image(img) draw(pic)</code></pre></div> <p>Raw images are unfortunately not supported in the Staging API (which has not seen any enhacements for the past couple of years).</p> <p>Here's your code, reworked to work with raw images and the Turtle/Pictures APIs:</p> <div class="code"> <pre><code>class ColorGen { var map = Array.ofDim[Int](200, 200) map(0)(0) = random(256) for (i &lt;- 1 until 200) { map(i)(0) = if (random(5) == 0) random(256) else map(i - 1)(0) map(0)(i) = if (random(5) == 0) random(256) else map(0)(i - 1) } def get(i: Int, j: Int): Int = { if (map(i)(j) == 0) { val d = random(3) + 1 map(i)(j) = if (random(1000) == 0) random(256) else { get((200 + i - (d &amp; 1)) % 200, (200 + j - (d &gt;&gt; 1)) % 200) } } map(i)(j) } } val r = new ColorGen val g = new ColorGen val b = new ColorGen val img = image(200, 200) for (x &lt;- 0 until 200; y &lt;- 0 until 200) { val clr = Color(r.get(x, y), g.get(x, y), b.get(x, y)) setImagePixel(img, x, y, clr) } // draw image via Turtle api //clear() //setCostumeImage(img) // draw image via Pictures api cleari() setBackground(black) val pic = PicShape.image(img) draw(pic)</code></pre></div> <p>Edit. call <tt>setBackground()</tt> after <tt>clear()</tt> in the reworked code to make it have an effect.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://kogics.wikidot.com/forum/t-1015480#post-2104163</guid>
				<title>Pixel graphics and performance</title>
				<link>http://kogics.wikidot.com/forum/t-1015480/pixel-graphics-and-performance#post-2104163</link>
				<description></description>
				<pubDate>Sun, 07 Sep 2014 07:49:29 +0000</pubDate>
				<wikidot:authorName>Dmitry</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>I've been evaluating Kojo for the purposes of introducing my kids to programming. Yesterday I tried to rewrite <a href="http://codegolf.stackexchange.com/a/35626">this example</a> from the Tweetable Mathematical Art contest on codegolf.stackexchange.com. It works, but is extremely slow - painting a 200x200 square takes over 20 seconds. (I had to paint dot-by-dot because I could not figure out if there is a pixel graphics API - if there is, a pointer would be appreciated.)</p> <div class="code"> <pre><code>import Staging._ import Staging.{screenSize, dot} reset screenSize(200, 200) println(screenWidth) background(black) class ColorGen { var map = Array.ofDim[Int](200, 200) map(0)(0) = random(256) for (i &lt;- 1 until 200) { map(i)(0) = if (random(5) == 0) random(256) else map(i-1)(0) map(0)(i) = if (random(5) == 0) random(256) else map(0)(i-1) } def get(i: Int, j: Int): Int = { if (map(i)(j) == 0) { val d = random(3) + 1 map(i)(j) = if (random(1000) == 0) random(256) else {get((200+i-(d&amp;1)) % 200, (200+j-(d &gt;&gt; 1)) % 200)} } map(i)(j) } } val r = new ColorGen val g = new ColorGen val b = new ColorGen for (x &lt;- 0 until 200; y &lt;- 0 until 200) { stroke(color(r.get(x,y), g.get(x,y), b.get(x,y))) dot(x, y) } println(&quot;DONE&quot;)</code></pre></div> <p>I then did the same sample in <a href="http://processing.org">Processing</a> and found it capable of repainting a 200x200 square <em>over thirty times per second</em>.</p> <p>Does this happen because Kojo is inherently slower, or because it is not suitable for pixel graphics?</p> 
				 	]]>
				</content:encoded>							</item>
				</channel>
</rss>