title: Intersection of two lines. Which of the following equations is the most resource-efficient?
body: <p>In my program, I need to find the y value of the intersection of two lines.</p>
  
  <p>First line is denoted by: y=a1*x+b1
  Second line is denoted by y=a2*x+b2</p>
  
  <p>Solving for x in each, setting the equations equal to each other, solving for y and rearranging, I came up with four different but equivalent (that is yet to be determined, actually) equations:</p>
  
  <ol>
  <li><p>y=(b1/a1-b2/a2)/(1/a1-1/a2)</p></li>
  <li><p>y=(b2+(a2/a1)*b1)/(1-a2/a1)</p></li>
  <li><p>y=(a1*b2-b1*a2)/(a1-a2)</p></li>
  <li><p>y=a1*((b2-b1)/(a1-a2))+b1</p></li>
  </ol>
  
  <p>Now, I ran the following Python code to check if all equations give equal answers:</p>
  
  <pre><code>import random
  
  def linearfunction(coords1,coords2):
      x1=coords1[0]
      x2=coords2[0]
      y1=coords1[1]
      y2=coords2[1]
      a=(y2-y1)/(x2-x1)
      b=y2-a*x2
      return [a,b]
  
  def doesitequal():
      first=[random.randint(0,100),random.randint(0,100)]
      second=[random.randint(0,100),random.randint(0,100)]
      third=[random.randint(0,100),random.randint(0,100)]
      fourth=[random.randint(0,100),random.randint(0,100)]
  
      a1=linearfunction(first,second)[0]
      b1=a1=linearfunction(first,second)[1]
      a2=linearfunction(third,fourth)[0]
      b2=linearfunction(third,fourth)[1]
  
      firstversion=(b1/a1-b2/a2)/(1/a1-1/a2)
      secondversion=(b2+(a2/a1)*b1)/(1-a2/a1)
      thirdversion=(a1*b2-b1*a2)/(a1-a2)
      fourthversion=a1*((b2-b1)/(a1-a2))+b1
  
      print('First equation: ', firstversion)
      print('Second equation:', secondversion)
      print('Third equation:', thirdversion)
      print('Fourth equation', fourthversion)
  
  for i in range(100):
      doesitequal()
  </code></pre>
  
  <p>Surprisingly (to me), the second equation always returns a value that is slightly off the other ones, which return values that are exactly equal to each other. Please try it for yourselves to see what I mean.</p>
  
  <p>Example output:</p>
  
  <pre><code>First equation:  -71.13238078843175
  Second equation: -67.71148218281976
  Third equation: -71.13238078843175
  Fourth equation -71.1323807884317
  First equation:  190.62597809076678
  Second equation: 186.9640062597809
  Third equation: 190.6259780907668
  Fourth equation 190.62597809076686
  First equation:  81.93742601048538
  Second equation: 80.48246237104686
  Third equation: 81.93742601048537
  Fourth equation 81.93742601048537
  </code></pre>
  
  <p>So my first question is:</p>
  
  <p>Why dos the second equation return slightly different values than what it should? Did I mess up my arithmetics? Or is it due to the way Python evaluates given math operations?</p>
  
  <p>My second question is:</p>
  
  <p>Out of these four formulas, which is the most memory-efficient one? That is, which one is the least likely to cause lags in calculations if the line intersection is to be calculated a huge amount of times in a short time period?</p>
  
  <p>A wild guess would be that the efficiency is inversely proportional to the number of operations in a given equation. For instance, first equation involves 7 math operations, whereas last one involves 5, meaning the last one is more efficient than the first one. However, different math operations are likely not to be equivalent to each other in terms of complexity and, hence, efficiency. That is why I am unsure about the answer to this question.</p>
  
  <p>Any help is greatly appreciated!</p>