{"id":137,"date":"2008-12-28T10:44:04","date_gmt":"2008-12-28T17:44:04","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=431"},"modified":"2008-12-28T10:44:04","modified_gmt":"2008-12-28T17:44:04","slug":"isometric-voxels","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=137","title":{"rendered":"Isometric Voxels"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/ isometric conversion\r\nvar centerX:Number=stage.stageWidth\/2;\r\nvar centerY:Number=stage.stageHeight\/2;\r\nvar theta:Number=Math.PI\/4;\/\/ 45 degrees;\r\nvar cosX:Number=Math.cos(theta);\r\nvar sinX:Number=Math.sin(theta);\r\nvar pnt:Point = new Point();\r\nfunction iso3D(x:Number, y:Number, z:Number):Point {\r\n\tpnt.x = centerX + (x-z) *  cosX\r\n\tpnt.y = centerY -  (x+z) * 0.5 * sinX - y;\r\n\treturn pnt;\r\n}\r\n\/\/ example:\r\nvar canvas:BitmapData=new BitmapData(stage.stageWidth,stage.stageHeight,true,0xFF000000);\r\naddChild(new Bitmap(canvas,&quot;auto&quot;,true));\r\nvar size:int=100;\r\nvar hs:int=size \/ 2;\r\nvar pen:Point = new Point();\r\nvar vect:Vector3D = new Vector3D();\r\n\/\/ draw a few shapes with offset:\r\nfor (var dist:int = 10; dist &lt;= 80; dist *= 2) {\r\n\t\/\/ voxel space:\r\n\tfor (var i:int = 0; i&lt;size; i++) {\r\n\t\tfor (var j:int = 0; j&lt;size; j++) {\r\n\t\t\tfor (var k:int = 0; k&lt;size; k++) {\r\n\t\t\t\tvect.x=j-hs;\r\n\t\t\t\tvect.y=i-hs;\r\n\t\t\t\tvect.z=k-hs;\r\n\t\t\t\tpen = iso3D(vect.x,vect.y,vect.z);\r\n\t\t\t\tif (Math.sqrt((vect.x * vect.x) + (vect.y * vect.y) + (vect.z * vect.z)) &lt;dist) {\r\n\t\t\t\t\t\/\/ using Vector3D.distance() is very slow compared to above\r\n\t\t\t\t\t\/\/ a few types of coloring:\r\n\t\t\t\t\tvar xp:Number = pen.x + (dist &lt;&lt;2) - 200;\r\n\t\t\t\t\tcanvas.setPixel(xp, pen.y-100, (i &lt;&lt;16 | j &lt;&lt;8 | k) &lt;&lt;1);\r\n\t\t\t\t\tcanvas.setPixel(xp, pen.y+100, (k &lt;&lt;16 | k &lt;&lt;8 | k+j)  );\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>The above will draw this:<\/p>\n<p><img src='https:\/\/actionsnippet.com\/imgs\/voxels.jpg' \/><\/p>\n<p>You can read more about voxels <a href='http:\/\/en.wikipedia.org\/wiki\/Voxel' target='blank'>here<\/a>.<\/p>\n<p><i>This isn&#8217;t the speediest way to do voxels (especially if you want to animate). This was just the first thing that came to mind.<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ isometric conversion var centerX:Number=stage.stageWidth\/2; var centerY:Number=stage.stageHeight\/2; var theta:Number=Math.PI\/4;\/\/ 45 degrees; var cosX:Number=Math.cos(theta); var sinX:Number=Math.sin(theta); var pnt:Point = new Point(); function iso3D(x:Number, y:Number, z:Number):Point { pnt.x = centerX + (x-z) * cosX pnt.y = centerY &#8211; (x+z) * 0.5 * sinX &#8211; y; return pnt; } \/\/ example: var canvas:BitmapData=new BitmapData(stage.stageWidth,stage.stageHeight,true,0xFF000000); addChild(new Bitmap(canvas,&quot;auto&quot;,true)); var size:int=100; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101,24,59,25,26],"tags":[3,4,77,105],"class_list":["post-137","post","type-post","status-publish","format-standard","hentry","category-3d","category-bitmapdata","category-color","category-pixel-manipulation","category-setpixel","tag-actionscript","tag-flash","tag-isometric","tag-voxels"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=137"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/137\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}