代码之家  ›  专栏  ›  技术社区  ›  Ace

实时绘制到下一点的直线

  •  1
  • Ace  · 技术社区  · 12 年前

    我当前的程序允许用户单击一个点,然后单击另一个点(至少20个像素),并在这两个点之间画一条线。我使用了一条多段线,这样就可以多次执行。尽管所有线条的集合只有在完成所有单击之后才会出现。

    void DrawingCanvas_MouseUp(object sender, MouseButtonEventArgs e) {
            Point position = e.GetPosition(this);
    
            if (leftList == null) {
                //starting a new set
                leftList.Add(position);
                lastPoint = position;
                return;
            }
            //calculate distance, i.e. end click
            double a = lastPoint.X - position.X;
            double b = lastPoint.Y - position.Y;
            double distance = Math.Sqrt(a * a + b * b);
            if (distance > 20) {
                //continue to add to list
                leftList.Add(position);
                lastPoint = position;
            } else {
                //end of the line
                paint();
                leftList = new PointCollection(); 
            }
    
        }
    
        private void paint() {
            Polyline line = new Polyline();
            line.Visibility = System.Windows.Visibility.Visible;
            line.StrokeThickness = 2;
            line.Stroke = System.Windows.Media.Brushes.Black;
            line.Points = leftList;
            myCanvas.Children.Add(line);
        }
    

    所以我的问题有两个:

    A) 我如何做到每次点击后都会立即添加新行。

    B) 如何渲染上一点和鼠标光标当前所在位置之间的线(即,在选择下一点之前)

    2 回复  |  直到 12 年前
        1
  •  2
  •   Clemens    12 年前

    以下简单示例在按住鼠标左键并将鼠标移动最小点距离20的同时开始绘制新的多段线。它以红色或绿色绘制最后一条多段线线段(到当前鼠标位置),具体取决于其长度。如果释放鼠标按钮,并且新线段的长度为>=20,一个新的点被附加到多段线。否则,多段线将终止,并且可以创建新的多段线。

    private Polyline polyline;
    private Polyline segment = new Polyline { StrokeThickness = 2 };
    
    private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (polyline == null)
        {
            var canvas = (Canvas)sender;
            var point = e.GetPosition(canvas);
    
            // create new polyline
            polyline = new Polyline { Stroke = Brushes.Black, StrokeThickness = 2 };
            polyline.Points.Add(point);
            canvas.Children.Add(polyline);
    
            // initialize current polyline segment
            segment.Stroke = Brushes.Red;
            segment.Points.Add(point);
            segment.Points.Add(point);
            canvas.Children.Add(segment);
        }
    }
    
    private void Canvas_MouseMove(object sender, MouseEventArgs e)
    {
        if (polyline != null)
        {
            // update current polyline segment
            var canvas = (Canvas)sender;
            segment.Points[1] = e.GetPosition(canvas);
    
            var distance = (segment.Points[0] - segment.Points[1]).Length;
            segment.Stroke = distance >= 20 ? Brushes.Green : Brushes.Red;
        }
    }
    
    private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (polyline != null)
        {
            var canvas = (Canvas)sender;
            segment.Points[1] = e.GetPosition(canvas);
    
            var distance = (segment.Points[0] - segment.Points[1]).Length;
    
            if (distance >= 20)
            {
                polyline.Points.Add(segment.Points[1]);
                segment.Points[0] = segment.Points[1];
            }
            else
            {
                if (polyline.Points.Count < 2)
                {
                    canvas.Children.Remove(polyline);
                }
    
                polyline = null;
                segment.Points.Clear();
                canvas.Children.Remove(segment);
            }
        }
    }
    
        2
  •  0
  •   Shafqat Masood    12 年前

    请在每次点击时保留一个积分集合。在集合中,您可以添加一个类,该类将具有StartPoint和EndPoint等两个属性。

    当第一次单击鼠标时,只需将一个类对象添加到只有起点的集合中。 当您下次单击鼠标时,将端点标记到类的最后一个对象,同时创建一个新对象,并将该点指定为其起点并将其添加到集合中,然后调用paint函数。