{"id":355,"date":"2009-07-23T09:32:58","date_gmt":"2009-07-23T16:32:58","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1826"},"modified":"2009-07-23T09:32:58","modified_gmt":"2009-07-23T16:32:58","slug":"sphere-of-squares","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=355","title":{"rendered":"Sphere of Squares"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;SWF(width = 600, height = 600, backgroundColor=0x000000)]\r\nvar squareNum:int  = 1000;\r\nvar hw:Number = stage.stageWidth \/ 2;\r\nvar hh:Number = stage.stageHeight \/ 2;\r\n\/\/ verts defines a single square\r\nvar verts:Vector.&lt;Number&gt; = Vector.&lt;Number&gt;(&#x5B;-20, 0, 0, 20, 0, 0, 20, 0, 40, -20, 0, 40, -20, 0, 0]);\r\nvar cmds:Vector.&lt;int&gt; = Vector.&lt;int&gt;(&#x5B;1,2,2,2,2]);\r\nvar tempVerts:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;();\r\nvar newVerts:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;();\r\nvar pVerts:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(10 * squareNum);\r\nvar uv:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(15 * squareNum);\r\nvar vectors:Shape = new Shape();\r\nvar canvas:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false, 0x000000);\r\naddChild(new Bitmap(canvas));\r\nvar blurred:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false, 0x000000);\r\nvar blur:BlurFilter = new BlurFilter(20,20,1);\r\n\r\nvar m:Matrix3D = new Matrix3D();\r\n\r\nvar radius:Number = 200;\r\n\/\/ duplicate the verts array a bunch of times\r\n\/\/ each time moving the square to a random place on the\r\n\/\/ circumference of a sphere\r\nfor (var i:int = 0; i&lt;squareNum; i++){\r\n\tm.identity();\r\n\tvar s:Number = Math.random()*.5 + .5;\r\n\tm.appendScale(s, s, s);\r\n\tm.appendRotation(90,Vector3D.X_AXIS);\r\n\tm.appendTranslation(0, 0, radius);\r\n\tm.appendRotation(Math.random()*360,Vector3D.X_AXIS);\r\n\tm.appendRotation(Math.random()*360,Vector3D.Y_AXIS);\r\n\tm.appendRotation(Math.random()*360,Vector3D.Z_AXIS);\r\n\tm.transformVectors(verts,tempVerts);\r\n\tnewVerts = newVerts.concat(tempVerts);\r\n\tcmds = cmds.concat(Vector.&lt;int&gt;(&#x5B;1,2,2,2,2]));\r\n}\r\nnewVerts.fixed = pVerts.fixed = uv.fixed = true;\r\nvar dx:Number = 0, dy:Number = 0;\r\nvar pnt:Point = new Point();\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\t   dx += (mouseX - dx) \/ 4;\r\n\t   dy += (mouseY - dy) \/ 4;\r\n\t   m.identity();\r\n\t   m.appendRotation(dx,Vector3D.Z_AXIS);\r\n\t   m.appendRotation(dy,Vector3D.X_AXIS);\r\n\t   m.appendTranslation(hw,hh, 0);\r\n\t   Utils3D.projectVectors(m, newVerts, pVerts, uv);\r\n\t   with(vectors.graphics){\r\n\t\t   clear();\r\n\t\t   beginFill(0xFFFFFF);\r\n\t\t   drawCircle(hw, hh, radius+10);\r\n\t\t   beginFill(0x000000);\r\n\t\t   drawPath(cmds, pVerts, GraphicsPathWinding.NON_ZERO);\r\n\t   }\r\n\t   canvas.fillRect(canvas.rect, 0x000000);\r\n\t   canvas.draw(vectors);\r\n\t   blurred.copyPixels(canvas, canvas.rect, pnt);\r\n\t   blurred.applyFilter(blurred,blurred.rect, pnt, blur);\r\n\t   canvas.draw(blurred, null, null, BlendMode.SCREEN);\r\n}\r\n<\/pre>\n<p>This snippet is similar to yesterdays post but the visual result is rather different. This one does a little more Matrix3D stuff resulting in a sphere made up entirely of squares. This is obscured by the size of the squares and the fact that they overlap and cut each other up. BitmapData a BlurFilter and a BlendMode give the entire thing a slight glow&#8230;<\/p>\n<p><a data-asruffle-linked=\"true\" href=\"https:\/\/actionsnippet.com\/swfs\/sphere_of_squares2.html\" target=\"blank\"><br \/>\nHave a look at the swf here&#8230;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/sphere_of_squares.jpg\" \/><\/a> <span class=\"asruffle-link\" data-swf=\"{&quot;url&quot;:&quot;https:\\\/\\\/actionsnippet.com\\\/swfs\\\/sphere_of_squares2.swf&quot;,&quot;title&quot;:&quot;sphere of squares2&quot;,&quot;width&quot;:600,&quot;height&quot;:600,&quot;missing&quot;:false,&quot;allowScriptAccess&quot;:false,&quot;backgroundColor&quot;:&quot;#000000&quot;}\"><button type=\"button\" hidden aria-expanded=\"false\">Play demo<span class=\"screen-reader-text\">: sphere of squares2<\/span><\/button><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x5B;SWF(width = 600, height = 600, backgroundColor=0x000000)] var squareNum:int = 1000; var hw:Number = stage.stageWidth \/ 2; var hh:Number = stage.stageHeight \/ 2; \/\/ verts defines a single square var verts:Vector.&lt;Number&gt; = Vector.&lt;Number&gt;(&#x5B;-20, 0, 0, 20, 0, 0, 20, 0, 40, -20, 0, 40, -20, 0, 0]); var cmds:Vector.&lt;int&gt; = Vector.&lt;int&gt;(&#x5B;1,2,2,2,2]); var tempVerts:Vector.&lt;Number&gt; = new [&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,79],"tags":[3,14,4,126],"class_list":["post-355","post","type-post","status-publish","format-standard","hentry","category-3d","category-bitmapdata","category-vector","tag-actionscript","tag-as3","tag-flash","tag-fp10"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/355","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=355"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/355\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}