Pythagoras?
I have to get pthagaros to get the distance between 2 points. so the function distanceBetweenPoints(Point(1, 2), Point(4, 6)) should result in 5.0. the parameters are supposed to be P1, P2, which i think i have done?. The get an error when i try to execute this?.. help please
def distanceBetweenPoints(p1, p2):
return float(math.sqrt((p2-p1)**2+(p2-p1)**2))
gangster88
Junior Poster in Training
65 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
The get an error when i try to execute this?
Not enough info for anyone to help (could be an indentation error, or could be in the calcs). First add some print statements for the individual calcs to isolate the error, and post the specific error message.
def distance_between_points(p1, p2):
p2subp1 = p2-p1
p1subp2 = p1-p2
print "after subtraction", p2subp1, p1subp2
## one print for each step
point_1 = (1, 2)
point_2 = (4, 6)
distance_between_points(point_1, point_2)
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
but the thing is we need it in this form.. distanceBetweenPoints(Point(1, 2), Point(4, 6)), so when we call the function and type it in we get the answer...
gangster88
Junior Poster in Training
65 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
According to your previous post, I think that you are using the "graphics.py" module like this one . In that case, since p1 and p2 are not numbers but Points, you must use first p1.getX() and p1.getY() to get the coordinates of the points.
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
yeah but how do you do that?.. any examples you got?
gangster88
Junior Poster in Training
65 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
For example
def myfunction(pointA, pointB):
xa, ya = pointA.getX(), pointA.getY()
xb, yb = pointB.getX(), pointB.getY()
do_something_with(xa, ya, xb, yb)
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
anything around these lines?
def distanceBetweenPoints(p1, p2):
x1, y1 = P1.getX(), P1.getY()
x2, y2 = P2.getX(), P2.getY()
distance = float(math.sqrt((x2-x1)**2+(y2-y1)**2))
gangster88
Junior Poster in Training
65 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
anything around these lines?
def distanceBetweenPoints(p1, p2):
x1, y1 = P1.getX(), P1.getY()
x2, y2 = P2.getX(), P2.getY()
distance = float(math.sqrt((x2-x1)**2+(y2-y1)**2))
The parameters p1 and p2 should not become P1 ad P2 the next line. Also, what does pyhon say ?
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
The parameters p1 and p2 should not become P1 ad P2 the next line. Also, what does pyhon say ?
def distanceBetweenPoints(p1, p2):
x1, y1 = p1.getX(), p1.getY()
x2, y2 = p2.getX(), p2.getY()
distanceBetweenPoints=float(math.sqrt((x2-x1)**2+(y2-y1)**2))
what about nw?.. python gives an error says "the name Point is not defined"
gangster88
Junior Poster in Training
65 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
what about nw?.. python gives an error says "the name Point is not defined"
add
from graphics import *
at the top of your file. Also please use code tags to post your python code. And also post python error tracebacks if there is one.
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
you forgot the return statement. This is python, not pascal :)
from graphics import *
import math
def distanceBetweenPoints(p1, p2):
x1, y1 = p1.getX(), p1.getY()
x2, y2 = p2.getX(), p2.getY()
return math.sqrt((x2-x1)**2+(y2-y1)**2)
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
When you post a question like that, give as much pertinent information as you can. You needed to tell us right from the start that you are using the Zelle module graphics. Also, read the code that folks respond with and check it against the errors in your code.
I have to give Gribouillis and A+ for patience! Try this code, it seems to work now ...
from graphics import *
import math
def distanceBetweenPoints(p1, p2):
x1, y1 = p1.getX(), p1.getY()
x2, y2 = p2.getX(), p2.getY()
return math.sqrt( (x2-x1)**2 + (y2-y1)**2 )
p1 = Point(1, 2)
p2 = Point(4, 6)
print( distanceBetweenPoints(p1, p2) ) # --> 5.0
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
yup.. thankyou so much "Gribouillis"... it wrks... cheers..
gangster88
Junior Poster in Training
65 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
When you post a question like that, give as much pertinent information as you can. You needed to tell us right from the start that you are using the Zelle module graphics. Also, read the code that folks respond with and check it against the errors in your code.
I have to give Gribouillis and A+ for patience! Try this code, it seems to work now ...
from graphics import *
import math
def distanceBetweenPoints(p1, p2):
x1, y1 = p1.getX(), p1.getY()
x2, y2 = p2.getX(), p2.getY()
return math.sqrt( (x2-x1)**2 + (y2-y1)**2 )
p1 = Point(1, 2)
p2 = Point(4, 6)
print( distanceBetweenPoints(p1, p2) ) # --> 5.0
ok will make sure.. i do everything possible frm next time.. you learn frm your mistakes right?.. ;)... thx for the help guys
gangster88
Junior Poster in Training
65 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0