private void DrawIcon(Vertex v1, Vertex v2)
        {
            BitmapSource source = null;

            if (v1.NextConstraint == Vertex.SideConstraint.Horizontal)
            {
                source = Constants.HorizontalImage as BitmapSource;
            }
            else if (v1.NextConstraint == Vertex.SideConstraint.Vertical)
            {
                source = Constants.VerticalImage as BitmapSource;
            }
            else if (v1.Constraint == Vertex.VertexConstraint.Angle || v2.Constraint == Vertex.VertexConstraint.Angle)
            {
                source = Constants.AngleImage as BitmapSource;
            }
            else
            {
                return;
            }

            int stride = source.PixelWidth * (source.Format.BitsPerPixel / 8);

            byte[] data = new byte[stride * source.PixelHeight];
            source.CopyPixels(data, stride, 0);

            int X;
            int Y;

            if (v1.Constraint == Vertex.VertexConstraint.Angle)
            {
                double d1 = v1.Prev.ToPoint().DistanceToPoint(v1.ToPoint());
                double d2 = v1.ToPoint().DistanceToPoint(v2.ToPoint());
                if (d2 < d1)
                {
                    X = (int)((2 * v1.X + v2.X) / 3 - source.Width / 2);
                    Y = (int)((2 * v1.Y + v2.Y) / 3 - source.Height / 2);
                }
                else
                {
                    var vec = Point.Subtract(v2.ToPoint(), v1.ToPoint()) * 1 / 3 * d1 / d2;
                    X = (int)(v1.X + vec.X - source.Width / 2);
                    Y = (int)(v1.Y + vec.Y - source.Height / 2);
                }
            }
            else if (v2.Constraint == Vertex.VertexConstraint.Angle)
            {
                double d1 = v1.ToPoint().DistanceToPoint(v2.ToPoint());
                double d2 = v2.ToPoint().DistanceToPoint(v2.Next.ToPoint());
                if (d1 < d2)
                {
                    X = (int)((v1.X + 2 * v2.X) / 3 - source.Width / 2);
                    Y = (int)((v1.Y + 2 * v2.Y) / 3 - source.Height / 2);
                }
                else
                {
                    var vec = Point.Subtract(v1.ToPoint(), v2.ToPoint()) * 1 / 3 * d2 / d1;
                    X = (int)(v2.X + vec.X - source.Width / 2);
                    Y = (int)(v2.Y + vec.Y - source.Height / 2);
                }
            }
            else
            {
                X = (int)((v1.X + v2.X - source.Width) / 2);
                Y = (int)((v1.Y + v2.Y - source.Height) / 2);
            }

            if (X < 0)
            {
                X = 0;
            }
            if (Y < 0)
            {
                Y = 0;
            }
            if (X > BitmapWidth - source.PixelWidth)
            {
                X = BitmapWidth - source.PixelWidth;
            }
            if (Y > BitmapHeight - source.PixelHeight)
            {
                Y = BitmapHeight - source.PixelHeight;
            }

            bitmap.WritePixels(new Int32Rect(X, Y, source.PixelWidth, source.PixelHeight), data, stride, 0);
        }
        private void Canvas_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            var p = e.GetPosition(Canvas);

            if (!drawMode)
            {
                foreach (var polygon in polygons)
                {
                    Vertex clickedVertex = polygon.GetClickedVertex(p);
                    if (clickedVertex != null)
                    {
                        currentPolygon = polygon;
                        ContextMenu menu = new ContextMenu();
                        menu.Items.Add(new MenuItem()
                        {
                            Header           = "Remove vertex",
                            Command          = removeCommand,
                            CommandParameter = clickedVertex
                        });
                        if (clickedVertex.Constraint == Vertex.VertexConstraint.None)
                        {
                            menu.Items.Add(new MenuItem()
                            {
                                Header           = "Add angle constraint",
                                Command          = angleCommand,
                                CommandParameter = clickedVertex
                            });
                        }
                        else
                        {
                            menu.Items.Add(new MenuItem()
                            {
                                Header           = "Clear constraint",
                                Command          = clearVertexCommand,
                                CommandParameter = clickedVertex
                            });
                        }
                        menu.Margin = new Thickness(p.X, p.Y, 0, 0);
                        menu.IsOpen = true;
                    }
                    else
                    {
                        Vertex clickedSideFirstVertex = polygon.GetClickedSideFirstVertex(p);
                        if (clickedSideFirstVertex != null)
                        {
                            currentPolygon = polygon;
                            ContextMenu menu = new ContextMenu();
                            if (clickedSideFirstVertex.NextConstraint == Vertex.SideConstraint.None)
                            {
                                menu.Items.Add(new MenuItem()
                                {
                                    Header           = "Add horizontal constraint",
                                    Command          = horizontalCommand,
                                    CommandParameter = clickedSideFirstVertex
                                });
                                menu.Items.Add(new MenuItem()
                                {
                                    Header           = "Add vertical constraint",
                                    Command          = verticalCommand,
                                    CommandParameter = clickedSideFirstVertex
                                });
                            }
                            else
                            {
                                menu.Items.Add(new MenuItem()
                                {
                                    Header           = "Clear constraint",
                                    Command          = clearSideCommand,
                                    CommandParameter = clickedSideFirstVertex
                                });
                            }
                            menu.Margin = new Thickness(p.X, p.Y, 0, 0);
                            menu.IsOpen = true;
                        }
                    }
                }
            }
        }