Check if a circle is contained in another circle

前端 未结 4 761
青春惊慌失措
青春惊慌失措 2020-12-15 12:33

I\'m trying to check if a circle is contained within another circle. I\'m not sure if the math behind it is the problem or if its my if statement because I keep getting

4条回答
  •  佛祖请我去吃肉
    2020-12-15 13:18

    CODE ACCORDING TO THE IMAGE IN THE ACCEPTED ANSWER (without getters, as it's more readable):

    import math
    def contains(self, circle):
      d = math.sqrt(
            (circle.center[0] - self.center[0]) ** 2 +
            (circle.center[1] - self.center[1]) ** 2)
      return self.radius  > (d + circle.radius)
    

    I used it, and it works. In the following plot, you can see how the circles completely contained in the red big one are painted in green, and the others in black:

提交回复
热议问题