0
How do write python code to print the distance of two coordinates, x and y, using oop approach.
6 Respuestas
+ 7
You'll need a Coordinate object with a pair of x and y value, followed by a function to compute the distance using Pythagorean theorem. 😉
+ 2
"""
What are you calling "distance of two coordinates" ?
"Coordinates" could define a dot or a vector...
You can compute distance between two points (with each two coordinates if those points are in a 2D space), or the distance of a point to the origin as the length of the vector defined by the same pair of coordinates... (and distance between two points is the length of the vector defined by these two points)
"""
# Anyway, it would be more efficient (and more pythonic) to use built-in math.hypot(x,y) for computing pythagorean theorem (size of hypotenuse):
import math
x = 3
y = 4
dist = math.hypot(x,y) # 5.0 <=> sqrt( 3*3 + 4*4 )
# if you search length of vector OP where O is the origin (0;0) and P a point (3;5)
# if you search distance between two points P1 and P2, this is equivalent to get the length of the vector P1P2, same as OP where P (x2-x1;y2-y1)
+ 1
Like suggested you must use pythagorean theorem... Simple Pseudo code (i have defined a Point class but is an example only):
define Point as class:
define in Point attributes x,y
define in Point method distance(o)
# ---you must get the distance vector
# dist_x = difference of x's
# dist_y = difference of y's
# ---calculate distance with pythagorean th
# distance= sqrt(dist_x^2 + dist_y^2)
0
pls can u help me with the code. I am a novice in python oop.
0
thank u very much @krow
0
👍👍👍