<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: polyline code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 03:25:49 GMT</pubDate>
    <description>DZone Snippets: polyline code</description>
    <item>
      <title>Drawing a single Polyline using SVG</title>
      <link>http://snippets.dzone.com/posts/show/5270</link>
      <description>This code is very similar to &lt;a href="http://snippets.dzone.com/posts/show/5269"&gt;Draw a series of lines using SVG&lt;/a&gt; [dzone.com] but now we can draw a series of points at the same time the mouse button is pressed and is moving. Here's an &lt;a href="http://twitxr.com/image/18451/"&gt;example of the SVG squiggle output&lt;/a&gt; [twitxr.com].&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;svg	xmlns="http://www.w3.org/2000/svg" width="100%"&lt;br /&gt;		xmlns:xlink="http://www.w3.org/1999/xlink" &gt;&lt;br /&gt;  &lt;g id="sketch" class="sketch"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;rect x="0"&lt;br /&gt;y="0" width="100%" height="100%" fill="yellow" /&gt;&lt;br /&gt;  &lt;/g&gt;&lt;br /&gt;&lt;br /&gt;  &lt;script&gt;&lt;br /&gt;  &lt;![CDATA[&lt;br /&gt;  var	xmlns="http://www.w3.org/2000/svg"&lt;br /&gt;  Root=document.documentElement&lt;br /&gt;  on_start(Root)&lt;br /&gt;&lt;br /&gt;  function mouseUp(evt) {&lt;br /&gt;	  on_pen_up(Root);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function on_start(R){&lt;br /&gt;	  var Attr={&lt;br /&gt;	    "onmousemove":"mouseMove",&lt;br /&gt;      "onmousedown":"add_line(evt)",	&lt;br /&gt;      "onmouseup":"mouseUp"&lt;br /&gt;	  }&lt;br /&gt;	  assignAttr(R,Attr)&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function on_pen_down(R){&lt;br /&gt;	  var Attr={&lt;br /&gt;	    "onmousemove":"addPoint(evt)",&lt;br /&gt;	    "onmouseup":"on_pen_up(Root)"&lt;br /&gt;	  }&lt;br /&gt;	  assignAttr(R,Attr)&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function on_pen_up(R){&lt;br /&gt;	  var Attr={&lt;br /&gt;	    "onmousemove":"null"&lt;br /&gt;	  }&lt;br /&gt;	  assignAttr(R,Attr)&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function add_line(evt){&lt;br /&gt;	  //if (evt.target.nodeName!="rect") return&lt;br /&gt;	  var C=document.createElementNS(xmlns,"polyline") &lt;br /&gt;	&lt;br /&gt;	  var Attr={&lt;br /&gt;		  "x":30,&lt;br /&gt;		  "y":180,&lt;br /&gt;		  "fill":'none',&lt;br /&gt;		  "stroke": '#44a',&lt;br /&gt;		  "id":'pl1',&lt;br /&gt;		  "stroke-width":2&lt;br /&gt;	  }&lt;br /&gt;    &lt;br /&gt;	  assignAttr(C,Attr)&lt;br /&gt;	  Root.appendChild(C)&lt;br /&gt;    &lt;br /&gt;	  on_pen_down(Root)&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function addPoint (evt) {&lt;br /&gt;    element = document.getElementById('pl1') 	&lt;br /&gt;    var point = element.ownerDocument.documentElement.createSVGPoint(); &lt;br /&gt;    point.x = evt.clientX; &lt;br /&gt;    point.y = evt.clientY;&lt;br /&gt;    &lt;br /&gt;    element.points.appendItem(point);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function assignAttr(O,A){&lt;br /&gt;	  for (i in A) O.setAttributeNS(null,i, A[i])&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  ]]&gt;&lt;br /&gt;  &lt;/script&gt;&lt;br /&gt;&lt;br /&gt;  &lt;text font-size="12pt" x="50" y="20" id="t1"&gt;Click something to move it&lt;/text&gt;&lt;br /&gt;  &lt;text font-size="12pt" x="80" y="40" id="t2"&gt;Click nothing to add something&lt;/text&gt;&lt;br /&gt;&lt;/svg&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;*update 7:44pm*&lt;br /&gt;By simply making the id unique using a global variable count it was possible to draw &lt;a href="http://www.twitxr.com/image/18474/"&gt;multiple polylines&lt;/a&gt; [twitxr.com].</description>
      <pubDate>Sat, 22 Mar 2008 19:15:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5270</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Draw a series of lines using SVG</title>
      <link>http://snippets.dzone.com/posts/show/5269</link>
      <description>This code draws a line every time the user presses the mouse button.  Here is an &lt;a href="http://twitxr.com/image/18406/"&gt;example of the SVG output&lt;/a&gt; [twitxr.com].&lt;br /&gt;&lt;br /&gt;Tested in Firefox 3 beta 4.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;br /&gt;&lt;svg xmlns="http://www.w3.org/2000/svg" version="1.1"&lt;br /&gt; onclick="addPoint(evt, document.getElementById('pl1'));"&gt; &lt;br /&gt;  &lt;title&gt;click to add point to polyline&lt;/title&gt;&lt;br /&gt;  &lt;script type="text/ecmascript"&gt;&lt;br /&gt;    &lt;![CDATA[ &lt;br /&gt;      function addPoint (evt, element) {&lt;br /&gt;        var point = element.ownerDocument.documentElement.createSVGPoint(); &lt;br /&gt;        point.x = evt.clientX; &lt;br /&gt;        point.y = evt.clientY; &lt;br /&gt;        element.points.appendItem(point);&lt;br /&gt;      }&lt;br /&gt;    ]]&gt;&lt;br /&gt;  &lt;/script&gt; &lt;br /&gt;  &lt;rect x="0" y="0" width="100%" height="100%" fill-opacity="0" /&gt;&lt;br /&gt;  &lt;polyline id="pl1" fill="none" stroke="green" stroke-width="2" points="30,180 50,150" /&gt;&lt;br /&gt;&lt;/svg&gt; &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 22 Mar 2008 16:50:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5269</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Closest Polyline Point //Javascript Function</title>
      <link>http://snippets.dzone.com/posts/show/670</link>
      <description>&lt;a href="http://jsfromhell.com/math/closest-polyline-point"&gt;&lt;br /&gt;Given a dot and a set of lines, it returns the nearest dot over one of the given lines.&lt;br /&gt;&lt;br /&gt;[UPDATED CODE AND HELP CAN BE FOUND HERE]&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/math/closest-polyline-point [v1.0]&lt;br /&gt;&lt;br /&gt;closestPolyLinePoint = function( px, py, x0, y0, x1, y1, etc, etc, etc ){&lt;br /&gt;	function dotLineLength( x, y, x0, y0, x1, y1, o ){&lt;br /&gt;		function lineLength( x, y, x0, y0 ){&lt;br /&gt;			return Math.sqrt( ( x -= x0 ) * x + ( y -= y0 ) * y );&lt;br /&gt;		}&lt;br /&gt;		if( o &amp;&amp; !( o = function( x, y, x0, y0, x1, y1 ){&lt;br /&gt;			if( !( x1 - x0 ) ) return { x: x0, y: y };&lt;br /&gt;			else if( !( y1 - y0 ) ) return { x: x, y: y0 };&lt;br /&gt;			var left, tg = -1 / ( ( y1 - y0 ) / ( x1 - x0 ) );&lt;br /&gt;			return { x: left = ( x1 * ( x * tg - y + y0 ) + x0 * ( x * - tg + y - y1 ) ) / ( tg * ( x1 - x0 ) + y0 - y1 ), y: tg * left - tg * x + y };&lt;br /&gt;		}( x, y, x0, y0, x1, y1 ), o.x &gt;= Math.min( x0, x1 ) &amp;&amp; o.x &lt;= Math.max( x0, x1 ) &amp;&amp; o.y &gt;= Math.min( y0, y1 ) &amp;&amp; o.y &lt;= Math.max( y0, y1 ) ) ){&lt;br /&gt;			var l1 = lineLength( x, y, x0, y0 ), l2 = lineLength( x, y, x1, y1 );&lt;br /&gt;			return l1 &gt; l2 ? l2 : l1;&lt;br /&gt;		}&lt;br /&gt;		else {&lt;br /&gt;			var a = y0 - y1, b = x1 - x0, c = x0 * y1 - y0 * x1;&lt;br /&gt;			return Math.abs( a * x + b * y + c ) / Math.sqrt( a * a + b * b );&lt;br /&gt;		}&lt;br /&gt;	};&lt;br /&gt;	for( var args = [].slice.call( arguments, 0 ), lines = []; args.length &gt; 4; lines[lines.length] = { y1: args.pop(), x1: args.pop(), y0: args.pop(), x0: args.pop() } );&lt;br /&gt;	if( !lines.length )&lt;br /&gt;		return { x: px, y: py };&lt;br /&gt;	var l, i, o = lines[0], lower = { i: 0, l: dotLineLength( px, py, o.x0, o.y0, o.x1, o.y1 ) };&lt;br /&gt;	for( i in lines )&lt;br /&gt;		if( lower.l &gt; ( l = dotLineLength( px, py, ( o = lines[i] ).x0, o.y0, o.x1, o.y1 ) ) )&lt;br /&gt;			lower = { i: i, l: l };&lt;br /&gt;	py = py &lt; Math.min( ( o = lines[lower.i] ).y0, o.y1 ) ? Math.min( o.y0, o.y1 ) : py &gt; Math.max( o.y0, o.y1 ) ? Math.max( o.y0, o.y1 ) : py;&lt;br /&gt;	px = px &lt; Math.min( o.x0, o.x1 ) ? Math.min( o.x0, o.x1 ) : px &gt; Math.max( o.x0, o.x1 ) ? Math.max( o.x0, o.x1 ) : px;&lt;br /&gt;	return Math.abs( o.x0 - o.x1 ) &lt; Math.abs( o.y0 - o.y1 ) ? { x: ( py * ( o.x0 - o.x1 ) - o.x0 * o.y1 + o.y0 * o.x1 ) / ( o.y0 - o.y1 ), y: py }&lt;br /&gt;	: { x: px, y: ( px * ( o.y0 - o.y1 ) - o.y0 * o.x1 + o.x0 * o.y1 ) / ( o.x0 - o.x1 ) };&lt;br /&gt;};&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Sep 2005 04:40:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/670</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
  </channel>
</rss>
