Implement a class that represents 2D-points (i.e., a point that has an x- and a y- position) Implement the following methods: distanceFromOrigin(): calculates and returns the distance to Point2D(0,0).
#this code
#p = Point2D(3,4)
#print(p.distanceFromOrigin())
#shall produce the following output
#5.0
import math
class Point2D:
def disctanceFromOrigin():
dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
return dist
p = Point2D(3,4)
print(p.distanceFromOrigin())
Can somebody tell me why this doesn't work? It gives me back a TypeError: object() takes no parameters
import math
class Point2D:
def __init__(self, x, y):
self.x = x
self.y = y
def disctanceFromOrigin(self):
origin = Point2D(0, 0)
dist = self.distanceFromPoint(self, origin)
return dist
def distanceFromPoint(self, point):
return math.sqrt((self.x - point.x)**2 + (self.y - point.y)**2
p = Point2D(3,4)
print(p.distanceFromOrigin())