Ok, so I just started using Python last week and I have written 4 VERY simple, non-GUI programs. This is for my intro to game physics class and I happen to be stuck on the last project until my next class. What this project is SUPPOSED to do is ask for two linear equations and determine if the intersect. I have it set to respond if the lines are parallel or the same, but I'm having an issue figuring out the code needed to determine an intersection point. Again, I just started using Python and don't have much experience with any language, so please excuse me if my coding is sloppy.

Here is what I have. Sorry for the wall of text...

#linear intersection
#asks for the equation to two lines and determines if they intersect, if not returns an error.
import math

x1 = "x+"
y1 = "y="
x = "x"
y = "y"


print "Please enter the slope for line 1."
m1 = raw_input("")

print "Please enter the y-intercept for line 1."
yi = raw_input("")

print "Please enter the slope for line 2."
m2 = raw_input("")

print "Please enter the y-intercept for line 2."
yi2 = raw_input("")

#do the lines intersect?
if m1 == m2 and yi == yi2:
	print "Error!!! Infinite solutions, lines are the same."
elif m1 == m2 and yi != yi2:
	print "Error!!! Zero solutions, lines are parallel."
else:
    m1 != m2
#calculate if the lines intersect


#attempt to make things easier
y2 = "=" + yi
y3 = "=" + yi2
#lines in standard form
line1 = m1 + x + "-" + y + y2
line2 = m2 + x + "-" + y + y3




print line1
print line2
#print line3
print "One point of intersection at ."
raw_input("Press enter to exit.")

Recommended Answers

All 9 Replies

This looks like more of a math problem than a programming problem. Use b for your y-intercept. Anyone that has opened an Algebra book will be familiar with it.

y1 = m1x + b1
y2 = m2x + b2

Solve for x and you have an equation you can plug into your code.

Also, if all you need to know if they intersect (but don't care about the point), you've already figured out if they are the same line or if they are parallel. If it's not one of those two, what can you say about the two lines?

This looks like more of a math problem than a programming problem. Use b for your y-intercept. Anyone that has opened an Algebra book will be familiar with it.

y1 = m1x + b1
y2 = m2x + b2

Solve for x and you have an equation you can plug into your code.

Also, if all you need to know if they intersect (but don't care about the point), you've already figured out if they are the same line or if they are parallel. If it's not one of those two, what can you say about the two lines?

The problem I'm having is figuring out how to have the program recognize the lines themselves and solve for the variables.

Solve for x on paper and you have your equation. Just use the variable names you have in your program. The program doesn't have to do anything special. That's why I said it's a math problem.

Here's a start: m1x + b1 = m2x + b2 Solve for x.

So I must be really slow or something. I still can't figure this out and I'm getting a headache...I've changed the entirety, which wasn't much, of the else: portion of code and now have.
else:

m1 != m2
#calculate if the lines intersect

#m1x + b=m2x + b2
y1 = m1*x + b == m2x + b1
print y1

I just can't seem to wrap my brain around this. I also added the import cmath line at the top of the program. Don't really want you to just give me the answer, but if you have an example that would be great too.

You need to solve this equation: m1x + b1 = m2x + b2 Forget python, forget you are programming. You need solve for x so that you have: x = ... It's math that you have to solve on a piece of paper.

You can then just plug that into your program to get the x value at which these lines intersect.

But, like I said, if all you are trying to determine is if the lines intersect, this is all unnecessary.

Finally got it. x= m2/m1 is what i needed, then just set y= m2*x + b2 and I have my point.

No, here's the work to solve m1x + b1 = m2x + b2 for x:

m1x + b1 = m2x + b2 #initial equation
(m1x - m2x) + b1 = b2 # subtract m2x from both sides
m1x - m2x = b2 - b1 # subtract b1 from both sides
x(m1 - m2) = (b2 - b1) # take out the common term, x, on the left side
x = (b2 - b1)/(m1 - m2) # divide both sides by (m1-m2)

Then you can plug x back into either equation, as you said, and have your point (x,y).

I think that you overlooked what adam was saying in his first post. If two straight lines are not parallel and are not the same line, then they must intersect as some point.

Unless you need to figure out the exact point of intersection, you can simply use if-else logic here.

I think that you overlooked what adam was saying in his first post. If two straight lines are not parallel and are not the same line, then they must intersect as some point.

Unless you need to figure out the exact point of intersection, you can simply use if-else logic here.

I needed the point, but I got it now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.