{"id":312,"date":"2009-06-10T10:42:18","date_gmt":"2009-06-10T17:42:18","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1519"},"modified":"2009-06-10T10:42:18","modified_gmt":"2009-06-10T17:42:18","slug":"vectorpark-style-hemisphere","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=312","title":{"rendered":"Vectorpark Style Hemisphere"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nconst TWO_PI:Number = Math.PI * 2;\r\nconst PI_HALF_PI:Number = Math.PI + Math.PI \/ 2;\r\nx = stage.stageWidth \/ 2;\r\ny = stage.stageHeight \/ 2;\r\n\r\nvar runHemisphere:Function = makeHemisphere(0,0, 100,0xFF0000, 0xFFCC00);\r\nvar dx:Number = 0;\r\nvar dy:Number = 0;\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\tdx += (mouseX \/ 50 - dx) \/ 4;\r\n\tdy += (mouseY - dy) \/ 4;\r\n\trunHemisphere(dx, dy);\r\n}\r\nfunction makeHemisphere(x:Number, y:Number, size:Number, colA:uint, colB:uint):Function{\r\n\tvar s:Sprite = Sprite(addChild(new Sprite()));\r\n\ts.x = x;\r\n\ts.y = y;\r\n\tvar bg:Sprite = Sprite(s.addChild(new Sprite()));\r\n\t\r\n\twith (bg.graphics) clear(), beginFill(colA), halfCircle(bg.graphics, 0,0 , size);\r\n\tvar scol:uint = colB;\r\n\tvar slice:Sprite = Sprite(s.addChild(new Sprite()));\r\n\treturn function(theta:Number, rot:Number){\r\n\t\t theta %= TWO_PI;\r\n\t\tif (theta &lt; 0){\r\n\t\t\ttheta = TWO_PI - -theta;\r\n\t\t}\r\n\t\tvar scale:Number = Math.cos(theta);\r\n\t\tif (theta &gt; 1.57){\r\n\t\t\tscol = colA;\r\n\t\t} else {\r\n\t\t\tscol = colB;\r\n\t\t}\r\n\t\tif (theta &gt; Math.PI){\r\n\t\t\tbg.rotation = 180;\r\n\t\t}else{\r\n\t\t\tbg.rotation = 0;\r\n\t\t}\r\n\t\tif (theta &gt; PI_HALF_PI){\r\n\t\t\tscol = colB;\r\n\t\t}\r\n\t\twith(slice.graphics) clear(), beginFill(scol), scaleYcircle(slice.graphics,size, scale);\r\n\t\ts.rotation = rot;\r\n\t}\r\n}\r\n\/\/ original circle function by senocular (www.senocular.com) from here http:\/\/www.actionscript.org\/forums\/showthread.php3?s=&amp;threadid=30328\r\nfunction halfCircle(g:Graphics, x:Number,y:Number,r:Number):void {\r\n\tvar c1:Number = r * (Math.SQRT2 - 1);\r\n\tvar c2:Number = r * Math.SQRT2 \/ 2;\r\n\tvar xr:Number = x + r, yr:Number = y + r;\r\n\tvar x_r:Number = x - r, yc1:Number = y + c1;\r\n\tvar yc2:Number = y + c2, xc1:Number = x + c1, xc2:Number = x + c2;\r\n\tvar x_c1:Number = x - c1;\r\n\tg.moveTo(xr, y);\r\n\tg.curveTo(xr, yc1, xc2, yc2);\r\n\tg.curveTo(xc1, yr, x, yr);\r\n\tg.curveTo(x - c1,yr, x - c2, yc2);\r\n\tg.curveTo(x_r, yc1, x_r, y);\r\n}\r\n\/\/ circle that can be scaled on the y axis\r\nfunction scaleYcircle(g:Graphics, r:Number, s:Number = 1):void {\r\n\tvar c1:Number = r * (Math.SQRT2 - 1);\r\n\tvar c2:Number = r * Math.SQRT2 \/ 2;\r\n\tvar rs:Number = r * s, c1s:Number = c1 * s, c2s:Number = c2 * s;\r\n\tvar x_r:Number =  -r, y_r:Number = -rs, x_c2:Number =  -c2;\r\n\tvar y_c2:Number =  -c2s, x_c1:Number =  -c1, y_c1:Number =  -c1s\r\n\tg.moveTo(r, 0), g.curveTo(r, c1s, c2, c2s);\r\n\tg.curveTo(c1, rs, 0, rs), g.curveTo(x_c1,rs, x_c2, c2s);\r\n\tg.curveTo(x_r, c1s, x_r, 0), g.curveTo(x_r,y_c1,x_c2,y_c2);\r\n\tg.curveTo(x_c1,y_r,0,y_r), g.curveTo(c1,y_r,c2,y_c2);\r\n\tg.curveTo(r,y_c1,r,0);\r\n}\r\n<\/pre>\n<p>The above snippet draws a 3D hemisphere using a technique I first saw at <a href=\"http:\/\/www.vectorpark.com\/\" target=\"blank\">vectorpark.com<\/a>. <a href=\"http:\/\/www.vectorpark.com\/\" target=\"blank\">Vectorpark.com<\/a> has been around for years and it&#8217;s an amazing site&#8230; if you&#8217;ve never seen it before I highly recommend spending some time there&#8230;<\/p>\n<p>When I decided to post the half-circle snippet yesterday&#8230; I knew I would be writing this today. It&#8217;s one of those things I&#8217;ve always thought about doing, but never gotten around to. I show <a href=\"http:\/\/www.vectorpark.com\/\"  target=\"blank\">vectorpark.com<\/a> to my animation class (almost no coding in that class) and we emulate some of the 3D techniques for simple primitives with pure timeline animation. The above is the first time I&#8217;ve tried to emulate one of these techniques with code.<\/p>\n<p>I was pleasantly surprised when visiting <a href=\"http:\/\/www.vectorpark.com\/\">vectorpark.com<\/a> this morning&#8230; Patrick Smith has posted <a href=\"http:\/\/windosill.com\/online\/\">some new work there<\/a>. Really excellent stuff&#8230;<\/p>\n<p>UPDATE:<br \/>\nKatopz converted this to a<a href=\"http:\/\/wonderfl.kayac.com\/code\/62b8340fc4edf6729746f077b504bb8ef994a972\" target=\"blank\"> doc class and put it up on wonderfl&#8230;<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>const TWO_PI:Number = Math.PI * 2; const PI_HALF_PI:Number = Math.PI + Math.PI \/ 2; x = stage.stageWidth \/ 2; y = stage.stageHeight \/ 2; var runHemisphere:Function = makeHemisphere(0,0, 100,0xFF0000, 0xFFCC00); var dx:Number = 0; var dy:Number = 0; addEventListener(Event.ENTER_FRAME, onLoop); function onLoop(evt:Event):void { dx += (mouseX \/ 50 &#8211; dx) \/ 4; dy += (mouseY [&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,33],"tags":[3,14,4],"class_list":["post-312","post","type-post","status-publish","format-standard","hentry","category-3d","category-graphics","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/312","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=312"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/312\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}