类点,线多类?

2024-03-28 20:32:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我做这个练习。我得到了Line和{}类。我要找出直线的斜率,如果点在直线上。但我的问题是我有两个类,def isonline需要所有的属性x,y,a,b,c。所以这就是我所做的。你觉得我该怎么解决这个问题吗?在

class Point:
    def __init__(self,x,y):
        self.x=x
        self.y=y
class Line:
    def __init__(self,a,b,c):
        self.a=a
        self.b=b
        self.c=c
    def slope(self):
        try:
            return -self.a/self.b
        except ZeroDivisionError:
            return None
    def isonline(self,Point):
        if (self.a*self.x+self.b*self.y+self.c)==0:
            return True
        else:
            return False


coordinatesPoint=Point(4,1)
abcfromLine=Line(10,2,1)

print abcfromLine.slope()
print abcfromLine.isonline(coordinatesPoint)

Tags: selfreturn属性initdeflineslope直线
1条回答
网友
1楼 · 发布于 2024-03-28 20:32:44

我认为Point.xPoint.y会解决你的问题。在

class Point:
    def __init__(self,x,y):
        self.x=x
        self.y=y
class Line:
    def __init__(self,a,b,c):
        self.a=a
        self.b=b
        self.c=c
    def slope(self):
        try:
            return -self.a/self.b
        except ZeroDivisionError:
            return None
    def isonline(self, point):
        if (self.a*point.x+self.b*point.y+self.c)==0:
            return True
        else:
            return False


coordinatesPoint=Point(4,1)
abcfromLine=Line(10,2,1)

print abcfromLine.slope()
print abcfromLine.isonline(coordinatesPoint)

顺便说一句,我更喜欢用小写字母作为参数。在

相关问题 更多 >