{"id":459,"date":"2012-01-15T21:44:45","date_gmt":"2012-01-15T18:44:45","guid":{"rendered":"http:\/\/unitycoder.com\/blog\/?p=459"},"modified":"2012-01-19T20:04:06","modified_gmt":"2012-01-19T17:04:06","slug":"xiaolin-wus-line-algorithm-to-unity-javascript","status":"publish","type":"post","link":"https:\/\/unitycoder.com\/blog\/2012\/01\/15\/xiaolin-wus-line-algorithm-to-unity-javascript\/","title":{"rendered":"Xiaolin Wu&#8217;s line algorithm to Unity Javascript"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"460\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2012\/01\/15\/xiaolin-wus-line-algorithm-to-unity-javascript\/mlines1\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mlines1.jpg?fit=680%2C512&amp;ssl=1\" data-orig-size=\"680,512\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"mlines1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mlines1.jpg?fit=680%2C512&amp;ssl=1\" class=\"alignnone size-full wp-image-460\" title=\"mlines1\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mlines1.jpg?resize=680%2C512\" alt=\"\" width=\"680\" height=\"512\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mlines1.jpg?w=680&amp;ssl=1 680w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mlines1.jpg?resize=300%2C225&amp;ssl=1 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/p>\n<p>Converted this function to Unity3D Javascript : <a href=\"http:\/\/en.wikipedia.org\/wiki\/Xiaolin_Wu%27s_line_algorithm\" target=\"_blank\">http:\/\/en.wikipedia.org\/wiki\/Xiaolin_Wu%27s_line_algorithm<\/a><br \/>\n(Still has few bugs left..see that vertical line in screenshot..)<\/p>\n<p><strong>Webplayer:<\/strong><br \/>\ncoming later<br \/>\nCan anyone find the error in this code? Why doesnt it draw vertical lines..?<br \/>\n!note, couple lines are commented out, swapping x&lt;&gt;y,<br \/>\nif I enable that, line position doesnt match with mouse click.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/ Xiaolin Wu's line algorithm - converted to unity- mgear - http:\/\/unitycoder.com\/blog\r\n\/\/ original source: http:\/\/en.wikipedia.org\/wiki\/Xiaolin_Wu%27s_line_algorithm\r\n\/\/ *attach this scrip to a plane, then draw with mouse: click startpoint, click endpoint\r\n\r\nprivate var texture:Texture2D;\r\npublic var texture_width:int=256;\r\npublic var texture_height:int=256;\r\nprivate var point1:boolean = false;\r\nprivate var point2:boolean = false;\r\nprivate var startpoint:Vector2;\r\nprivate var endpoint:Vector2;\r\n\r\nfunction Start()\r\n{\r\ntexture = new Texture2D (texture_width, texture_height);\r\nrenderer.material.mainTexture = texture;\r\n}\r\n\r\nfunction Update()\r\n{\r\n\/\/ draw lines with mouse\r\nif (Input.GetMouseButtonDown(0))\r\n{\r\nvar ray:Ray = Camera.main.ScreenPointToRay(Input.mousePosition);\r\nvar hit:RaycastHit;\r\nif (Physics.Raycast(ray,hit, 100))\r\n{\r\nif (!point1)\r\n{\r\npixelUV = hit.textureCoord;\r\npixelUV.x *= texture.width;\r\npixelUV.y *= texture.height;\r\nstartpoint = Vector2(pixelUV.x, pixelUV.y);\r\npoint1=true;\r\n\r\nprint (startpoint);\r\n}else{\r\npixelUV = hit.textureCoord;\r\npixelUV.x *= texture.width;\r\npixelUV.y *= texture.height;\r\nendpoint = Vector2(pixelUV.x, pixelUV.y);\r\npoint1=false;\r\ndrawLine(startpoint.x,startpoint.y,endpoint.x,endpoint.y);\r\nprint (endpoint);\r\n}\r\n}\r\n}\r\n}\r\n\r\n\r\n\/\/ the actual code from wiki :\r\n\r\nfunction plot(x, y, c)\r\n{\r\ntexture.SetPixel (x,y,Color(c,c,c,1));\r\n}\r\n\r\nfunction ipart(x)\r\n{\r\nreturn Mathf.Floor(x);\r\n}\r\n\r\nfunction round(x)\r\n{\r\nreturn Mathf.RoundToInt(x+0.5);\r\n}\r\n\r\nfunction fpart(x)\r\n{\r\nreturn x - ipart(x);\r\n}\r\n\r\nfunction rfpart(x)\r\n{\r\nreturn 1 - fpart(x);\r\n}\r\n\r\nfunction drawLine(x1,y1,x2,y2)\r\n{\r\nvar dx:float = x2 - x1;\r\nvar dy:float = y2 - y1;\r\nif (Mathf.Abs(dx) &lt; Mathf.Abs(dy))\r\n{\r\n\/\/ ** Commented these out, I dont get it how \/ why x &amp; y should be swapped..?\r\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0 tmp = x1; x1=x2; x2=tmp;\r\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0 tmp = y1; y1=y2; y2=tmp;\r\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0 tmp = dx; dx=dy; dy=tmp;\r\n}\r\nif (x2 &lt; x1)\r\n{\r\ntmp = x1; x1=x2; x2=tmp;\r\ntmp = y1; y1=y2; y2=tmp;\r\n}\r\ngradient = dy \/ dx;\r\n\r\n\/\/ handle first endpoint\r\nxend = round(x1);\r\nyend = y1 + gradient * (xend - x1);\r\nxgap = rfpart(x1 + 0.5);\r\nxpxl1 = xend;\u00a0 \/\/ this will be used in the main loop\r\nypxl1 = ipart(yend);\r\nplot(xpxl1, ypxl1, rfpart(yend) * xgap);\r\nplot(xpxl1, ypxl1 + 1, fpart(yend) * xgap);\r\nintery = yend + gradient; \/\/ first y-intersection for the main loop\r\n\r\n\/\/ handle second endpoint\r\nxend = round (x2);\r\nyend = y2 + gradient * (xend - x2);\r\nxgap = fpart(x2 + 0.5);\r\nxpxl2 = xend;\u00a0 \/\/ this will be used in the main loop\r\nypxl2 = ipart (yend);\r\nplot (xpxl2, ypxl2, rfpart (yend) * xgap);\r\nplot (xpxl2, ypxl2 + 1, fpart (yend) * xgap);\r\n\r\n\/\/ main drawing loop\r\nfor (x=xpxl1+1;x&lt;xpxl2-1;x++)\r\n{\r\nplot (x, ipart (intery), rfpart (intery));\r\nplot (x, ipart (intery) + 1, fpart (intery));\r\nintery = intery + gradient;\r\n}\r\ntexture.Apply();\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Converted this function to Unity3D Javascript : http:\/\/en.wikipedia.org\/wiki\/Xiaolin_Wu%27s_line_algorithm (Still has few bugs left..see that vertical line in screenshot..) Webplayer: coming later Can anyone find the error in this code? Why doesnt it draw vertical lines..? !note, couple lines are commented out, swapping x&lt;&gt;y, if I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":460,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4,3],"tags":[93,76,121],"class_list":["post-459","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-demos","category-unity3d","tag-algorithm","tag-line","tag-xiaolin-wu"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/01\/mlines1.jpg?fit=680%2C512&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1KTaT-7p","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/comments?post=459"}],"version-history":[{"count":4,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/459\/revisions"}],"predecessor-version":[{"id":468,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/459\/revisions\/468"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media\/460"}],"wp:attachment":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media?parent=459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/categories?post=459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/tags?post=459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}