import math
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return "({}, {})".format(self.x, self.y)
def to_polar(self):
r = math.sqrt( self.x**2 + self.y**2 )
theta = math.degrees( math.atan( self.y / self.x ) )
return r, theta
def distance(self, other):
return math.sqrt((self.x - other.x)**2 + (self.y - other.y)**2)
p1 = Point(1,3)
print(p1)
p1
p2 = Point(-1,1)
p1.distance(p2)
p1.x
p1.y
class Line(object):
def __init__(self, p0, p1):
self.p0 = p0
self.p1 = p1
def is_vertical(self):
return self.p0.x == self.p1.x
def get_slope(self):
if self.is_vertical():
return float('inf')
else:
return (self.p1.y - self.p0.y) / (self.p1.x - self.p0.x)
def get_y_intercept(self):
if self.is_vertical():
if self.p0.x == 0:
return 0.0
else:
return float('NaN')
else:
return self.p0.y - self.get_slope()*self.p0.x
def __repr__(self):
if self.is_vertical():
return "x = {}".format(self.p0.x)
else:
return "y = {}*x + {}".format(self.get_slope(), self.get_y_intercept())
# y = 5x + 7
p = Point(0, 7)
q = Point(1, 12)
l = Line(p, q)
print(l)
class Point(object):
def __init__(self, x, y):
self.set_x(x)
self.set_y(y)
def get_x(self):
return self._x
def set_x(self, x):
# The following line checks whether 'x' is an int or a float.
# If it isn't, it raises an "exception", which we'll see in
# more detail later in the quarter
if not isinstance(x, (int, float)): raise ValueError("Not a number")
self._x = x
def get_y(self):
return self._y
def set_y(self, y):
if not isinstance(y, (int, float)): raise ValueError("Nor a number")
self._y = y
def __repr__(self):
return "({}, {})".format(self._x, self._y)
def to_polar(self):
r = math.sqrt( self._x**2 + self._y**2 )
theta = math.degrees( math.atan( self._y / self._x ) )
return r, theta
def distance(self, other):
return math.sqrt((self._x - other._x)**2 + (self._y - other._y)**2)
p = Point(2,3)
p.get_x()
p.set_x(3.14)
p
p.set_x("rofl")
p._x
p._x = 5
p
p = Point(0, 7)
q = Point(1, 12)
l = Line(p, q)
l.get_slope()
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def get_x(self):
return self._x
def set_x(self, x):
if not isinstance(x, (int, float)): raise ValueError("Not a number")
self._x = x
def get_y(self):
return self._y
def set_y(self, y):
if not isinstance(y, (int, float)): raise ValueError("Nor a number")
self._y = y
# Property definitions. Must be done *after* we've defined the getters/setters.
x = property(get_x, set_x)
y = property(get_y, set_y)
def __repr__(self):
return "({}, {})".format(self.x, self.y)
def to_polar(self):
r = math.sqrt( self.x**2 + self.y**2 )
theta = math.degrees( math.atan( self.y / self.x ) )
return r, theta
def distance(self, other):
return math.sqrt((self.x - other.x)**2 + (self.y - other.y)**2)
p = Point(2,3)
p.x
p.y
p.x = 6
p
p.x = "foo"
p = Point(0, 7)
q = Point(1, 12)
l = Line(p, q)
l.get_slope()
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
# Notice how the name of the method is not "get_x" but simply "x"
# the name of the attribute.
@property
def x(self):
return self._x
# Now we define the setter, which has the same name, but an
# additional parameter (the value being set). Notice how the
# decorator isn't "@property", it's the name the attribute and .setter
@x.setter
def x(self, x):
if not isinstance(x, (int, float)): raise ValueError("Not a number")
self._x = x
@property
def y(self):
return self._y
@y.setter
def y(self, y):
if not isinstance(y, (int, float)): raise ValueError("Nor a number")
self._y = y
def __repr__(self):
return "({}, {})".format(self.x, self.y)
def to_polar(self):
r = math.sqrt( self.x**2 + self.y**2 )
theta = math.degrees( math.atan( self.y / self.x ) )
return r, theta
def distance(self, other):
return math.sqrt((self.x - other.x)**2 + (self.y - other.y)**2)
p = Point(2,3)
p.x
p.x = 5
p
p.x = "foo"