<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: intersection code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 03:27:15 GMT</pubDate>
    <description>DZone Snippets: intersection code</description>
    <item>
      <title>Full intersection between 2 arrays</title>
      <link>http://snippets.dzone.com/posts/show/2134</link>
      <description>// This snippet adds a method to Array to perform a full intersection between 2 arrays. The &amp; method eliminates duplicate objects, this doesn't!&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;  def real_intersection(arr2)&lt;br /&gt;    self_sorted = self.sort&lt;br /&gt;    target_sorted = arr2.sort&lt;br /&gt;    intersection= []&lt;br /&gt;    jstart=0&lt;br /&gt;    for i in (0..self_sorted.length-1)&lt;br /&gt;      for j in (jstart..target_sorted.length-1)&lt;br /&gt;        if self_sorted[i] == target_sorted[j]&lt;br /&gt;          jstart = j+1&lt;br /&gt;          intersection[intersection.length] = self_sorted[i]&lt;br /&gt;          break&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    return intersection&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// tests&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'test/unit'&lt;br /&gt;&lt;br /&gt;class ArrayIntersectionTests &lt; Test::Unit::TestCase    &lt;br /&gt;  def test_real_array_intersection&lt;br /&gt;    assert_equal [2], [2, 2, 2, 3, 7, 13, 49] &amp; [2, 2, 2, 5, 11, 107]&lt;br /&gt;    assert_equal [2, 2, 2], [2, 2, 2, 3, 7, 13, 49].real_intersection([2, 2, 2, 5, 11, 107])&lt;br /&gt;    assert_equal ['a', 'c'], ['a', 'b', 'a', 'c'] &amp; ['a', 'c', 'a', 'd']&lt;br /&gt;    assert_equal ['a', 'a', 'c'], ['a', 'b', 'a', 'c'].real_intersection(['a', 'c', 'a', 'd'])&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 Jun 2006 01:32:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2134</guid>
      <author>gaspode ()</author>
    </item>
    <item>
      <title>Line intersection</title>
      <link>http://snippets.dzone.com/posts/show/2087</link>
      <description>the function gives the point where 2 lines intersect&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;float[] checkIntersection(float lineAx1,float lineAy1,float lineAx2,float lineAy2,float lineBx1,float lineBy1,float lineBx2,float lineBy2){&lt;br /&gt;  float aM, bM, aB, bB, isX=0, isY=0;&lt;br /&gt;  if((lineAx2-lineAx1)==0){&lt;br /&gt;    isX=lineAx1;&lt;br /&gt;    bM=(lineBy2-lineBy1)/(lineBx2-lineBx1);&lt;br /&gt;    bB=lineBy2-bM*lineBx2;&lt;br /&gt;    isY=bM*isX+bB;&lt;br /&gt;  }&lt;br /&gt;  else if((lineBx2-lineBx1)==0){&lt;br /&gt;    isX=lineBx1;&lt;br /&gt;    aM=(lineAy2-lineAy1)/(lineAx2-lineAx1);&lt;br /&gt;    aB=lineAy2-aM*lineAx2;&lt;br /&gt;    isY=aM*isX+aB;&lt;br /&gt;  }&lt;br /&gt;  else{&lt;br /&gt;    aM=(lineAy2-lineAy1)/(lineAx2-lineAx1);&lt;br /&gt;    bM=(lineBy2-lineBy1)/(lineBx2-lineBx1);&lt;br /&gt;    aB=lineAy2-aM*lineAx2;&lt;br /&gt;    bB=lineBy2-bM*lineBx2;&lt;br /&gt;    isX=max(((bB-aB)/(aM-bM)),0);&lt;br /&gt;    isY=aM*isX+aB;&lt;br /&gt;  }&lt;br /&gt;  float[] r={&lt;br /&gt;    isX,isY  };&lt;br /&gt;  return r; &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 23 May 2006 11:19:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2087</guid>
      <author>eskimoblood ()</author>
    </item>
    <item>
      <title>Lenght between a dot and a line //JavaScript Function</title>
      <link>http://snippets.dzone.com/posts/show/674</link>
      <description>&lt;a href="http://jsfromhell.com/math/dot-line-length"&gt;&lt;br /&gt;Given a dot and a line, it returns the distance between them, the last parameter tells if the line should be considered infinite or if the function should respect its limits.&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;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/math/dot-line-length [v1.0]&lt;br /&gt;&lt;br /&gt;dotLineLength = function( 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;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Sep 2005 05:01:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/674</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
    <item>
      <title>"Intersection dot" between a dot and a line //JavaScript Function</title>
      <link>http://snippets.dzone.com/posts/show/673</link>
      <description>&lt;a href="http://jsfromhell.com/math/dot-line-intersection"&gt;&lt;br /&gt;Given a line and a dot, it return the coordinates of their intersection.&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;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/math/dot-line-intersection [v1.0]&lt;br /&gt;&lt;br /&gt;dotLineIntersection = function( x, y, x0, y0, x1, y1 ){&lt;br /&gt;	if( !( x1 - x0 ) )&lt;br /&gt;		return { x: x0, y: y };&lt;br /&gt;	else if( !( y1 - y0 ) )&lt;br /&gt;		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;};&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Sep 2005 04:58:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/673</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
  </channel>
</rss>
