0

this code is working for drawing multiple lines by mouse clicks. But the lines are overlapping or intersecting even if one line is already created.

Can anyone help me how can i draw multiple lines [the working code for drawing multiple lines are given] without overlapping each other inestead it would find another round way to avoid conflicts to other lines.

    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();
    }
}
APALALA
  • 60
  • 9
  • You are collcting only two points so far. For more lines you need to collect more, best in a `List pointlist`. To draw them use `if (pointlist.Count > 1) DrawLines – TaW Feb 21 '16 at 16:00
  • 1
    This is a classic Winforms programming problem, with lots of relevant Q&As on the site already. As the previous comment suggests, the first step is to fix dealing with multiple lines. See the marked duplicate for that. Once you have solved that problem, please try to solve the "crossing" problem yourself; if you're unable to, post a new question in which you've included a good [mcve] showing what you've tried, with a clear, concise description of what the code does and what you want it to do instead. – Peter Duniho Feb 21 '16 at 17:40

0 Answers0