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)`
-
Is this a homework problem? :Dcrayzeewulf– crayzeewulf2013年03月07日 22:08:59 +00:00Commented Mar 7, 2013 at 22:08
-
you give us no information about what is wrong...Stephane Rolland– Stephane Rolland2013年03月07日 22:09:35 +00:00Commented Mar 7, 2013 at 22:09
-
1The answer that my program outputs is wrong. It will not give you the correct POI. I suspect something wrong with the calculations.Balkarn Gill– Balkarn Gill2013年03月07日 22:10:54 +00:00Commented Mar 7, 2013 at 22:10
-
2I input y=2x+3 and y=-0.5x+7 REAL POI= (1.6, 6.2) MY PROGRAM= (-1.60,-0.20)Balkarn Gill– Balkarn Gill2013年03月07日 22:13:16 +00:00Commented 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.crayzeewulf– crayzeewulf2013年03月07日 22:20:28 +00:00Commented Mar 7, 2013 at 22:20
3 Answers 3
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)
5 Comments
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]
3 Comments
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.