在鼠标单击时绘制多行但不重叠

时间:2016-02-21 15:24:36

标签: c# drawing mouseevent overlapping

此代码用于通过鼠标单击绘制多行。但即使已经创建了一条线,这些线也会重叠或相交。

任何人都可以帮助我如何绘制多条线[给出绘制多条线的工作代码]而不会相互重叠,相反它会找到另一种方法来避免与其他线条发生冲突。

    public partial class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    private Point _setPointA, _setPointB;
    private static List<Point> PointList = new List<Point>();
    private Dictionary<Point, Point> PointDictionary = new Dictionary<Point, Point>();

    public Form1()
    {
        PointDictionary.Add(new Point { X = 10, Y = 10 }, new Point { X = 85, Y = 85 });

    }
    protected override void OnMouseClick(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            if (!PointList.Any())
            {
                _setPointA = e.Location;
                PointList.Add(_setPointA);
            }
            else
            {
                _setPointB = e.Location;
                PointList.Add(_setPointB);

                //check if not intersects

                if (_setPointA.X <= _setPointB.X)
                {
                    PointDictionary.Add(_setPointA, _setPointB);
                }
                else
                {
                    PointDictionary.Add(_setPointB, _setPointA);
                }
                Refresh();
            }
        }
        base.OnMouseClick(e);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        foreach (var v in PointDictionary)
        {
            e.Graphics.DrawLine(SystemPens.ControlDarkDark, v.Key, v.Value);
        }

        base.OnPaint(e);
        PointList.Clear();
    }
}

0 个答案:

没有答案