Example #1
0
        private void createEdge(Ellipse startVert, Ellipse endVert, string edgeName)
        {
            
            Vector startVertVec = VisualTreeHelper.GetOffset(startVert);
            Vector endVertVec = VisualTreeHelper.GetOffset(endVert);
            // Vectors are created in the upper left corner of ellipse, so move the vector to center.
            Point startCenterPt = new Point(startVertVec.X + startVert.GetRadius(), startVertVec.Y + startVert.GetRadius());
            Point endCenterPt = new Point(endVertVec.X + endVert.GetRadius(), endVertVec.Y + endVert.GetRadius());
            Path edge = new Path();
            edge.Name= edgeName;

            //make shaft
            LineGeometry shaftGeo = new LineGeometry();
            rescaleLine(shaftGeo, startCenterPt, endCenterPt, startVert.GetRadius(), endVert.GetRadius());
            
            //make arrowhead
            Vector vect = startCenterPt - endCenterPt;
            vect.Normalize();
            vect *= (Math.Sqrt(Math.Pow(startCenterPt.X - endCenterPt.X, 2) + Math.Pow(startCenterPt.Y - endCenterPt.Y, 2)) / 14) + 10;
            Matrix rotationMx = new Matrix();
            LineGeometry arrowHead1 = new LineGeometry();
            arrowHead1.StartPoint = shaftGeo.EndPoint;
            rotationMx.Rotate(30);
            arrowHead1.EndPoint = shaftGeo.EndPoint + vect * rotationMx;
            LineGeometry arrowHead2 = new LineGeometry();
            arrowHead2.StartPoint = shaftGeo.EndPoint;
            rotationMx.Rotate(-60);
            arrowHead2.EndPoint = shaftGeo.EndPoint + vect * rotationMx;
            
            // group shaft + arrowhead
            GeometryGroup edgeGeo = new GeometryGroup();
            edgeGeo.Children.Add(shaftGeo);
            edgeGeo.Children.Add(arrowHead1);
            edgeGeo.Children.Add(arrowHead2);
            edge.Data = edgeGeo;
            edge.Stroke = Brushes.Blue;
            edge.StrokeThickness = 2;

            TextBox capacityBox = new TextBox();
            capacityBox.Width = 30;
            capacityBox.Text = "";
            capacityBox.Tag = edge;
            capacityBox.Name = edgeName + "capacity";
            //capacityBox.LostKeyboardFocus += capacityBox_LostKeyboardFocus;
            capacityBox.KeyUp += capacityBox_KeyUp;
            Canvas.SetLeft(capacityBox, (startCenterPt.X + endCenterPt.X) / 2);
            Canvas.SetTop(capacityBox, (startCenterPt.Y + endCenterPt.Y) / 2);
            canvas.Children.Add(capacityBox);
            canvas.Children.Add(edge);
            vertices.ForEach(x => x.MouseDown -= vertice_MouseDown);
            capacityBox.Tag = edge;
            Keyboard.Focus(capacityBox);
        }