1

I've been trying to code a Point of Intersection calculator for myself. For some reason my calculator has either been coded completely wrong or the intended order of operations are not working. Allow me to elaborate.

def point_of_intersection (m1, b1, m2, b2)

    if m1 == m2 || m1 - m2 == 0
        puts "No Solution"
    elsif m1 == m2 || b1 == b2
        puts "Infinite Solutions"
    end

    x = -1 * (b1 - b2) / (m1 - m2)
    y = (m1 * x) + b1

    puts "The Point Of Intersection is: (#{x}, #{y})"

end

As you can see, the method point_of_intersection takes four parameters: The slope of the first linear equation (m1) and its y-intercept (b1) and the slope of the second linear equation (m2) and its y-intercept (b2)

This calculator has worked correctly for me for some cases but for some others it seems to output the wrong numbers. Here are the results of some of my tests

point_of_intersection(1, 4, 2, 6) #Gives (-2,2) which is correct
point_of_intersection(0,5,2,0) #Gives (2,5) when it should be (2.5, 5)
point_of_intersection(1,2,5,3) #Gives (-1, 1) when it should be (-0.25, 1.75)

I know some people may be quick to question whether or not the formula that -1 times the difference in y-intercept divided by the difference in the value of the slopes is right or not. I can guarantee that for every test I performed on the text editor, I also used the exact same formula on paper and got different results.

My best guess is that my computer is somehow performing the order of operations for

x = -1 * (b1 - b2) / (m1 - m2)

however I am not experienced enough to identify how the computer could screw up the operation.

I would appreciate all of your help thank you.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    I think there's something wrong with your conditionals, `||` means 'or' not 'and'. If `m1 == m2` is false then `m1 - m2 == 0` will also be false, whereas if `m1 == m2` is true then `m1 - m2 == 0` will be irrelevant because the first test already passed. OTOH, the first term in the elsif will never occur, because if `m1 == m2` were true, the first branch would have been taken, so only the `b1 == b2` matters on that line – philomory Oct 02 '16 at 01:17

2 Answers2

0

Convert your arguments to floats:

def point_of_intersection (m1, b1, m2, b2)
    m1 = m1.to_f
    b1 = b1.to_f
    m2 = m2.to_f
    b2 = b2.to_f

    if m1 == m2 || m1 - m2 == 0
        puts "No Solution"
    elsif m1 == m2 || b1 == b2
        puts "Infinite Solutions"
    end

    x = -1 * (b1 - b2) / (m1 - m2)
    y = (m1 * x) + b1

    puts "The Point Of Intersection is: (#{x}, #{y})"

end

Examples:

point_of_intersection(1, 4, 2, 6)
#The Point Of Intersection is: (-2.0, 2.0)

point_of_intersection(0, 5, 2, 0)
#The Point Of Intersection is: (2.5, 5.0)

point_of_intersection(1, 2, 5, 3)
#The Point Of Intersection is: (-0.25, 1.75)
Sagar Pandya
  • 9,323
  • 2
  • 24
  • 35
0

You need to work with floats:

x = (-1 * (b1 - b2) / (m1 - m2)).to_f
y = ((m1 * x) + b1).to_f
Tiago Lopo
  • 7,619
  • 1
  • 30
  • 51