private void AddLineAfterVerticleDeletion(Line lineOne, Line lineTwo, int deletedVerticleId)
        {
            List <int> verticlesIds = new List <int>
            {
                lineOne.VerticleOneId == deletedVerticleId ? lineOne.VerticleTwoId : lineOne.VerticleOneId,
                lineTwo.VerticleOneId == deletedVerticleId ? lineTwo.VerticleTwoId : lineTwo.VerticleOneId
            };

            Verticle verticleOne = _verticles.FirstOrDefault(v => v.Id == verticlesIds[0]);
            Verticle verticleTwo = _verticles.FirstOrDefault(v => v.Id == verticlesIds[1]);

            _lines.Add(DrawLine(verticleOne, verticleTwo));
        }
        private void MoveVerticle(object sender, MouseEventArgs e)
        {
            if (_isMovingVerticleSet)
            {
                // usuwanie wierzchołka i linii

                canvas.Children.Remove(_movingVerticle.Rectangle);

                int verticleId = _movingVerticle.Id;

                List <Line> linesToDelete = new List <Line>(_lines.Where(l => l.VerticleOneId == verticleId || l.VerticleTwoId == verticleId));

                List <int> verticlesIds = new List <int>();

                foreach (var line in linesToDelete)
                {
                    foreach (var rectangle in line.rectangles)
                    {
                        canvas.Children.Remove(rectangle);
                    }
                    verticlesIds.Add(line.VerticleOneId == verticleId ? line.VerticleTwoId : line.VerticleOneId);
                    _lines.Remove(line);
                }

                // -------------

                // nowy punkt wierzchołka
                Point p = Mouse.GetPosition((Canvas)sender);
                int   x = (int)p.X;
                int   y = (int)p.Y;

                Rectangle newRectangle = SetPixel(x, y, VerticleSize, true);
                newRectangle.ContextMenu          = _verticleContextMenu;
                newRectangle.MouseLeftButtonDown += LeftButtonDownVerticle;
                newRectangle.MouseLeftButtonUp   += LeftButtonUpVerticle;

                _movingVerticle.SetNewRectangle(x, y, newRectangle);

                // --------------------

                // przerysowanie linii

                foreach (var verId in verticlesIds)
                {
                    Verticle ver = _verticles.FirstOrDefault(v => v.Id == verId);
                    _lines.Add(DrawLine(ver, _movingVerticle));
                }

                //--------------------------------
            }
        }
        private void LineHelper(object sender, MouseEventArgs e)
        {
            if (_verticles.Count == 0)
            {
                return;
            }

            ClearHelpingLine();

            Point p = Mouse.GetPosition((Canvas)sender);
            int   x = (int)p.X;
            int   y = (int)p.Y;

            Verticle extraVerticle = new Verticle(int.MaxValue, x, y);
            Verticle lastVerticle  = _verticles.LastOrDefault();

            _helpingLine = DrawLine(lastVerticle, extraVerticle);
        }
        private void LeftButtonDownVerticle(object sender, MouseButtonEventArgs e)
        {
            if (_isMovingVerticleSet)
            {
                _movingVerticle      = null;
                _isMovingVerticleSet = false;
                return;
            }

            Rectangle rectangle = sender as Rectangle;

            if (rectangle != null)
            {
                _movingVerticle = _verticles.FirstOrDefault(v => Equals(v.Rectangle, rectangle));

                _isMovingVerticleSet = true;
            }
        }
        private void DeleteLine(Verticle verticle)
        {
            int verticleId = verticle.Id;

            List <Line> linesToDelete = new List <Line>(_lines.Where(l => l.VerticleOneId == verticleId || l.VerticleTwoId == verticleId));

            foreach (var line in linesToDelete)
            {
                foreach (var rectangle in line.rectangles)
                {
                    canvas.Children.Remove(rectangle);
                }
                _lines.Remove(line);
            }

            if (linesToDelete.Count == 2)
            {
                AddLineAfterVerticleDeletion(linesToDelete[0], linesToDelete[1], verticleId);
            }
        }
        private void DeleteVerticle(object sender, RoutedEventArgs routedEventArgs)
        {
            if (_polygonMade && _verticles.Count < 3)
            {
                return;
            }

            MenuItem mi = sender as MenuItem;

            if (mi != null)
            {
                Rectangle rc = ((ContextMenu)mi.Parent).PlacementTarget as Rectangle;
                canvas.Children.Remove(rc);

                Verticle verticle = _verticles.FirstOrDefault(v => Equals(v.Rectangle, rc));

                DeleteLine(verticle);
                _verticles.Remove(verticle);
            }
        }
        private void EndDrawingPolygon(object sender, RoutedEventArgs routedEventArgs)
        {
            if (_verticles.Count < 3)
            {
                return;
            }

            _polygonMade = true;

            canvas.MouseLeftButtonDown -= SetVerticle;
            canvas.MouseMove           -= LineHelper;
            canvas.MouseLeftButtonDown += LeftButtonDownPolygon;
            canvas.MouseLeftButtonUp   += LeftButtonUpPolygon;
            foreach (var ver in _verticles)
            {
                ver.Rectangle.MouseLeftButtonDown += LeftButtonDownVerticle;
                ver.Rectangle.MouseLeftButtonUp   += LeftButtonUpVerticle;
            }
            _verticles.FirstOrDefault(v => v.Id == 0).Rectangle.ContextMenu = _verticleContextMenu;

            ClearHelpingLine();

            MenuItem mi = sender as MenuItem;

            if (mi != null)
            {
                Rectangle rc = ((ContextMenu)mi.Parent).PlacementTarget as Rectangle;

                Verticle endVerticle  = _verticles.FirstOrDefault(v => Equals(v.Rectangle, rc));
                Verticle lastVerticle = _verticles.LastOrDefault();

                _lines.Add(DrawLine(lastVerticle, endVerticle));
            }

            canvas.MouseMove += MoveVerticle;
        }
        private Line DrawLine(Verticle v1, Verticle v2)
        {
            int x1 = v1.X;
            int x2 = v2.X;
            int y1 = v1.Y;
            int y2 = v2.Y;

            List <Rectangle> listOfRectangles = new List <Rectangle>();

            int d, dx, dy, ai, bi, xi, yi;
            int x = x1, y = y1;

            // ustalenie kierunku rysowania
            if (x1 < x2)
            {
                xi = 1;
                dx = x2 - x1;
            }
            else
            {
                xi = -1;
                dx = x1 - x2;
            }
            // ustalenie kierunku rysowania
            if (y1 < y2)
            {
                yi = 1;
                dy = y2 - y1;
            }
            else
            {
                yi = -1;
                dy = y1 - y2;
            }

            // pierwszy piksel
            listOfRectangles.Add(SetPixel(x, y, LinePointSize));

            // oś wiodąca OX
            if (dx > dy)
            {
                ai = (dy - dx) * 2;
                bi = dy * 2;
                d  = bi - dx;
                // pętla po kolejnych x
                while (x != x2)
                {
                    // test współczynnika
                    if (d >= 0)
                    {
                        x += xi;
                        y += yi;
                        d += ai;
                    }
                    else
                    {
                        d += bi;
                        x += xi;
                    }
                    listOfRectangles.Add(SetPixel(x, y, LinePointSize));
                }
            }
            // oś wiodąca OY
            else
            {
                ai = (dx - dy) * 2;
                bi = dx * 2;
                d  = bi - dy;
                // pętla po kolejnych y
                while (y != y2)
                {
                    // test współczynnika
                    if (d >= 0)
                    {
                        x += xi;
                        y += yi;
                        d += ai;
                    }
                    else
                    {
                        d += bi;
                        y += yi;
                    }
                    listOfRectangles.Add(SetPixel(x, y, LinePointSize));
                }
            }
            return(new Line(v1.Id, v2.Id, listOfRectangles));
        }
 private void LeftButtonUpVerticle(object sender, MouseButtonEventArgs e)
 {
     _movingVerticle      = null;
     _isMovingVerticleSet = false;
 }