TheAlgorithms/C-Sharp · error

should be greater than zero

Error message

{nameof(bitmapWidth)} should be greater than zero

What it means

GetKochSnowflake draws a Koch snowflake fractal into an SKBitmap of the requested width. The library validates that bitmapWidth is strictly positive before allocating and computing offsets, throwing ArgumentOutOfRangeException otherwise. A non-positive width cannot be used to create a bitmap, so the throw is an early fail-fast guard.

Solutions

  1. Pass a bitmapWidth > 0, e.g. the default 600
  2. Validate the width at the call site before invoking GetKochSnowflake
  3. If the value comes from config or user input, clamp or reject non-positive values where it is parsed

Example fix

// before
var bitmap = KochSnowflake.GetKochSnowflake(userWidth);
// after
if (userWidth <= 0) userWidth = 600;
var bitmap = KochSnowflake.GetKochSnowflake(userWidth);
Defensive patterns

Strategy: validation

Validate before calling

if (bitmapWidth <= 0) throw new ArgumentException("bitmapWidth must be positive", nameof(bitmapWidth));

Type guard

static bool IsValidBitmapWidth(int w) => w > 0;

Try / catch

try { var bmp = KochSnowflake.GetKochSnowflake(width); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "bitmapWidth")
{
    // fallback: use default width 600
}

Prevention

When it happens

Trigger: Calling Algorithms.Other.KochSnowflake.GetKochSnowflake with bitmapWidth <= 0, e.g. GetKochSnowflake(0) or GetKochSnowflake(-100), or passing a computed width that evaluated to zero or a negative number.

Common situations: Widths read from config files, command-line args, or UI inputs that were not validated; integer division or subtraction upstream producing 0; callers passing 0 as a default placeholder meaning 'unset'.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/04b172fc22b26243. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Other/KochSnowflake.cs:58

        }

        return vectors;
    }

    /// <summary>
    ///     Method to render the Koch snowflake to a bitmap. To save the
    ///     bitmap the command 'GetKochSnowflake().Save("KochSnowflake.png")' can be used.
    /// </summary>
    /// <param name="bitmapWidth">The width of the rendered bitmap.</param>
    /// <param name="steps">The number of iterations.</param>
    /// <returns>The bitmap of the rendered Koch snowflake.</returns>
    public static SKBitmap GetKochSnowflake(
        int bitmapWidth = 600,
        int steps = 5)
    {
        if (bitmapWidth <= 0)
        {
            throw new ArgumentOutOfRangeException(
                nameof(bitmapWidth),
                $"{nameof(bitmapWidth)} should be greater than zero");
        }

        var offsetX = bitmapWidth / 10f;
        var offsetY = bitmapWidth / 3.7f;
        var vector1 = new Vector2(offsetX, offsetY);
        var vector2 = new Vector2(bitmapWidth / 2, (float)Math.Sin(Math.PI / 3) * bitmapWidth * 0.8f + offsetY);
        var vector3 = new Vector2(bitmapWidth - offsetX, offsetY);
        List<Vector2> initialVectors = [vector1, vector2, vector3, vector1];
        List<Vector2> vectors = Iterate(initialVectors, steps);
        return GetBitmap(vectors, bitmapWidth, bitmapWidth);
    }

    /// <summary>
    ///     Loops through each pair of adjacent vectors. Each line between two adjacent
    ///     vectors is divided into 4 segments by adding 3 additional vectors in-between
    ///     the original two vectors. The vector in the middle is constructed through a

View on GitHub (pinned to 96e2905cab)