Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不使用继承的点类和矩形类_Python - Fatal编程技术网

Python 不使用继承的点类和矩形类

Python 不使用继承的点类和矩形类,python,Python,我试图创建一个具有浮点值“x”和“y”的数据属性的point类,从而定义点在二维空间中的位置。此外,我希望有init的方法,比如它是默认值x=0和y=0的初始化。然后是一个移动函数,它接受“x”和“y”作为点的新位置。最后是一个函数,它告诉我们到一个点的距离。我想让它返回从这一点到x,y的另一点的欧几里德距离。如何做到这一点 以下是迄今为止我对上述描述的代码: import math class Point: def __init__(self): self.x =

我试图创建一个具有浮点值“x”和“y”的数据属性的point类,从而定义点在二维空间中的位置。此外,我希望有init的方法,比如它是默认值x=0和y=0的初始化。然后是一个移动函数,它接受“x”和“y”作为点的新位置。最后是一个函数,它告诉我们到一个点的距离。我想让它返回从这一点到x,y的另一点的欧几里德距离。如何做到这一点

以下是迄今为止我对上述描述的代码:

import math

class Point:

    def __init__(self):
        self.x = 0 # initialize to 0
        self.y = 0 # initialize to 0

    def move(self, x, y):
        self.x = x
        self.y = y 
在这一点和从这一点到另一点在x,y的欧几里安距离上可能需要帮助。目前我不确定我的想法是否正确。python新手,因此不知道如何测试此代码的功能。谢谢你的帮助


在这一部分之后,我希望能够在矩形的两个相对角定义两个点,并使用上面定义的点,而不使用继承。关于如何创建此类有什么想法吗?

您可以这样做(代码中的注释):

例子: 测验: 输出: 矩形类可以是这样的:[编辑以添加默认的
值]

Rectangle.\uuuu init\uuuu
中,作为参数提供的
点的x和y值的最小值和最大值被排序,以确定定义矩形的左上和右下点

class Rectangle:
        def __init__(self, p0: Point=Point(), p1: Point=Point(1.0, 1.0))-> None:  # <--- [edit]: now with default values
        x0, y0 = p0.x, p0.y
        x1, y1 = p1.x, p1.y
        self.topleft = Point(min(x0, x1), max(y0, y1))      # calculate the topleft and bottomright
        self.bottomright = Point(max(x0, x1), min(y0, y1))  # of the bounding box

    def __str__(self)-> str:
        return f'Rectangle defined by bbox at: {self.topleft}, {self.bottomright})'
输出: [编辑:]使用矩形中点的默认值: 输出:
def\uuuu init\uuuuu(self,x=0,y=0):
会更好,因此您可以在创建点时设置坐标。你可能会发现这个答案很有趣,因为你似乎很清楚这门课应该是什么样子。到底是什么阻止你简单地写它?@PM2Ring谢谢!我知道这是超基本的,但欧几里德距离只需要有两个点(x1,y1)(x2,y2)并将它们插入公式:sqrt((x1-x2)^2+(y1-y2)^2)…。因此,在python中,这将是math.Sqrt((x1-x2)**2+(y1-y2)**2)?@mkrieger1让我困惑的部分是取两个点,并使用上面定义的点定义矩形的两个相对角,也就是不使用继承。不知道该怎么做。对。虽然它是
math.sqrt
。如链接的答案所示,一个很好的方法是为Point类提供一个计算点到原点距离的
\uuu abs\uuu
魔术方法。如果您也给出点a
\uuu sub\uuu
方法,那么您可以执行
abs(a-b)
来获得点a和点b之间的距离。谢谢。Python新手,第一次看到:在参数列表中,它意味着什么?还有->无我从未见过这些使用我选择你的作为最佳答案我如何在矩形类的init方法中使用默认的p1=(0,0),p2=(1,1)进行初始化?我也不明白最小值和最大值发生了什么,请帮忙。谢谢。我编辑了答案以回答您的问题。在
Rectangle.\uuuu init\uuuu
中,对
点的x和y值的最小值和最大值进行排序,以确定定义矩形的左上点和右下点
p = Point(1, 2)
q = Point()
print(p, q, p.distance(q) == 5**0.5)
p.move_by(.1, -.1)
print(p)
Point(1, 2) Point(0.0, 0.0) True
Point(1.1, 1.9)
class Rectangle:
        def __init__(self, p0: Point=Point(), p1: Point=Point(1.0, 1.0))-> None:  # <--- [edit]: now with default values
        x0, y0 = p0.x, p0.y
        x1, y1 = p1.x, p1.y
        self.topleft = Point(min(x0, x1), max(y0, y1))      # calculate the topleft and bottomright
        self.bottomright = Point(max(x0, x1), min(y0, y1))  # of the bounding box

    def __str__(self)-> str:
        return f'Rectangle defined by bbox at: {self.topleft}, {self.bottomright})'
p = Point(1, 2)
q = Point()
print(p, q, p.distance(q) == 5**0.5)
p.move_by(.1, -.1)
print(p)

r = Rectangle(p, q)
print(r)
Point(1, 2) Point(0.0, 0.0) True
Point(1.1, 1.9)
Rectangle defined by bbox at: Point(0.0, 1.9), Point(1.1, 0.0))
s = Rectangle()
print(s)
Rectangle defined by bbox at: Point(0.0, 1.0), Point(1.0, 0.0))