educoder-头哥 python 定义一个类描述平面上的点并提供移动点和计算到另一个点距离的方法

from math import sqrt

class Point(object):

    def __init__(self, x=0, y=0):
        """初始化方法

        :param x: 横坐标
        :param y: 纵坐标
        """
        self.x = x
        self.y = y

    def move_to(self, x, y):
        """移动到指定位置

        :param x: 新的横坐标
        "param y: 新的纵坐标
        :return : 无返回值
        """
        #        请在此处添加代码       #
        # *************begin************#
        self.x = x
        self.y = y
        # **************end*************#

    def move_by(self, dx, dy):
        """移动指定的增量

        :param dx: 横坐标的增量
        "param dy: 纵坐标的增量
        :return : 无返回值
        """
        #        请在此处添加代码       #
        # *************begin************#
        self.x += dx
        self.y += dy
        # **************end*************#

    def distance_to(self, other):
        """计算与另一个点的距离

        :param other: 另一个点,坐标为(other.x,other.y)
        :return :返回两点之间的距离
        """
        #        请在此处添加代码       #
        # *************begin************#
        dx = self.x - other.x
        dy = self.y - other.y
        temp = dx * dx + dy * dy
        return sqrt(temp)
        # **************end*************#

    def __str__(self):
        return '(%s, %s)' % (str(self.x), str(self.y))


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值