I'm trying to compute a signed distance field of an black and white images pixels, but I think I've managed to get my code wrong somewhere. As this is my input and output:

Input

Input

Output

Output

The issue I'm having is the black line in the middle of the S, my understanding leaves me to believe that it should be completely light gray?

This is the code I'm using:

    for (int x = 0; x < source.width; ++x)
    {
        for(int y = 0; y < source.height; ++y) 
        {
            // Get pixel
            float a = source.GetPixel(x, y).r;

            // Distance to closest pixel which is the inverse of a
            // start on float.MaxValue so we can be sure we found something
            float distance = float.MaxValue;

            // Search coordinates, x min/max and y min/max
            int fxMin = Math.Max(x - searchDistance, 0);
            int fxMax = Math.Min(x + searchDistance, source.width);
            int fyMin = Math.Max(y - searchDistance, 0);
            int fyMax = Math.Min(y + searchDistance, source.height);

            for (int fx = fxMin; fx < fxMax; ++fx)
            {
                for (int fy = fyMin; fy < fyMax; ++fy)
                {
                    // Get pixel to compare to
                    float p = source.GetPixel(fx, fy).r;

                    // If not equal a
                    if (a != p)
                    {
                        // Calculate distance
                        float xd = x - fx;
                        float yd = y - fy;
                        float d = Math.Sqrt((xd * xd) + (yd * yd));

                        // Compare absolute distance values, and if smaller replace distnace with the new oe
                        if (Math.Abs(d) < Math.Abs(distance))
                        {
                            distance 
Edit
Report