2

I'm currently to come up with a solution towards finding the distance the point and center of the a circle.

I have to add the following method to my Circle class, which given another point's x and y coordinates, in which the method returns whether or not that point is within the bounds of the Circle or not.

I think to accomplish the draw of the circle with a center point and radius, i will have to draw two others points, one inside the circle and one outside. How could I determine which point is inside the circle and which point is outside of the circle?

I'm asking for the distance between two points and center of a circle.

Here's the code I've written so far.

public bool Contains(float px, float py)
        {
            (Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2)) < (d * d);
            return mContains;
        }
ThunderCat
  • 21
  • 4
  • Possible duplicate of [Equation for testing if a point is inside a circle](http://stackoverflow.com/questions/481144/equation-for-testing-if-a-point-is-inside-a-circle) – aybe Jun 10 '16 at 21:56
  • not really, since im asking distance between to points and center of a circle – ThunderCat Jun 10 '16 at 21:59

1 Answers1

2

Well, if you have a property x and y, the radius and you're given a point (x1, y1), you can easily test if it's inside the circle:

bool IsInCircle(int x1, int y1) 
{
    return Math.Sqrt(Math.Pow(x1 - this.x, 2) + Math.Pow(y1 - this.y, 2)) <= this.radius;
}

Then just check it for both of your points - one will give true, the other false

If you want to have a function that gets two points, you can just return an int - 1 if the first is inside, 2 if the second, 0 if none, 3 if both:

int AreInCircle(int x1, int y1, int x2, int y2) 
    {
        bool a = Math.Sqrt(Math.Pow(x1 - this.x, 2) + Math.Pow(y1 - this.y, 2)) <= this.radius;
        bool b = Math.Sqrt(Math.Pow(x2 - this.x, 2) + Math.Pow(y2 - this.y, 2)) <= this.radius;
        return a && b ? 3 : (!a && !b ? 0 : (a ? 1 : 2));
    }
Yotam Salmon
  • 2,400
  • 22
  • 36