给定两点找到坡度-Python类

时间:2019-04-24 03:02:28

标签: python geometry

我是python的新手,我试图弄清楚为什么这段代码不起作用。

<Route exact path='/' component={Home} />
  <Route exact path='/calendar' component={Calendar} />

要使此代码正常工作,我应该进行哪些更改?谢谢

1 个答案:

答案 0 :(得分:1)

您的计算有些混乱:

slope_from_origin是线段经过原点Point(0, 0)并给定点的斜率。

slope_between_two_points是穿过两个给定点的线段的斜率。

slope是一个标量,您试图用它构造一个Point并返回它

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def slope_between_two_points(self, p1=Point(0, 0)):  # uses default argument to define the origin
        if p1.x - self.x == 0:
            return float('inf')   # prevent dividing by zero when slope is infinite
       return (p1.y - self.y) / (p1.x - self.x)

    def slope_from_origin(self):
        return self.slope_between_two_points()

    def __str__ (self):
        return str(slope)

p = Point(6, 7)
q = Point(3, 4)
print(p.slope_between_two_points(q), p.slope_from_origin(), q.slope_from_origin())