private void canvas_pic_MouseClick(object sender, MouseEventArgs e)
        {
            if (!KnotIsClosed)
            {
                Point newP = new Point(e.X, e.Y);
                newP = new Point(e.X, e.Y);
                MovablePoint movPoint = new MovablePoint(newP);
                //movPoint.MouseMove += MovPoint_MouseMove;

                if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
                {
                    CrossingType crossType = e.Button == MouseButtons.Left ? CrossingType.Under : CrossingType.Over;
                    //char charType = e.Button == MouseButtons.Left ? 'U' : 'O';
                    points.Add(movPoint);
                    canvas_pic.Controls.Add(movPoint);

                    if (points.Count >= 2)
                    {
                        MovablePoint beforeLast  = points.Skip(points.Count - 2).First();
                        Line         line        = new Line(beforeLast, movPoint);
                        MovablePoint last        = points.Last();
                        List <Point> interPoints = new List <Point>();

                        bool doNotCross = true;


                        for (int i = 0; i < lines.Count; i++)
                        {
                            Point?p = line.Intersect(lines[i]);
                            if (p.HasValue)
                            {
                                if (!lines[i].Intersections.Any(ip => ip.Distance(p.Value) <= 5))
                                {
                                    interPoints.Add(p.Value);

                                    IntersectionPoint p1 = new IntersectionPoint(p.Value, crossType);
                                    IntersectionPoint p2 = new IntersectionPoint(p.Value, crossType == CrossingType.Over ? CrossingType.Under : CrossingType.Over);
                                    p1.gaussCross = crossType == CrossingType.Over ? indexCross : -indexCross;
                                    p2.gaussCross = -p1.gaussCross;
                                    indexCross++;
                                    line.Intersections.Add(p1);
                                    lines[i].Intersections.Add(p2);
                                    lines[i].OrderPoints();
                                    doNotCross = false;

                                    int indexOfLine = renderingOrder.IndexOf(lines[i]);
                                    if (p1.CrossingType == CrossingType.Over)
                                    {
                                        renderingOrder.Insert(indexOfLine + 1, p1);
                                        renderingOrder.Add(line);
                                    }
                                    else
                                    {
                                        renderingOrder.Insert(indexOfLine, p1);
                                        renderingOrder.Insert(indexOfLine, line);
                                    }
                                }
                            }
                        }

                        if (doNotCross)
                        {
                            renderingOrder.Add(line);
                        }

                        line.OrderPoints();
                        lines.Add(line);

                        //if (interPoints.Count > 0)
                        //    gaussCode_txt.Text += new string(charType, interPoints.Count);
                    }
                }

                this.Invalidate();
            }
        }
 public Line(MovablePoint start, MovablePoint finish)
 {
     Start         = start;
     Finish        = finish;
     Intersections = new List <IntersectionPoint>();
 }