Skip to content

AddLine creates zigzag pattern instead of separate parallel lines when StartFigure is called #396

Description

@RonHeck

Problem Description

When calling StartFigure() followed by AddLine() multiple times, the resulting path creates a connected zigzag pattern instead of separate parallel lines.

Expected Behavior

Each call to StartFigure() should begin a new independent figure. Subsequent AddLine() calls should draw isolated line segments that don't connect to previous figures.

Current Behavior

The lines are being connected together, creating a zigzag pattern that connects all the line segments sequentially.

Reproduction Code

XGraphicsPath path = new XGraphicsPath();
path.StartFigure();
path.AddLine(new XPoint(50, 50), new XPoint(100, 50));
path.StartFigure();
path.AddLine(new XPoint(50, 100), new XPoint(100, 100));
path.StartFigure();
path.AddLine(new XPoint(50, 150), new XPoint(100, 150));
path.StartFigure();
path.AddLine(new XPoint(50, 200), new XPoint(100, 200));
gfx.DrawPath(pen, path);

Expected Result: Four separate horizontal parallel lines
Actual Result: A connected zigzag pattern

Root Cause Analysis

The issue is in the AddLine(double x1, double y1, double x2, double y2) method (lines 276-306). The WPF implementation has problematic logic:

#if WPF
    PathFigure figure = CurrentPathFigure;
    if (figure.Segments.Count == 0)
    {
        figure.StartPoint = new SysPoint(x1, y1);
        var lineSegment = new LineSegment(new SysPoint(x2, y2), true);
        figure.Segments.Add(lineSegment);
    }
    else
    {
        // This branch connects to the previous point!
        var lineSegment1 = new LineSegment(new SysPoint(x1, y1), true);
        var lineSegment2 = new LineSegment(new SysPoint(x2, y2), true);
        figure.Segments.Add(lineSegment1);
        figure.Segments.Add(lineSegment2);
    }
#endif

The Problem:
When figure.Segments.Count > 0 (i.e., the figure already has segments), the code adds TWO line segments:

  1. A line from the current position to (x1, y1) - this connects to the previous line's endpoint
  2. A line from (x1, y1) to (x2, y2) - the intended line

This causes unwanted connections between figures that should be independent after StartFigure() is called.

Expected Implementation

After StartFigure() creates a new figure, the figure should be empty, and the next AddLine() call should behave as if it's the first line (setting StartPoint to x1, y1 and adding a single segment to x2, y2), not connecting to previous figures.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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