0

I'm trying to figure out a way to check if a certain point is inside or outside a circle on an isometric map. I'm currently using the following method to draw the circle:

    public static List<Coords> GetBrushCircleCoords(int x0, int y0, int radius)
    {
        List<Coords> coords = new List<Coords>();
        int x = radius;
        int y = 0;
        int err = 0;

        while (x >= y)
        {
            coords.Add(new Coords(x0 + x, y0 + y));
            coords.Add(new Coords(x0 + y, y0 + x));
            coords.Add(new Coords(x0 - y, y0 + x));
            coords.Add(new Coords(x0 - x, y0 + y));
            coords.Add(new Coords(x0 - x, y0 - y));
            coords.Add(new Coords(x0 - y, y0 - x));
            coords.Add(new Coords(x0 + y, y0 - x));
            coords.Add(new Coords(x0 + x, y0 - y));

            y += 1;
            err += 1 + 2 * y;
            if (2 * (err - x) + 1 > 0)
            {
                x -= 1;
                err += 1 - 2 * x;
            }
        }
        return coords;
    }

And the approach I'm trying to determine if the point is inside the circle is basically taking the desired point, determine its distance to the center and checking if it's bigger than the radius with the following method:

    public static int GetDistance(Coords _from, Coords _to)
    {
        return Math.Max(Math.Abs(_from.X - _to.X), Math.Abs(_from.Y - _to.Y));
    }

However, it seems the GetDistance method isn't the best way to calculate this as the distance calculated by it is fairly shorter than the one used on the GetBrushCircleCoords. What would be the correct way of determining if a certain point is inside/outside this circle?

Lithiumme
  • 41
  • 3
  • 1
    Some notes [here](https://stackoverflow.com/a/52921415/7444103). A probably more important note [here](https://stackoverflow.com/a/50478311/7444103). – Jimi Apr 12 '20 at 18:29

1 Answers1

0

On a Euclidean plane distance function (metric) is given by the Pythagorean Theorem. So shouldn't GetDistance be something like:

public static double GetDistance(Coords from, Coords to)
{
    //a^2 + b^2 = c^2
    var a = from.X - to.X;
    var b = from.Y - to.Y;
    var c = Math.Sqrt(a*a+b*b);
    return c;
}
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67
  • I've tried this approach and it's still not accurate tho.. I'd say that around 30% of the coords around the edges are still being flagged as if they were outside the circle even tho they're not. I appreciate the help tho. – Lithiumme Apr 12 '20 at 18:26
  • There will always be rounding errors very near the boundary. – David Browne - Microsoft Apr 12 '20 at 18:29
  • Yea but what I meant is that it has an error of ~30%. I tried a drawing a circle with a radius of 100 and it was flagging every point with a distance higher than 67 as if they were outside the circle – Lithiumme Apr 12 '20 at 18:33
  • Because you are using `int` instead of `double`? – David Browne - Microsoft Apr 12 '20 at 18:34
  • I'm not entirely sure if I follow? This is an iso grid without decimals. I'm passing the x,y coords as doubles to run the GetDistance method you suggested – Lithiumme Apr 12 '20 at 18:59