title: Fastest way to return all triangles from array of points
body: <p>I have a number of points in array <code>arr()</code>. What is the fastest and most efficient way to return an array <code>combi()</code> of all possible triangles that can be drawn from those points, in VBA?</p>
  
  <p>I have put together a simple script, and it works a charm for a small number of points. But it slows down exponentially as I add more points. Below is a simplified version of what I have.</p>
  
  <p>Please note: you will see in my code that I am doing some basic checks to ensure all vertices in the triangle are different. I am also splitting the points in each and ordering them by 'size'. The reason I am doing that is that I will need to remove duplicate triangles. Of course, if your proposed implementation finds the triangles without the duplicates then that would be optimal</p>
  
  <p><strong>My code:</strong></p>
  
  <pre><code>Sub find_triangles()
  
      Dim arr(6) As Variant
      Dim combi() As Variant
  
  
      arr(0) = 1
      arr(1) = 2
      arr(2) = 3
      arr(3) = 4
      arr(4) = 5
      arr(5) = 6
      'arr(x)= x ... etc.
  
      ReDim combi(0)
  
      For i = 1 To UBound(arr)
         For j = 1 To UBound(arr)
            For k = 1 To UBound(arr)
               If Not i = j And Not j = k And Not i = k Then
  
                  m = Array(i, j, k)
  
                  ReDim temp(2)
  
                  temp(0) = Application.Small(m, 1)
                  temp(1) = Application.Small(m, 2)
                  temp(2) = Application.Small(m, 3)
  
                  combi(UBound(combi)) = temp(0) &amp; "-" &amp; temp(1) &amp; "-" &amp; temp(2)
                  ReDim Preserve combi(UBound(combi) + 1)
  
               End If
            Next
         Next
      Next
  End Sub
  </code></pre>