| | |
Pythagoras?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 39
Reputation:
Solved Threads: 0
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))
def distanceBetweenPoints(p1, p2):
return float(math.sqrt((p2-p1)**2+(p2-p1)**2))
•
•
Join Date: Nov 2007
Posts: 147
Reputation:
Solved Threads: 32
0
#2 25 Days Ago
What error are you getting? It seems to run fine for me.
python Syntax (Toggle Plain Text)
>>> import math >>> def distance(p1, p2): ... return float(math.sqrt((p2-p1)**2+(p2-p1)**2)) ... >>> print distance(1,6) 7.07106781187 >>>
•
•
Join Date: Dec 2006
Posts: 1,024
Reputation:
Solved Threads: 288
0
#3 25 Days Ago
•
•
•
•
The get an error when i try to execute this?
Python Syntax (Toggle Plain Text)
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)
Last edited by woooee; 25 Days Ago at 8:08 pm.
Linux counter #99383
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.
0
#8 25 Days Ago
For example
python Syntax (Toggle Plain Text)
def myfunction(pointA, pointB): xa, ya = pointA.getX(), pointA.getY() xb, yb = pointB.getX(), pointB.getY() do_something_with(xa, ya, xb, yb)
Last edited by Gribouillis; 25 Days Ago at 7:50 am.
0
#10 25 Days Ago
The parameters p1 and p2 should not become P1 ad P2 the next line. Also, what does pyhon say ?
![]() |
Similar Threads
- Pythagoras Theorem in C++ (C++)
- Array Help (C++)
- school task help (Java)
- Method question (Java)
- Calculating distance between pixels (Java)
Other Threads in the Python Forum
- Previous Thread: Maze solving
- Next Thread: Problems with dictionaries
| Thread Tools | Search this Thread |







