Pythagoras?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2009
Posts: 39
Reputation: gangster88 is an unknown quantity at this point 
Solved Threads: 0
gangster88 gangster88 is offline Offline
Light Poster

Pythagoras?

 
0
  #1
25 Days Ago
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))
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 147
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 32
mn_kthompson mn_kthompson is offline Offline
Junior Poster
 
0
  #2
25 Days Ago
What error are you getting? It seems to run fine for me.
  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. >>>
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,024
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 288
woooee woooee is online now Online
Veteran Poster
 
0
  #3
25 Days Ago
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.
  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; 25 Days Ago at 8:08 pm.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 33
Reputation: jaison2 is an unknown quantity at this point 
Solved Threads: 1
jaison2 jaison2 is offline Offline
Light Poster
 
0
  #4
25 Days Ago
..
Last edited by jaison2; 25 Days Ago at 8:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 39
Reputation: gangster88 is an unknown quantity at this point 
Solved Threads: 0
gangster88 gangster88 is offline Offline
Light Poster
 
0
  #5
25 Days Ago
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...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 932
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 216
Gribouillis's Avatar
Gribouillis Gribouillis is online now Online
Posting Shark
 
0
  #6
25 Days Ago
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; 25 Days Ago at 8:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 39
Reputation: gangster88 is an unknown quantity at this point 
Solved Threads: 0
gangster88 gangster88 is offline Offline
Light Poster
 
0
  #7
25 Days Ago
yeah but how do you do that?.. any examples you got?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 932
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 216
Gribouillis's Avatar
Gribouillis Gribouillis is online now Online
Posting Shark
 
0
  #8
25 Days Ago
For example
  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; 25 Days Ago at 7:50 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 39
Reputation: gangster88 is an unknown quantity at this point 
Solved Threads: 0
gangster88 gangster88 is offline Offline
Light Poster
 
0
  #9
25 Days Ago
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; 25 Days Ago at 8:52 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 932
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 216
Gribouillis's Avatar
Gribouillis Gribouillis is online now Online
Posting Shark
 
0
  #10
25 Days Ago
Originally Posted by gangster88 View Post
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 ?
Reply With Quote Quick reply to this message  
Reply

Tags
port

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC