Python语言中心的一个圆使用OOP [英] Python language-center of a circle using OOP

查看:416
本文介绍了Python语言中心的一个圆使用OOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Point:

    def __init__(self, initX, initY):
        """ Create a new point at the given coordinates. """
        self.x = initX
        self.y = initY

    def getX(self):
        return self.x

    def getY(self):
        return self.y

    def distanceFromOrigin(self):
        return ((self.x ** 2) + (self.y ** 2))** 0.5

    def __str__(self):
        return "x=" + str(self.x) + ", y=" + str(self.y)

    def get_line_to(self, target):
        mx = (-target.x + self.x ) 
        my = (-target.y + self.y)
        grad=my/mx
        c=-(grad*(self.x))+self.y
        return grad
    def halfway(self, target):
        """calculating midpoint"""
        mx = (self.x + target.x) / 2
        my = (self.y + target.y) / 2
        return Point(mx, my)

def cencd(p1,p2,p3):
    """calculating the center of a circle"""
    ma=(p2.getY-p1.getY)/(p2.getX-p1.getX)
    mb=(p3.getY-p2.getY)/(p3.getX-p2.getX)
    hw=p1.halfway(p2)
    x=(ma*mb*(p1.getY-p3.getY)+mb*(p1.getX+p2.getX)-ma*(p2.getX+p3.getX))/2*(mb-ma)
    ya=-(1/ma)*((x-hw.getX)+hw.getY)
    return x,ya

"""defining the points for p1,p2 and p3"""

    p = Point(5,5)

    q = Point(6,-2)

    r=Point(2,-4)

    print(cencd(p,q,r))



获取此错误消息:SyntaxError:
的函数定义中的重复参数'p1'Traceback(最近调用最后):
文件python,第45行,在
文件python line 34,in cencd
TypeError: - :'method'和'method'的不支持的操作数类型

I get this error message:SyntaxError: duplicate argument 'p1' in function definition on Traceback (most recent call last): File "python", line 45, in File "python", line 34, in cencd TypeError: unsupported operand type(s) for -: 'method' and 'method'

请协助。
working solution

please assist. """working solution """"

ma=(p2.y-p1.y)/(p2.x-p1.x)
mb=(p3.y-p2.y)/(p3.x-p2.x)
hw=p1.halfway(p2)

x1=(ma*mb*(p1.y-p3.y)+mb*(p1.x+p2.x)-ma*(p2.x+p3.x))/(2*(mb-ma))
ya=-(1/ma)*((x1-hw.x))+hw.y


推荐答案

getX getY 因此你需要使用 getX() getY()调用它们。

Both getX and getY are methods in your code, not attributes. So you will need to call them using getX() and getY().

因此 ma =(p2.getY-p1.getY)/(p2.getX-p1.getX)成为:

ma = (p2.getY()-p1.getY())/(p2.getX()-p1.getX())

等等,其他代码更改。

否则,您还可以将方法定义为 @property

Otherwise, you can also define your methods as @property:

class Point:
    ...
    ...
    @property
    def getX(self):
        return self.x
    @property
    def getY(self):
        return self.y
    ...

现在, p1.getX p2.getY 等。

请注意,上面的@property装饰器将方法转换为一个getter,这是有意义的,只使用私有变量(定义变量开始 _ )。

Note that the above @property decorator turns the method into a getter, which makes sense to use only with private variables (variables defined to start with _).

因此,由于x和y都是类的正常属性,因此您可以直接访问它们,而无需使用和属性装饰器或使用getter方法,例如 p1 .x p2.y ,作为@Padraic在他的帖子中指出。

As such, since both x and y are normal attributes of your class, you can access them directly without using and property decorators or using getter methods, like p1.x and p2.y, as @Padraic points in his post.

这篇关于Python语言中心的一个圆使用OOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆