Skip to content

Incorrect Ternary Coordinate Validation in TriangularAxis.GetCoordinates #4797

@manaruto

Description

@manaruto

The current implementation of GetCoordinates in TriangularAxis incorrectly validates ternary coordinates. It enforces that leftFraction + rightFraction = 1, which violates the fundamental rule of ternary plots, where the sum of bottomFraction + leftFraction + rightFraction must always equal 1.

Current Problematic Code


public Coordinates GetCoordinates(double bottomFraction, double leftFraction, double rightFraction)
{
    if (leftFraction + rightFraction != 1)
    {
        throw new ArgumentException("sum of left and right fractions must equal 1");
    }

    return GetCoordinates(bottomFraction, leftFraction);
}

Issues with this Code

1-Violates the ternary constraint:

  • The correct ternary rule states that: bottomFraction+leftFraction+rightFraction=1
  • The current check only ensures left + right = 1, which means bottomFraction can be anything, leading to invalid ternary coordinates.

2-Allows invalid values:

  • Example input (0.5, 0.8, 0.2) results in 1.5 (invalid), but the current function does not catch this.
  • Example input (0.5, 0.3, 0.2) is valid (sum = 1) but gets rejected because left + right != 1.

3-Does not support clockwise vs counterclockwise configurations:

  • Ternary plots can be visualized clockwise or counterclockwise, but the existing function does not account for that.

Proposed Fix:

    public Coordinates GetCoordinates(double bottomFraction, double leftFraction, double rightFraction)
    {
        if (Math.Abs(bottomFraction + leftFraction + rightFraction - 1) > 1e-6)
        {
            throw new ArgumentException("The sum of bottom, left, and right fractions must equal 1.");
        }

        double x, y;
        
        if (!Clockwise)
        {
            // Counterclockwise transformation
            x = 0.5 * (2 * bottomFraction + rightFraction);
            y = (Math.Sqrt(3) / 2) * rightFraction;
        }
        else
        {
            // Clockwise transformation
            x = 0.5 * (2 * rightFraction + leftFraction);
            y = (Math.Sqrt(3) / 2) * leftFraction;
        }
        
        return new Coordinates(x, y);
    }

ScottPlot 5.0.54

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions