AvaloniaUI/Avalonia · error · ArgumentException

maxDegree must be between 1 and 3

Error message

maxDegree must be between 1 and 3

What it means

Thrown by SetMaxDegree on the precise elliptic-arc helper when maxDegree is outside the inclusive range 1..3. The Bezier approximation code only supports degrees 1 (linear), 2 (quadratic), and 3 (cubic); any other value has no implementation path.

Source

Thrown at src/Avalonia.Base/Media/PreciseEllipticArcHelper.cs:422

                _sinTheta = Math.Sin(theta);
                _maxDegree = 3;
                _defaultFlatness = 0.5; //half a pixel
                ComputeFocii();
                ComputeEndPoints();
                ComputeBounds();
                ComputeDerivedFlatnessParameters();
            }

            /// <summary>
            /// Sets the maximal degree allowed for Bezier curve approximation.
            /// </summary>
            /// <param name="maxDegree">Maximal allowed degree (must be between 1 and 3)</param>
            /// <exception cref="ArgumentException">Thrown if maxDegree is not between 1 and 3</exception>
            public void SetMaxDegree(int maxDegree)
            {
                if (maxDegree < 1 || maxDegree > 3)
                {
                    throw new ArgumentException(@"maxDegree must be between 1 and 3", nameof(maxDegree));
                }
                _maxDegree = maxDegree;
            }

            /// <summary>
            /// Sets the default flatness for Bezier curve approximation
            /// </summary>
            /// <param name="defaultFlatness">default flatness (must be greater than 1e-10)</param>
            /// <exception cref="ArgumentException">Thrown if defaultFlatness is lower than 1e-10</exception>
            public void SetDefaultFlatness(double defaultFlatness)
            {
                if (defaultFlatness < 1.0E-10)
                {
                    throw new ArgumentException(@"defaultFlatness must be greater than 1.0e-10", nameof(defaultFlatness));
                }
                _defaultFlatness = defaultFlatness;
            }

View on GitHub (pinned to 11c5427268)

Solutions

  1. Pass 1, 2, or 3 (3 is the typical default for cubic approximation quality).
  2. Clamp the input: var d = Math.Clamp(userValue, 1, 3); arc.SetMaxDegree(d);
  3. Validate the source configuration range (slider min=1, max=3).

Example fix

// before
arc.SetMaxDegree(userQuality); // userQuality could be 0 or 5

// after
arc.SetMaxDegree(Math.Clamp(userQuality, 1, 3));
Defensive patterns

Strategy: validation

Validate before calling

var d = Math.Clamp(maxDegree, 1, 3);
arc.SetMaxDegree(d);

Type guard

static bool IsValidDegree(int d) => d is >= 1 and <= 3;

Prevention

When it happens

Trigger: arc.SetMaxDegree(0), arc.SetMaxDegree(4), or arc.SetMaxDegree(-1).

Common situations: Configuring arc flattening quality from a UI slider or config value that allows out-of-range integers; defaulting an uninitialized int (0) into SetMaxDegree.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/f989d910742baeb5. Report an issue: GitHub.