wpf Canvas画箭线

 private void DrawLine(Point p1, Point p2)
        {
            var line = new Line();
            line.Stroke = Brushes.Red;
            line.StrokeThickness = 2;
            line.X1 = p1.X;
            line.Y1 = p1.Y;
            line.X2 = p2.X;
            line.Y2 = p2.Y;
            this.CanvasPaintPath.Children.Add(line);

            var headlen = 10;//箭头线的长度
            var theta = 45;//箭头线与直线的夹角,45°
            double arrowX, arrowY;//箭头线终点坐标
            // 计算各角度和对应的箭头终点坐标
            var angle = Math.Atan2(p1.Y - p2.Y, p1.X - p2.X) * 180 / Math.PI;
            var angle1 = (angle + theta) * Math.PI / 180;
            var angle2 = (angle - theta) * Math.PI / 180;
            var topX = headlen * Math.Cos(angle1);
            var topY = headlen * Math.Sin(angle1);
            var botX = headlen * Math.Cos(angle2);
            var botY = headlen * Math.Sin(angle2);
            arrowX = p2.X + topX;
            arrowY = p2.Y + topY;

            var line2 = new Line();
            line2.Stroke = Brushes.Red;
            line2.StrokeThickness = 2;
            line2.X1 = arrowX;
            line2.Y1 = arrowY;
            line2.X2 = p2.X;
            line2.Y2 = p2.Y;
            this.CanvasPaintPath.Children.Add(line2);

            arrowX = p2.X + botX;
            arrowY = p2.Y + botY;
            var line3 = new Line();
            line3.Stroke = Brushes.Red;
            line3.StrokeThickness = 2;
            line3.X1 = arrowX;
            line3.Y1 = arrowY;
            line3.X2 = p2.X;
            line3.Y2 = p2.Y;
            this.CanvasPaintPath.Children.Add(line3);
        }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值