0

I have two UIElements(i.e. rectangles) in Canvas and their coordinates. How can I connect them with arc in code behind?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Bip
  • 893
  • 5
  • 14
  • 29
  • I use google and i could not find anythin useful. I tried to create ArcSegment but without any success. It would be great to have: `ArcSegment arc = new ArcSegment(x1, y1, x2, y2);` – Bip May 09 '12 at 14:42

1 Answers1

2

No need to get an exact hit on the rectangles (or other objects): make sure the Z ordering is correct. arc.SetValue(Canvas.ZIndex, -1) will push it to the background. If you want a perpendicular hit, you'll need to break out the algebra :/

For the arc: (see http://msdn.microsoft.com/en-us/library/ms751808.aspx), it needs to be contained in a PathFigure.

Edit: this shows two connected rectangles. The line simple runs between the two centers. The arc starts on one center (the pathFigure startpoint), first argument is the center of the second object.

        r1 = new Rectangle();
        r1.Margin = new Thickness(50, 50, 0, 0);
        r1.VerticalAlignment = System.Windows.VerticalAlignment.Top;
        r1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
        r1.Height = 50;
        r1.Width= 50;
        r1.Fill = new SolidColorBrush(Colors.Red);


        r2 = new Rectangle();
        r2.Width = 50;
        r2.Height = 50;
        r2.Fill = new SolidColorBrush(Colors.Blue);
        r2.Margin = new Thickness(350, 450, 0, 0);
        r2.VerticalAlignment = System.Windows.VerticalAlignment.Top;
        r2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

        l = new Line();
        l.X1 = 75;
        l.Y1 = 75;
        l.X2 = 375;
        l.Y2 = 475;
        l.Fill = new SolidColorBrush(Colors.Purple);
        l.Stroke = new SolidColorBrush(Colors.Purple);
        l.StrokeThickness = 2;
        l.SetValue(Canvas.ZIndexProperty, -1);

        PathGeometry myPathGeometry = new PathGeometry();

        // Create a figure.
        PathFigure pathFigure1 = new PathFigure();
        pathFigure1.StartPoint = new Point(75, 75);

        pathFigure1.Segments.Add(
            new ArcSegment(
                new Point(375, 475),
                new Size(50, 50),
                45,
                true, /* IsLargeArc */
                SweepDirection.Clockwise,
                true /* IsStroked */ ));
        myPathGeometry.Figures.Add(pathFigure1);

        // Display the PathGeometry. 
        Path myPath = new Path();
        myPath.Stroke = Brushes.Black;

        myPath.StrokeThickness = 1;
        myPath.Data = myPathGeometry;
        myPath.SetValue(Canvas.ZIndexProperty, -1);

        LayoutRoot.Children.Add(r1);
        LayoutRoot.Children.Add(r2);
        LayoutRoot.Children.Add(l);
        LayoutRoot.Children.Add(myPath);
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Nzc
  • 170
  • 1
  • 5
  • Not if you hide the line behind the two other shapes. The arc can run between the centers of the two other objects. The z ordering takes care of the rest. – Nzc May 10 '12 at 07:40
  • The math is contained in the Arc object. – Nzc May 11 '12 at 08:57