【问题标题】:get distance between two Points [duplicate]获取两点之间的距离[重复]
【发布时间】:2020-12-15 01:43:55
【问题描述】:

所以我试图检查一个点是否位于一个圆圈内。 我创建了这两个类:

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

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

现在我有了这个功能:

def in_circle(circle: Circle, point: Point) -> bool:

我的想法是当Point到中心的距离大于半径时,它必须在圆的外面。 我现在的问题是如何获得 Point(x,y) 和中心之间的距离。逻辑上我认为我可以通过使用毕达哥拉斯来解决这个问题,但我的问题是我刚开始使用 Python 并且不太了解语法。 提前致谢。

【问题讨论】:

  • 毕达哥拉斯是你的朋友。

标签: python geometry


【解决方案1】:

你可以使用勾股定理。

(x-center_x)^2 + (y - center_y)^2 < radius^2

center_x 是圆心的 X 坐标,center_y 是圆心的 Y 坐标,如果它们的平方和小于半径的平方。

那么你必须在python中实现它if

  • 但是有一个问题,你接受一个参数center,它应该是一个元组或一个列表,你应该将它解包到Point类的构造函数中,使用*

  • in_circle() 作为方法添加到Circle

实施:

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

class Circle:
  def __init__(self, center, radius):
        self.center = Point(*center)
        self.radius = radius   

  def in_circle(self, point: Point) -> bool:
        if (((point.x-self.center.x)**2+(point.y-self.center.y)**2)<self.radius**2):
            return True
        else:
            return False

如果你想完全这样调用,这里有另一个实现:

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

class Circle:
  def __init__(self, center: Point, radius):
        self.center = center
        self.radius = radius   

def in_circle(circle: Circle, point: Point) -> bool:
      if (((point.x-circle.center.x)**2+(point.y-circle.center.y)**2)<circle.radius**2):
          return True
      else:
          return False

circle = Circle(Point(0, 0), 2)  
print(in_circle(circle, Point(1, 1)))

【讨论】:

  • 非常感谢,这对我来说很有意义,但是当我尝试像这样测试它时:circle = Circle(Point(0, 0), 2) in_circle(circle, Point(1, 1)) 它给了我这个 TypeError: type object argument after * must be an iterable, not点
  • 拨打circle = Circle((0, 0), 2)然后circle.in_circle(Point(1, 1))
  • 效果很好,问题是我不允许在任务中更改这些行。
  • 请检查编辑
  • 感谢您的努力,现在一切正常。
【解决方案2】:

Python 有一个 math.hypot 函数可以获取三角形的斜边:

import math

def distance(p1: Point, p2: Point) -> float:
    return math.hypot(p2.x - p1.x, p2.y - p1.y)

或者,您可以使用math.sqrt** 自己进行求幂:

import math

def distance(p1: Point, p2: Point) -> float:
    return math.sqrt((p2.x - p1.x) ** 2 + (p2.y - p1.y) ** 2)

【讨论】:

    【解决方案3】:
    def in_circle(circle: Circle, point: Point) -> bool:
        # If Point is located inside circle, it will return True.
        # Pythagoras Theorem
        if (pow((point.x-circle.center.x),2)+pow((point.y-circle.center.y),2))<pow((circle.radius),2):
            return True
        
        # If Piont is not located inside circle 
        return False
    

    pow(x,y) 函数返回 x 的 y 次幂 (x^y)。

    【讨论】:

      【解决方案4】:

      你可以做的是:

          def in_circle(circle: Circle, point: Point) -> bool:
               if ((point.y-circle.center.y)**2+(point.x-circle.center.x)**2)**(1/2)<=circle.radius:
                  return true
               else:
                  return false
      

      【讨论】:

      • 非常感谢!好像我之前尝试过的时候已经很接近了。问题是当我尝试像这样运行它时: circle = Circle(Point(0, 0), 2) in_circle(circle, Point(1, 1)) 它给了我一个 NameError: name 'x' is not defined
      • 确实,我写错了:我写的 Point.x 意思是“类点的 x”,这没有意义。我把它改成了point.x,意思是“对象点的x”,我认为它应该知道。
      猜你喜欢
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      • 2014-02-17
      • 2014-01-21
      • 2011-02-14
      • 2020-12-22
      • 2013-01-15
      • 2011-05-28
      相关资源
      最近更新 更多