0

I tried to complete the program but the answer is wrong, and I can't pinpoint what exactly.

QUESTION: Given the equations of two lines (y=mx+b), determine if the two lines are parallel, the same or intersect. Calculate and output the point of intersection.

My code:

equation_1 =raw_input("Please enter the equation of your 1st line(y=mx+b): ")
equation_2 =raw_input("Please enter the equation of your 2nd line(y=mx+b): ")
plus_1 = equation_1.find('+')
plus_2 = equation_2.find('+')
x_1 = equation_1.find('x')
x_2 = equation_2.find('x')
equalsign_1 = equation_1.find('=')
equalsign_2 = equation_2.find('=')
b1 = equation_1[x_1+1:]
b2 = equation_2[x_2+1:]
m1 = equation_1[equalsign_1+1:x_1]
m2 = equation_2[equalsign_2+1:x_2]
if m1==m2 and b1!=b2:
 print "Your equations are parallel. "
elif m1==m2 and b1==b2:
 print "Your equations are the same. "
else: 
 equation_intersect_y = float(b2)-float(b1)
 equation_intersect_x = float(m2)-float(m1) # equation_intersect_x = float(m1)-float(m2)
 poi_x = float(equation_intersect_y)/float(equation_intersect_x)
 poi_y = float(b1)*float(poi_x)+float(m1)`
m4573r
9907 silver badges17 bronze badges
asked Mar 7, 2013 at 22:06
10
  • Is this a homework problem? :D Commented Mar 7, 2013 at 22:08
  • you give us no information about what is wrong... Commented Mar 7, 2013 at 22:09
  • 1
    The answer that my program outputs is wrong. It will not give you the correct POI. I suspect something wrong with the calculations. Commented Mar 7, 2013 at 22:10
  • 2
    I input y=2x+3 and y=-0.5x+7 REAL POI= (1.6, 6.2) MY PROGRAM= (-1.60,-0.20) Commented Mar 7, 2013 at 22:13
  • 1
    @BalkarnGill Glad to hear that you are taking the initiative to learn. I suggest that you create a function that takes m1, b1, m2, b2 as parameters and test it out with several values of these parameters (without having to input them from a prompt). Post the test cases and results here. If this function works then the problem is probably in the way you are parsing the user input. Commented Mar 7, 2013 at 22:20

3 Answers 3

1

The equation you used for computing poi_x is wrong. Also, the formula your code used for calculating poi_y has m and b interchanged. Here is a slightly modified code that ought to help:

#! /usr/bin/env python
equation_1 ="y=2x+3"
equation_2 ="y=-0.5x+7"
plus_1 = equation_1.find('+')
plus_2 = equation_2.find('+')
x_1 = equation_1.find('x')
x_2 = equation_2.find('x')
equalsign_1 = equation_1.find('=')
equalsign_2 = equation_2.find('=')
b1 = float(equation_1[x_1+1:])
b2 = float(equation_2[x_2+1:])
m1 = float(equation_1[equalsign_1+1:x_1])
m2 = float(equation_2[equalsign_2+1:x_2])
print m1,b1,m2,b2
if m1==m2 and b1!=b2:
 print "Your equations are parallel. "
elif m1==m2 and b1==b2:
 print "Your equations are the same. "
else:
 equation_intersect_y = b2 - b1
 equation_intersect_x = m1 - m2
 poi_x = equation_intersect_y/equation_intersect_x
 poi_y = m1*poi_x+b1
 print poi_x, poi_y

The output is:

2.0 3.0 -0.5 7.0
1.6 6.2

And here is a slightly better code that reduces repetition:

#! /usr/bin/env python
def parse_equation_string(eq_string):
 x_pos = eq_string.find('x')
 equal_pos = eq_string.find('=')
 b = float(eq_string[x_pos+1:])
 m = float(eq_string[equal_pos+1:x_pos])
 return m, b
def get_point_of_intersection(line1, line2):
 m1, b1 = line1
 m2, b2 = line2
 if m1==m2 and b1!=b2:
 return "The lines are parallel. "
 elif m1==m2 and b1==b2:
 return "The lines are the same. "
 else:
 equation_intersect_y = b2 - b1
 equation_intersect_x = m1 - m2
 poi_x = equation_intersect_y/equation_intersect_x
 poi_y = m1*poi_x+b1
 return poi_x, poi_y
equation_1 = "y=2x+3"
equation_2 = "y=-0.5x+7"
line_1 = parse_equation_string(equation_1)
line_2 = parse_equation_string(equation_2)
print line_1, line_2
print get_point_of_intersection(line_1, line_2)

The output is:

(2.0, 3.0) (-0.5, 7.0)
(1.6, 6.2)
answered Mar 7, 2013 at 22:31
Sign up to request clarification or add additional context in comments.

5 Comments

Wow thank you! But why is it outputting "2.0 3.0 -0.5 7.0" What if I just want the POI? How would I do that? Thanks again :)
Also what if the input is y=x and y=-x? How would I make that work?
I think I will let you figure both of those out on your own. Try stuff first :D
Okay i took out the extra print command, which fixed my first question. :D Sorry it was a dumb question but im just getting started with programming.
But I cant seem to get the 2nd question down. What if the input is y=x and y=-x? I think I would use another if statement but how and where?
0

Shouldn't

b1 = equation_1[x_1+1:]
b2 = equation_2[x_2+1:]

Be

b1 = equation_1[plus_1+1:]
b2 = equation_2[plus_2+1:]

Or

b1 = equation_1[x_1+2:]
b2 = equation_2[x_2+2:]

Also I think

m1 = equation_1[equalsign_1+1:x_1]
m2 = equation_2[equalsign_2+1:x_2]

Should be

m1 = equation_1[equalsign_1+1:x_1-1]
m2 = equation_2[equalsign_2+1:x_2-1]
answered Mar 7, 2013 at 22:12

3 Comments

It still outputs the wrong answer. y=2x+3 and y=-0.5x+7 POI supposed to be (1.6, 6.2) My program: Your equations intersect in Quadrant 3(-1.60,-0.20)
To fix the x-intersect you need a check that says if (equation_intersect_x < 0) equation_intersect_x = equation_intersect_x*-1. Still looking at the y-intersect issue. Edit: Actually that fixes the y-intersect as well. That's your issue.
Yeah, Crayzeewulf's is a cleaner solution. Flip the deltas. The rest will work itself out.
0

First couple of suggestions:

Print back the equation to the user before performing operations (before the first "if" statement):

print "Equation 1 y={}x+{}".format(m1, b1)
print "Equation 2 y={}x+{}".format(m2, b2)

re or the string functions split and strip may be easier than string indexing:

m1 = equation_1.split('=')[1].split('x')[0]
b1 = equation_1.split('=')[1].split('+')[1]

Give the program some simple test cases before harder ones: 1: y=0x+-3 2: y=1x+0 intersects at X = 3

1: y=-1x+0
2: y=0x+3
intersects at X = -3
1: y=2x+2
2: y=-2x+0
intersects at X = -0.5

Now all that's left is the algebra:

Do a hard case by hand first:

Assuming they are not parallel or the same: find the point where x1=x2 and y1=y2 Find X where both Ys are equal: Therefore:
(m1)*x1 + b1 = y1 = y2 = (m2)*x2 + b2 rewriting to find X: (m1)*x1 + b1 = (m2)*x2 + b2 But at the point of interest (X) x1 = x2 rewriting: (m1 + m2)*X = b2 -b1 rewriting: X = (b2 - b1) / (m1 + m2)

Now we can see that this doesn't match your equation_intersect x formula.

answered Mar 7, 2013 at 22:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.