What's wrong with:
def project_to_distance(point_x, point_y, distance):

`````` dist_to_origin = math.square_root(point_x ** 2 + point_y ** 2)

scale = distance / dist_to_origin
temp1= point_x * scale
temp2= point_y * scale
``````

print temp1, temp2

print project_to_distance(2, 7, 4)

3
Contributors
2
Replies
21
Views
3 Years
Discussion Span
Last Post by vegaseat
1. your indentation is not correct (and can not be corrected because you did not say what you are trying to do).
2. you do not import math
3. math does not contain a square_root function. Try help(math) to get a list of built-in functions for math
4. project_to_distance does not have a return statement Click Here so

print project_to_distance(2, 7,4)

prints None
5. the variable, scale, does not exist outside of the function in the code posted

``````temp1= point_x * scale
``````

Edited by woooee

Here is a general example that might help ...

``````''' distance_two_points.py
calculate the distance between two points given the coordinates
of the points (x1, y1) and (x2, y2) in a 2D system
'''

import math

def get_distance(x1, y1, x2, y2):
'''
returns distance between two points using the pythagorean theorem
the function parameters are the 2D coordinates of the two points
'''
dx = x2 - x1
dy = y2 - y1
return math.sqrt(dx**2 + dy**2)

sf = "Distance between point({}, {}) and point({}, {}) is {}"

x1, y1 = 1, 3
x2, y2 = 4, 7
print(sf.format(x1, y1, x2, y2, get_distance(x1, y1, x2, y2)))

x1, y1 = 2, 5
x2, y2 = 11, 23
print(sf.format(x1, y1, x2, y2, get_distance(x1, y1, x2, y2)))

''' result ...
Distance between point(1, 3) and point(4, 7) is 5.0
Distance between point(2, 5) and point(11, 23) is 20.1246117975
'''
``````
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.