943,965 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2372
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 7th, 2009
0

Pythagoras?

Expand Post »
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))
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gangster88 is offline Offline
65 posts
since Nov 2009
Nov 7th, 2009
0
Re: Pythagoras?
What error are you getting? It seems to run fine for me.
python Syntax (Toggle Plain Text)
  1. >>> import math
  2. >>> def distance(p1, p2):
  3. ... return float(math.sqrt((p2-p1)**2+(p2-p1)**2))
  4. ...
  5. >>> print distance(1,6)
  6. 7.07106781187
  7. >>>
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007
Nov 7th, 2009
0
Re: Pythagoras?
Quote ...
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.
Python Syntax (Toggle Plain Text)
  1. def distance_between_points(p1, p2):
  2. p2subp1 = p2-p1
  3. p1subp2 = p1-p2
  4. print "after subtraction", p2subp1, p1subp2
  5. ## one print for each step
  6.  
  7. point_1 = (1, 2)
  8. point_2 = (4, 6)
  9. distance_between_points(point_1, point_2)
Last edited by woooee; Nov 7th, 2009 at 8:08 pm.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006
Nov 7th, 2009
0
Re: Pythagoras?
..
Last edited by jaison2; Nov 7th, 2009 at 8:15 pm.
Reputation Points: 10
Solved Threads: 2
Light Poster
jaison2 is offline Offline
33 posts
since Nov 2009
Nov 7th, 2009
0
Re: Pythagoras?
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...
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gangster88 is offline Offline
65 posts
since Nov 2009
Nov 7th, 2009
0
Re: Pythagoras?
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.
Last edited by Gribouillis; Nov 7th, 2009 at 8:41 pm.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Nov 8th, 2009
0
Re: Pythagoras?
yeah but how do you do that?.. any examples you got?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gangster88 is offline Offline
65 posts
since Nov 2009
Nov 8th, 2009
0
Re: Pythagoras?
For example
python Syntax (Toggle Plain Text)
  1. def myfunction(pointA, pointB):
  2. xa, ya = pointA.getX(), pointA.getY()
  3. xb, yb = pointB.getX(), pointB.getY()
  4. do_something_with(xa, ya, xb, yb)
Last edited by Gribouillis; Nov 8th, 2009 at 7:50 am.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Nov 8th, 2009
0
Re: Pythagoras?
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))
Last edited by gangster88; Nov 8th, 2009 at 8:52 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gangster88 is offline Offline
65 posts
since Nov 2009
Nov 8th, 2009
0
Re: Pythagoras?
Click to Expand / Collapse  Quote originally posted by gangster88 ...
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 ?
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Alarm Question
Next Thread in Python Forum Timeline: Problems with dictionaries





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC