{"id":337,"date":"2009-07-06T13:31:04","date_gmt":"2009-07-06T20:31:04","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1710"},"modified":"2009-07-06T13:31:04","modified_gmt":"2009-07-06T20:31:04","slug":"utils3dprojectvectors-lathe","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=337","title":{"rendered":"Utils3D.projectVectors() Lathe"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;SWF(width = 500, height = 500, backgroundColor = 0x000000)]\r\nvar halfWidth:Number = stage.stageWidth \/ 2;\r\nvar halfHeight:Number = stage.stageHeight \/ 2;\r\nvar loc:Vector.&lt;Number&gt;;\r\n\r\ngraphics.lineStyle(0, 0xFF0000);\r\ngraphics.moveTo(halfWidth, 0);\r\ngraphics.lineTo(halfWidth, stage.stageHeight);\r\ngraphics.moveTo(0, halfHeight);\r\ngraphics.lineTo(stage.stageWidth, halfHeight);\r\n\r\nvar line:Shape = Shape(addChild(new Shape()));\r\nline.x = halfWidth;\r\n\r\nvar idle:Function = function(){};\r\nvar currentMode:Function = idle;\r\nstage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);\r\nstage.addEventListener(MouseEvent.MOUSE_UP, onUp);\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onDown(evt:MouseEvent):void{\r\n\tif (contains(frame)){\r\n\t\tremoveChild(frame);\r\n\t\tcurrentMode = idle;\r\n\t\tline.graphics.clear();\r\n\t\treturn;\r\n\t}\r\n\tloc = new Vector.&lt;Number&gt;();\r\n\tline.graphics.lineStyle(0,0xFFFFFF);\r\n\tline.x = halfWidth\r\n\tline.y = halfHeight;\r\n\tline.graphics.moveTo(line.mouseX, line.mouseY);\r\n\tcurrentMode = captureLocs;\r\n\tcanvas.fillRect(canvas.rect, 0x000000);\r\n}\r\nfunction onUp(evt:MouseEvent):void{\r\n\tif (currentMode == idle) return;\r\n\tsetupLathe();\r\n\tcurrentMode = showLathe;\r\n}\r\n\r\nfunction onLoop(evt:Event):void{\r\n\tcurrentMode();\r\n}\r\nfunction captureLocs():void{\r\n\tloc.push(line.mouseX);\r\n\tloc.push(line.mouseY);\r\n\tloc.push(0);\r\n\tline.graphics.lineTo(line.mouseX, line.mouseY);\r\n}\r\n\/**\r\n -- Lathe Stuff:\r\n*\/\r\nvar canvas:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight,false, 0x000000);\r\nvar frame:Bitmap = new Bitmap(canvas);\r\nvar dx:Number=0;\r\nvar dy:Number=0;\r\n var matrix:Matrix3D = new Matrix3D();\r\nvar pVerts:Vector.&lt;Number&gt;;\r\nvar uvts:Vector.&lt;Number&gt;;\r\nfunction setupLathe():void{\r\n\taddChild(frame);\r\n\tpVerts =  new Vector.&lt;Number&gt;();\r\n\tuvts = new Vector.&lt;Number&gt;();\r\n\tvar nVerts:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;();\r\n\tvar tVerts:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;();\r\n\tmatrix.identity();\r\n\tvar step:Number = 2;\r\n\tfor (var i:int = 0; i &lt;360; i+=step){\r\n\t\tmatrix.appendRotation(step,Vector3D.Y_AXIS);\r\n\t\tmatrix.transformVectors(loc, tVerts);\r\n\t\tnVerts = nVerts.concat(tVerts);\r\n\t}\r\n\tloc = nVerts.concat();\r\n}\r\nfunction showLathe():void{\r\n\tdx += (mouseX - dx)\/4;\r\n\tdy += (mouseY - dy)\/4;\r\n\tmatrix.identity();\r\n\tmatrix.appendRotation(dy,Vector3D.X_AXIS);\r\n\tmatrix.appendRotation(dx,Vector3D.Y_AXIS);\r\n\tmatrix.appendTranslation(halfWidth, halfHeight, 0);\r\n\tUtils3D.projectVectors(matrix, loc, pVerts, uvts);\r\n\tcanvas.lock();\r\n\tcanvas.fillRect(canvas.rect, 0x000000);\r\n\tvar leng:int = pVerts.length;\r\n\tfor (var i:int = 0; i&lt;leng; i+=2){\r\n\t\tcanvas.setPixel( pVerts&#x5B;i], pVerts&#x5B;i + 1], 0xFFFFFF);\r\n\t}\r\n\tcanvas.unlock();\r\n}\r\n<\/pre>\n<p>This snippet allows you to create 3D lathe shapes by drawing a 2D line. This is done using Utils3D.projectVectors() and Matrix.transformVectors().<br \/>\n<a data-asruffle-linked=\"true\" href=\"https:\/\/actionsnippet.com\/swfs\/lathe.html\" target=\"blank\"><br \/>\nHave a look at the swf here&#8230;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/lathe.jpg\" \/><\/a> <span class=\"asruffle-link\" data-swf=\"{&quot;url&quot;:&quot;https:\\\/\\\/actionsnippet.com\\\/swfs\\\/lathe.swf&quot;,&quot;title&quot;:&quot;lathe&quot;,&quot;width&quot;:500,&quot;height&quot;:500,&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\">: lathe<\/span><\/button><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x5B;SWF(width = 500, height = 500, backgroundColor = 0x000000)] var halfWidth:Number = stage.stageWidth \/ 2; var halfHeight:Number = stage.stageHeight \/ 2; var loc:Vector.&lt;Number&gt;; graphics.lineStyle(0, 0xFF0000); graphics.moveTo(halfWidth, 0); graphics.lineTo(halfWidth, stage.stageHeight); graphics.moveTo(0, halfHeight); graphics.lineTo(stage.stageWidth, halfHeight); var line:Shape = Shape(addChild(new Shape())); line.x = halfWidth; var idle:Function = function(){}; var currentMode:Function = idle; stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown); stage.addEventListener(MouseEvent.MOUSE_UP, onUp); addEventListener(Event.ENTER_FRAME, onLoop); [&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,114,121,118,26,79],"tags":[3,14,4],"class_list":["post-337","post","type-post","status-publish","format-standard","hentry","category-3d","category-bitmapdata","category-graphics-algorithms","category-math","category-matrix","category-setpixel","category-vector","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/337","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=337"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/337\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}