0

so I'm trying to check if a Point is located inside a Circle. I created these 2 classes:

 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   

now I have this function:

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

My thinking was that when the distance between the Point and the center is higher than the radius, it has to be outside the circle. My question now is how I can get the distance between the Point(x,y) and the center. Logically I think I can solve the problem by using Pythagoras, but my problem is that I just started Python and don't understand the syntax so well. Thanks in advance.

4 Answers4

0

Python has a math.hypot function that gets the hypotenouse of a triangle:

import math

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

Alternatively, you can do it yourself using math.sqrt and ** for exponentiation:

import math

def distance(p1: Point, p2: Point) -> float:
    return math.sqrt((p2.x - p1.x) ** 2 + (p2.y - p1.y) ** 2)
Aplet123
  • 33,825
  • 1
  • 29
  • 55
0

What you can do is :

    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
  • Thanks very much! Seems like I was pretty close when I tried it before. Problem is that when I try to run it like this: circle = Circle(Point(0, 0), 2) in_circle(circle, Point(1, 1)) it gives me a NameError: name 'x' is not defined –  Dec 13 '20 at 16:26
  • Indeed, I wrote something wrong : I wrote Point.x that means "the x of the Class point" which doesn't make sense. I changed this to point.x which means "the x of the object point", I think it should work know. – Virgile BRIAN Dec 14 '20 at 17:46
0

You can use Pythagorean theorem.

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

center_x is the X coordinate of the center and center_y is Y coordinate of the center if their squared sum is less than the square of radius.

Then you have to Implement it in python if.

  • But there is a problem, you take an argument center, it should be a tuple or a list and you should unpack it to the constructor of Point class, using *.

  • Add in_circle() as a method to the Circle class

Implementation:

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

If you want to call exactly like that, here's another implementation:

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)))
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • Thank you very much, this makes perfect sense for me, but when I try to test it like this: `circle = Circle(Point(0, 0), 2) in_circle(circle, Point(1, 1))` it gives me this TypeError: type object argument after * must be an iterable, not Point –  Dec 13 '20 at 17:03
  • Call like `circle = Circle((0, 0), 2)` and then `circle.in_circle(Point(1, 1))` – Wasif Dec 13 '20 at 17:04
  • That works perfectly, the problem is that I'm not allowed to change these lines as part of the task. –  Dec 13 '20 at 17:07
  • Check the edit please – Wasif Dec 13 '20 at 17:12
  • thanks for your efforts mate, it works perfectly fine now. –  Dec 13 '20 at 17:32
0
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

The pow(x,y) function returns the value of x to the power of y (x^y).