如何将画布上绘制的线切割成两部分

时间:2015-04-08 08:27:57

标签: c# wpf line

说我在画布上画了一条线。我只需要剪切/拆分Line就可以有两行。

Line line = new Line();
SolidColorBrush brush = new SolidColorBrush();
brush.Color = colorPicker2.SelectedColor;
line.Stroke = brush;
line.StrokeThickness = SliderThickness.Value;
line.X1 = currentPoint.X;
line.Y1 = currentPoint.Y;
line.X2 = e.GetPosition(DrawPanel).X;
line.Y2 = e.GetPosition(DrawPanel).Y;
currentPoint = e.GetPosition(DrawPanel);
DrawPanel.Children.Add(line);

对不起,我没有具体说明。用户将点击该线的一个点,该线将在该点切割成两个部分。然后用户将拖动或移动线的每个部分。

2 个答案:

答案 0 :(得分:0)

试试这个:

var middleX = (line.X1 + line.X2) / 2;
var middleY = (line.Y1 + line.Y2) / 2;

Line line1 = new Line();
line1.X1 = line.X1;
line1.Y1 = line.Y1;
line1.X2 = middleX;
line1.Y2 = middleY;

Line line2 = new Line();
line2.X1 = middleX;
line2.Y1 = middleY;
line2.X2 = line.X2;
line2.Y2 = line.Y2;

答案 1 :(得分:0)

试试这个:

            Line line = new Line();
            SolidColorBrush brush = new SolidColorBrush();
            brush.Color = colorPicker2.SelectedColor;
            line.Stroke = brush;
            line.StrokeThickness = SliderThickness.Value;
            line.X1 = currentPoint.X;
            line.Y1 = currentPoint.Y;
            line.X2 = e.GetPosition(DrawPanel).X;
            line.Y2 = e.GetPosition(DrawPanel).Y;
            currentPoint = e.GetPosition(DrawPanel);
            DrawPanel.Children.Add(line);

            Point middlePoint = ((line.X1 + line.X2) / 2, (line.Y1 + line.Y2) / 2)

            Line newFirst = new Line();
            newFirst.X1 = line.X1;
            newFirst.Y1 = line.Y1;
            newFirst.X2 = middlePoint.X;
            newFirst.Y2 = middlePoint.Y;
            newFirst.Stroke = brush;
            newFirst.StrokeThickness = SliderThickness.Value;

            Line newSecond = new Line();
            newSecond.X1 = middlePoint.X;
            newSecond.Y1 = middlePoint.Y;
            newSecond.X2 = line.X2;
            newSecond.Y2 = line.Y2;
            newSecond.Stroke = brush;
            newSecond.StrokeThickness = SliderThickness.Value;

            DrawPanel.Children.Remove(line);
            DrawPanel.Children.Add(newFirst);
            DrawPanel.Children.Add(newSecond);