{"id":305,"date":"2009-06-03T11:07:07","date_gmt":"2009-06-03T18:07:07","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1494"},"modified":"2009-06-03T11:07:07","modified_gmt":"2009-06-03T18:07:07","slug":"distance-between-point-and-line-segment","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=305","title":{"rendered":"Distance Between Point and Line Segment"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/**\r\nOriginal function by Pieter Iserbyt:\r\nhttp:\/\/local.wasp.uwa.edu.au\/~pbourke\/geometry\/pointline\/DistancePoint.java\r\nfrom Paul Bourke&#039;s website:\r\nhttp:\/\/local.wasp.uwa.edu.au\/~pbourke\/geometry\/pointline\/\r\n*\/\r\nfunction pointToLineDistance(p1:Sprite, p2:Sprite, p3:Sprite):Number {\r\n\tvar xDelta:Number = p2.x - p1.x;\r\n\tvar yDelta:Number = p2.y - p1.y;\r\n\tif ((xDelta == 0) &amp;&amp; (yDelta == 0)) {\r\n\t\t\/\/ p1 and p2 cannot be the same point\r\n\t\tp2.x += 1;\r\n\t\tp2.y += 1;\r\n\t\txDelta = 1;\r\n\t        yDelta = 1;\r\n\t}\r\n\tvar u:Number = ((p3.x - p1.x) * xDelta + (p3.y - p1.y) * yDelta) \/ (xDelta * xDelta + yDelta * yDelta);\r\n\tvar closestPoint:Point;\r\n\tif (u &lt; 0) {\r\n\t    closestPoint = new Point(p1.x, p1.y);\r\n\t} else if (u &gt; 1) {\r\n\t    closestPoint = new Point(p2.x, p2.y);\r\n\t} else {\r\n\t    closestPoint = new Point(p1.x + u * xDelta, p1.y + u * yDelta);\r\n\t}\r\n\treturn Point.distance(closestPoint, new Point(p3.x, p3.y));\r\n}\r\n\r\n\/**\r\nTest out the function\r\n*\/\r\n\r\nvar dotA:Sprite = dot(100, 100);\r\nvar dotB:Sprite = dot(200, 200);\r\nvar dotC:Sprite = dot(150, 100, 0x0000FF);\r\nvar txt:TextField = TextField(dotC.addChild(new TextField()));\r\nwith(txt) x = 5, y = 5, autoSize = &quot;left&quot;, selectable = false, mouseEnabled = false;\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\tgraphics.clear();\r\n\tgraphics.lineStyle(0,0x000000);\r\n\tgraphics.moveTo(dotA.x, dotA.y);\r\n    graphics.lineTo(dotB.x, dotB.y);\r\n\ttxt.text = pointToLineDistance(dotA, dotB, dotC).toFixed(2);\r\n}\r\n\r\n\/\/ draggable dot\r\nfunction dot(xp:Number, yp:Number, col:uint = 0xFF0000, rad:Number=4):Sprite {\r\n    var s:Sprite = Sprite(addChild(new Sprite));\r\n    s.x = xp;\r\n    s.y = yp;\r\n    with(s.graphics) beginFill(col), drawCircle(0,0,rad);\r\n    s.buttonMode = true;\r\n    s.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);\r\n    return s;\r\n}\r\nfunction onDrag(evt:MouseEvent):void { evt.currentTarget.startDrag() }\r\nstage.addEventListener(MouseEvent.MOUSE_UP, onUp);\r\nfunction onUp(evt:MouseEvent):void{stopDrag() }\r\n<\/pre>\n<p>Last night I was working late and found myself in need of a way to calculate the distance between a point and a line. After a quick google search I found myself once again on <a href=\"http:\/\/local.wasp.uwa.edu.au\/~pbourke\/geometry\/pointline\/\" target=\"blank\">Paul Bourke&#8217;s extremely useful website<\/a>. I was in a rush, so I just quickly ported <a href=\"http:\/\/local.wasp.uwa.edu.au\/~pbourke\/geometry\/pointline\/DistancePoint.java\" target=\"blank\">the java code (by Pieter Iserbyt)<\/a> and wrote this test snippet to make sure it works.<\/p>\n<p>This code could be optimized a bit, but it works nicely. <\/p>\n<p>I needed this for a commercial project, but as I was porting the code I thought of an interesting way to use this code to draw a gradient &#8211; may write and post that tomorrow.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/** Original function by Pieter Iserbyt: http:\/\/local.wasp.uwa.edu.au\/~pbourke\/geometry\/pointline\/DistancePoint.java from Paul Bourke&#039;s website: http:\/\/local.wasp.uwa.edu.au\/~pbourke\/geometry\/pointline\/ *\/ function pointToLineDistance(p1:Sprite, p2:Sprite, p3:Sprite):Number { var xDelta:Number = p2.x &#8211; p1.x; var yDelta:Number = p2.y &#8211; p1.y; if ((xDelta == 0) &amp;&amp; (yDelta == 0)) { \/\/ p1 and p2 cannot be the same point p2.x += 1; p2.y += 1; xDelta [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114,121],"tags":[3,14,4],"class_list":["post-305","post","type-post","status-publish","format-standard","hentry","category-graphics-algorithms","category-math","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/305","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=305"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/305\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}